From 68bd743257c68e14ccdfd84188f965f19a3339a1 Mon Sep 17 00:00:00 2001 From: Vatsal Gosaliya Date: Fri, 8 Jul 2016 13:52:12 +0530 Subject: [PATCH 1/4] Create linked-list --- linked-list | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 linked-list diff --git a/linked-list b/linked-list new file mode 100644 index 0000000..baf6f3d --- /dev/null +++ b/linked-list @@ -0,0 +1,2 @@ +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. From e61f8b3793c808170ccc4a51c899420a18d28def Mon Sep 17 00:00:00 2001 From: Vatsal Gosaliya Date: Fri, 8 Jul 2016 13:54:34 +0530 Subject: [PATCH 2/4] Delete linked-list --- linked-list | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 linked-list diff --git a/linked-list b/linked-list deleted file mode 100644 index baf6f3d..0000000 --- a/linked-list +++ /dev/null @@ -1,2 +0,0 @@ -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. From 7555c0baad386c3fbae2f8a4ee387bbe30599d69 Mon Sep 17 00:00:00 2001 From: Vatsal Gosaliya Date: Fri, 8 Jul 2016 13:56:27 +0530 Subject: [PATCH 3/4] Create info --- linked-list/info | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 linked-list/info 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. From bf1f76ee7d5ed2f89a727cd636cdcf67668ff6ab Mon Sep 17 00:00:00 2001 From: Vatsal Gosaliya Date: Fri, 8 Jul 2016 13:56:55 +0530 Subject: [PATCH 4/4] Add files via upload --- linked-list/LinkedList.java | 107 ++++++++++++++++++++++++++++++++++++ linked-list/ListNode.java | 14 +++++ 2 files changed, 121 insertions(+) create mode 100644 linked-list/LinkedList.java create mode 100644 linked-list/ListNode.java 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