Skip to content

Commit c466aad

Browse files
java code of Circular Linked List
1 parent 772fb91 commit c466aad

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

main.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
public class Main {
2+
public static void main(String[] args) {
3+
Main Obj = new Main();
4+
Obj.add(11);
5+
Obj.add(22);
6+
Obj.add(33);
7+
Obj.add(44);
8+
Obj.add(55);
9+
Obj.print();
10+
}
11+
public class Node{
12+
int element;
13+
Node next;
14+
public Node(int element) {
15+
this.element = element;
16+
}
17+
}
18+
19+
public Node head = null;
20+
public Node tail = null;
21+
22+
public void add(int element){
23+
Node newEle = new Node(element);
24+
if(head == null) {
25+
head = newEle;
26+
tail = newEle;
27+
newEle.next = head;
28+
}
29+
else {
30+
tail.next = newEle;
31+
tail = newEle;
32+
tail.next = head;
33+
}
34+
}
35+
36+
public void print() { //print function
37+
Node current = head;
38+
if(head == null) {
39+
System.out.println("List is empty");
40+
}
41+
else {
42+
System.out.println("Nodes of the circular linked list: ");
43+
do{
44+
System.out.print(" "+ current.element);
45+
current = current.next;
46+
}while(current != head);
47+
System.out.println();
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)