From cd14481ff160dc9d1d76849d521d55d669ba5740 Mon Sep 17 00:00:00 2001 From: kasch309 Date: Mon, 26 Oct 2020 10:32:00 +0100 Subject: [PATCH] Add linked list for java --- linkedlist/java/LinkedList.java | 93 +++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 linkedlist/java/LinkedList.java diff --git a/linkedlist/java/LinkedList.java b/linkedlist/java/LinkedList.java new file mode 100644 index 00000000..2b3868b3 --- /dev/null +++ b/linkedlist/java/LinkedList.java @@ -0,0 +1,93 @@ +import java.lang.*; + +public class List { + + class Node { + + int key; + Node next; + + public Node (int key){ + this.key = key; + } + + public void print() { + System.out.println ("Key: " + key); + System.out.println ("Next: " + next); + } + } + + Node head; + + public List (Node head){ + this.head = head; + } + + public boolean isEmpty(){ + return head.next == null; + } + + public boolean exists (int key) { + Node current = head; + while (current != null){ + if (current.key == key) return true; + current = current.next; + } + return false; + } + + public void insertAtHead (Node toInsert){ + toInsert.next = head.next; + head.next = toInsert; + } + + public void insertAtTail (Node toInsert){ + Node current = head; + while (current.next != null){ + current = current.next; + } + current.next = toInsert; + toInsert.next = null; + } + + public void insertAfterKey (int key, Node toInsert){ + Node current = head; + if (!exists(key)) System.out.println ("Key not found."); + else { + while (current.key != key){ + current = current.next; + } + toInsert.next = current.next; + current.next = toInsert; + } + } + + public Node getElement(int pos){ + Node current = head; + for (int i = 1; i <= pos; i++){ + current = current.next; + } + return current; + } + + public int countElements (){ + Node current = head; + int counter = 0; + while (current != null){ + counter++; + current = current.next; + } + return counter; + } + + public void printList (){ + Node current = head; + while (current != null){ + System.out.println ("Key: " + current.key); + if (current.next == null) System.out.println ("Last Node."); + else System.out.println ("Next: " + current.next.key); + current = current.next; + } + System.out.print (countElements() + " Elements in this List."); + } +} \ No newline at end of file