From 70e71a77184d143928443736ae9a71cc9a93aedc Mon Sep 17 00:00:00 2001 From: Ritesh Yadav <90106115+riteshyadav27@users.noreply.github.com> Date: Sun, 16 Oct 2022 12:55:30 +0530 Subject: [PATCH] chore(Java) : add circular singly linked list (#848) * Singly Circular LinkedList Singly Circular LinkedList with functionality of Add, Add in front, Display, Reverse , Search element in LinkedList * Depth First Search * Breadth First Search * Update README.md * Update README.md * Update circularll.java adding output of this code * Update circularll.java adding output of this code * Update breadth_first_search.java Adding output of breadth_first_search * Update depth_first_search.java adding output of depth_first_search * Delete breadth_first_search.java * Delete depth_first_search.java * Delete circularll.java * create circular-singly-linkedlist.java * update readme.md * Update algorithms/Java/linked-lists/circular-singly-linkedlist.java Co-authored-by: Mohit Chakraverty <79406819+mohitchakraverty@users.noreply.github.com> * Update max-subarray-sum.cpp Co-authored-by: Mohit Chakraverty <79406819+mohitchakraverty@users.noreply.github.com> --- algorithms/Java/README.md | 2 + .../circular-singly-linkedlist.java | 119 ++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 algorithms/Java/linked-lists/circular-singly-linkedlist.java diff --git a/algorithms/Java/README.md b/algorithms/Java/README.md index 5e1670cf..f551b21c 100644 --- a/algorithms/Java/README.md +++ b/algorithms/Java/README.md @@ -18,8 +18,10 @@ - [Dijkstras](graphs/Dijkstras.java) - [Prims](graphs/Prims.java) + ## Linked Lists +- [Circular Singly Linked List](linked-lists/circular-singly-linkedlist.java) - [Circular](linked-lists/circular.java) - [Clone Linked List](linked-lists/clone-linkedlist.java) - [Doubly](linked-lists/doubly.java) diff --git a/algorithms/Java/linked-lists/circular-singly-linkedlist.java b/algorithms/Java/linked-lists/circular-singly-linkedlist.java new file mode 100644 index 00000000..77219aaf --- /dev/null +++ b/algorithms/Java/linked-lists/circular-singly-linkedlist.java @@ -0,0 +1,119 @@ + class circularll { + Node head; + Node tail; + class Node{ + int data; + Node next; + + Node(int data){ + this.data=data; + this.next=null; + } + } + + //add() function add element to the rear of the linkedlist + public void add(int data){ + Node newNode = new Node(data); + if(head==null){ + head=tail=newNode; + } + else{ + tail.next=newNode; + tail=tail.next; + } + tail.next=head; + } + + //display() function to show elements of linkedlist + public void display(){ + System.out.printf("\nThe Linkedlist is "); + Node temp=head; + do{ + System.out.print(temp.data+" "); + temp=temp.next; + }while (temp!=head); + } + + //search() function searches the element in the linkedlist + public void search(int target){ + int flag=0; + Node temp=head; + do{ + if(target==temp.data){ + System.out.print("\nTarget is found"); + flag=1; + break; + } + temp=temp.next; + }while (temp!=head); + + if (flag==0) + System.out.print("\nTarget Not Found"); + } + + + // addfront() function added a element to the front of the circular singly linkedlist + public void addfront(int data){ + Node newNode = new Node(data); + newNode.next=head; + head=newNode; + tail.next=head; + } + + //reverse() function is reversing the circular singly linkedlist + public void reverse(){ + Node previous=tail; + Node current=head,nextnode = head; + + + do{ + nextnode=current.next; + current.next=previous; + + previous=current; + current=nextnode; + + }while(current!=head); + + tail=head; + head=previous; + tail.next=head; + + } + + public static void main(String[] args) { + System.out.println("LinkedList"); + circularll ll = new circularll(); + ll.add(5); + ll.add(8); + ll.add(7); + ll.add(1); + ll.add(2); + ll.display(); + ll.addfront(9); + ll.display(); + ll.search(2); + ll.search(4); + ll.reverse(); + ll.display(); + + } + } + + + +/* + +OUTPUT: + + LinkedList + +The Linkedlist is 5 8 7 1 2 +The Linkedlist is 9 5 8 7 1 2 +Target is found +Target Not Found +The Linkedlist is 2 1 7 8 5 9 + +*/ + +// Here initial linkedlist was 9 5 8 7 1 2 and after using reverse function the linkedlist become 2 1 7 8 5 9