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>
pull/1029/merge
Ritesh Yadav 2022-10-16 12:55:30 +05:30 committed by GitHub
parent 5e09de59e5
commit 70e71a7718
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 121 additions and 0 deletions

View File

@ -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)

View File

@ -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