From d12c5cbae4775b392a9646fb38b5335fd65b0b76 Mon Sep 17 00:00:00 2001 From: Sreshtha Mehrotra <55186163+sreshtha10@users.noreply.github.com> Date: Sun, 27 Sep 2020 14:07:33 +0530 Subject: [PATCH 1/2] Singly Linked List --- .../linkedlist/SinglyLinkedList.java | 115 ++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 com/williamfiset/datastructures/linkedlist/SinglyLinkedList.java diff --git a/com/williamfiset/datastructures/linkedlist/SinglyLinkedList.java b/com/williamfiset/datastructures/linkedlist/SinglyLinkedList.java new file mode 100644 index 0000000..52125d7 --- /dev/null +++ b/com/williamfiset/datastructures/linkedlist/SinglyLinkedList.java @@ -0,0 +1,115 @@ +class Node{ + int data; + Node next; +} +class LinkedList{ + Node head; + public void insert(int data){ + Node p = new Node(); + p.data = data; + p.next = null; + if(head == null){ + head = p; + } + else{ + Node n = head; + while(n.next != null){ + n = n.next; + } + n.next = p; + } + } + + public void print(){ + Node p = head; + if(head == null){ + System.out.println("List is empty"); + } + while(p!= null){ + System.out.println(p.data+" "); + p = p.next; + } + } + public void insertStart(int data){ + Node p = new Node(); + p.data= data; + p.next = head; + head = p; + + } + public void insertPos(int pos,int data){ + Node p = new Node(); + Node tmp = new Node(); + int count = 0; + p = head; + if(pos == 0){ + tmp.data = data; + tmp.next = head; + head = tmp; + return; + } + while(p!= null){ + if(count == pos-1){ + tmp.data = data; + tmp.next = p.next; + p.next = tmp; + return; + } + p = p.next; + count++; + } + } + public void delete(int data){ + Node p = new Node(); + Node tmp = new Node(); + p = head; + if(head == null){ + System.out.println("List is empty"); + return; + } + if(head.data == data){ + head = head.next; + return; + } + while(p.next!= null){ + if(p.next.data == data){ + tmp = p.next; + p.next = tmp.next; + return; + } + p = p.next; + } + System.out.println("Element not found"); + return; + } + public void reverse(){ + Node p = new Node(); + Node n = new Node(); + Node curr = new Node(); + curr = head; + p = null; + while(curr != null){ + n = curr.next; + curr.next = p; + p = curr; + curr = n; + } + head = p; + return; + } + +} + + +public class Main{ + public static void main(String args[]){ + LinkedList linkedlist = new LinkedList(); + linkedlist.insert(1); + linkedlist.insert(2); + linkedlist.insert(3); + linkedlist.insert(4); + linkedlist.insert(5); + linkedlist.reverse(); + linkedlist.print(); + } +} From 0f5c24fac18e800183f499434ee05520d6cd8b60 Mon Sep 17 00:00:00 2001 From: Sreshtha Mehrotra <55186163+sreshtha10@users.noreply.github.com> Date: Sun, 27 Sep 2020 14:08:12 +0530 Subject: [PATCH 2/2] Create CircularLinkedList.java --- .../linkedlist/CircularLinkedList.java | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 com/williamfiset/datastructures/linkedlist/CircularLinkedList.java diff --git a/com/williamfiset/datastructures/linkedlist/CircularLinkedList.java b/com/williamfiset/datastructures/linkedlist/CircularLinkedList.java new file mode 100644 index 0000000..1c4ea8b --- /dev/null +++ b/com/williamfiset/datastructures/linkedlist/CircularLinkedList.java @@ -0,0 +1,103 @@ +class CircularLinkedList{ + class Node{ + int data; + Node next; + public Node(int data){ + this.data = data; + } + } + Node last = null; + int length = 0; + public void print(){ + Node p; + if(last == null){ + System.out.println("List is empty"); + return; + } + p = last.next; + do{ + System.out.println(p.data); + p = p.next; + }while(p != last.next); + return; + } + public void insert(int data){ + Node p; + Node tmp = new Node(data); + if(last == null){ + last = tmp; + last.next = tmp; + length++; + return; + } + tmp.next = last.next; + last.next = tmp; + last = tmp; + length++; + return; + } + public void insertStart(int data){ + Node tmp = new Node(data); + if(last == null){ + last = tmp; + last.next = tmp; + length++; + return; + } + tmp.next = last.next; + last.next = tmp; + length++; + return; + } + public void delete(int data){ + Node tmp,p; + if(last == null){ + System.out.println("List is empty"); + return; + } + if(last.next == last){ + if( last.data == data){ + last.next = null; + last = null; + length--; + return; + } + else{ + System.out.println("Element not found"); + return; + } + } + if(last.next.data == data){ + last.next = last.next.next; + length--; + return; + } + p = last.next; + while(p.next != last.next){ + if(p.next.data == data ){ + tmp = p.next; + p.next = tmp.next; + if(p.next == last){ + last = p; + } + length--; + return; + } + p = p.next; + } + } +} + +public class Main{ + public static void main(String args[]){ + CircularLinkedList c = new CircularLinkedList(); + c.insert(1); + c.insert(2); + c.insert(3); + c.insert(4); + c.insert(5); + c.delete(5); + c.print(); + + } +}