Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Circular Linked List Hacktoberfest By Rishin Pandit #333

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions linked-list/CircularLinkedListByRishinPandit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
public class CircularLinkedListByRishinPandit{
public class Node{
int data;
Node next;
public Node(int data){
this.data = data;
}
}

public Node head = null;
public Node tail = null;

public void add(int data){
Node newNode = new Node(data);

if(head == null){
head = newNode;
tail = newNode;
newNode.next = head;
}
else {
tail.next = newNode;
tail = newNode;
tail.next = head;

}
}

public void display(){
Node current = head;
if(head == null){
System.out.println("List is empty");
}
else{
System.out.println("Nodes of the circular linked list: ");
do{
System.out.print(" "+ current.data);
current = current.next;
}while(current != head);
System.out.println();
}
}

public static void main(String[] args) {
CircularLinkedListByRishinPandit c1 = new CircularLinkedListByRishinPandit();
c1.add(10);
c1.add(20);
c1.add(30);
c1.add(40);

c1.display();
}
}