-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircularLinkedList.java
122 lines (116 loc) · 2.39 KB
/
CircularLinkedList.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package list;
public class CircularLinkedList {
static class Node{
int data;
Node next;
public Node(int data) {
this.data=data;
}
}
public Node head=null;
public Node tail=null;
public void insertAtBeg(int data) {
Node toAdd=new Node(data);
if(head==null) {
head=toAdd;
tail=toAdd;
toAdd.next=head;
}
else {
Node curr=head;
toAdd.next=curr;
head=toAdd;
tail.next=head;
}
}
public void insertAtEnd(int data) {
Node toAdd=new Node(data);
if(head==null) {
head=toAdd;
tail=toAdd;
toAdd.next=head;
}
else {
tail.next=toAdd;
tail=toAdd;
tail.next=head;
}
}
public void RemoveLast() {
Node curr=head;
if(curr==null)
System.out.println("cannot remove from empty list");
if(curr.next==null) {
head=null;
tail=null;
}
while(curr.next!=tail) {
curr=curr.next;
}
tail=curr;
tail.next=head;
}
public void RemoveBeg() {
if(head==null)
System.out.println("cannot remove from empty list");
if(head.next==null) {
head=null;
tail=null;
}
head=head.next;
tail.next=head;
}
static int isCircular(Node head)
{
Node curr=head;
if(head==null)
return 1;
while(curr.next!=head){
curr=curr.next;
}
if(curr==head)
return 1;
return 0;
}
public void display() {
Node curr=head;
if(head==null)
System.out.println("List is empty");
else {
do {
System.out.println(curr.data);
curr=curr.next;
}while(curr!=head);
}}
public static void main(String args[]) {
CircularLinkedList ll=new CircularLinkedList();
System.out.println("insertion at beg");
for(int i=0;i<10;i++) {
ll.insertAtBeg(i);
}
ll.display();
ll.RemoveLast();
System.out.println("remove last");
ll.display();
ll.RemoveBeg();
System.out.println("remove beg");
ll.display();
Node n1 = new Node(34);
Node n2 = new Node(25);
Node n3 = new Node(31);
Node n4 = new Node(56);
Node n5 = new Node(45);
n1.next = n2;
n2.next = n3;
n3.next = n4;
n4.next = n5;
n5.next = n1;
int x = isCircular(n1);
System.out.println(x);
/*System.out.println("insertion at end");
for(int i=0;i<10;i++) {
ll.insertAtEnd(i);
}
ll.display(); */
}
}