diff --git a/linked-list/LinkedList.java b/linked-list/LinkedList.java new file mode 100644 index 0000000..112cd1a --- /dev/null +++ b/linked-list/LinkedList.java @@ -0,0 +1,107 @@ +class LinkedList { + private static ListNode head = null; + private static ListNode tail = null; + LinkedList(int data) { + ListNode node = new ListNode(data); + head = node; + tail = node; + } + + public void printList(){ + System.out.print("START -> "); + ListNode ptr = head; + while(ptr != null){ + System.out.print(ptr.getData()+" -> "); + ptr = ptr.getNext(); + } + System.out.println("END"); + } + + public void insertHead(int data){ + ListNode node = new ListNode(data); + if(head == null) + head = tail = node; + else{ + node.setNext(head); + head = node; + } + } + + public void insertTail(int data){ + ListNode node = new ListNode(data); + if(head == null) + head = tail = node; + else{ + tail.setNext(node); + tail = node; + } + } + + public void insertAfterNodeData(int afterThisData, int data){ + ListNode node = new ListNode(data); + if(head == null) + head = tail = node; + else{ + ListNode ptr = head; + while(ptr.getData() != afterThisData) + ptr = ptr.getNext(); + node.setNext(ptr.getNext()); + ptr.setNext(node); + } + } + + public void deleteHead(){ + if(head == null){ + System.out.print("List Empty !"); + return; + } + else{ + ListNode ptr = head; + head = ptr.getNext(); + ptr = null; + } + } + + public void deleteTail(){ + if(head == null){ + System.out.print("List Empty !"); + return; + } + else{ + ListNode ptr = head; + while(ptr.getNext().getNext() != null) + ptr = ptr.getNext(); + ptr.setNext(null); + tail = ptr; + tail.setNext(null); + } + } + + public void deleteNodeWithData(int toBeDeleted){ + if(head == null){ + System.out.print("List Empty !"); + return; + } + else{ + ListNode ptr = head; + while(ptr.getNext().getData() != toBeDeleted) + ptr = ptr.getNext(); + ptr.setNext(ptr.getNext().getNext()); + ptr = ptr.getNext(); + ptr = null; + } + } + + public static void main(String[] args) { + // TODO Auto-generated method stub + LinkedList L = new LinkedList(21); + L.insertHead(15); + L.insertTail(36); + L.insertTail(50); + L.insertTail(45); + L.insertAfterNodeData(36, 10); + L.printList(); + L.deleteNodeWithData(10); + L.printList(); + } +} diff --git a/linked-list/ListNode.java b/linked-list/ListNode.java new file mode 100644 index 0000000..b4baebd --- /dev/null +++ b/linked-list/ListNode.java @@ -0,0 +1,14 @@ +class ListNode{ + private int data; + private ListNode next = null; + + ListNode(int data){ + this.data = data; + } + + public int getData(){ return this.data; } + public ListNode getNext(){ return this.next; } + + public void setData(int data){ this.data = data; } + public void setNext(ListNode next){ this.next = next; } +} \ No newline at end of file diff --git a/linked-list/info b/linked-list/info new file mode 100644 index 0000000..4c84063 --- /dev/null +++ b/linked-list/info @@ -0,0 +1,10 @@ +Java files for singly Linked List + +I have hardcoded the insertion and deletion operations, instead of giving the user choice feature. +The main aim of this repository is the core algorithm, not user interaction. +If needed, switch case or other similar structures can be used to let the user decide what methods to call at what time. + +Also, there is only singly LL here. +Implementing doubly LL and circular LL is very much similar once you understand singly LL. +Maybe later I will add them too. +Feel free to ask me for them in case I don't.