-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularLinkedList.java
More file actions
205 lines (162 loc) · 4.58 KB
/
CircularLinkedList.java
File metadata and controls
205 lines (162 loc) · 4.58 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
class CllNode{
private int data;
private CllNode next;
public CllNode(int data){
this.data = data;
next = null;
}
public int getData(){
return data;
}
public void setData(int data){
this.data = data;
}
public CllNode getNext(){
return next;
}
public void setNext(CllNode node){
this.next = node;
}
}
public class CircularLinkedList{
protected CllNode tail;
protected int length;
public CircularLinkedList(){
tail = null;
length = 0;
}
//add element to the head of the circular list
public void addToHead(int data){
CllNode temp = new CllNode(data);
if(tail == null){
tail = temp;
tail.setNext(tail);
}else{
temp.setNext(tail.getNext());
tail.setNext(temp);
}
length++;
}
//add element to the tail of the list
public void addToTail(int data){
addToHead(data);
tail = tail.getNext();
}
//add element
public void add(int data){
addToHead(data);
}
//peek element at the tail
public int peek(){
return tail.getData();
}
//remove the element from the head
public int removeFromHead(){
CllNode temp = tail.getNext();
if(tail == tail.getNext()){
tail = null;
}else{
tail.setNext(temp.getNext());
temp.setNext(null);
}
length--;
return temp.getData();
}
//remove the element from the tail
public int removeFromTail(){
if(isEmpty()){
return Integer.MIN_VALUE;
}
CllNode temp = tail.getNext();
while(temp.getNext() != tail){
temp = temp.getNext();
}
CllNode tailNode = tail;
if(temp == tail){
tail = null;
}else{
temp.setNext(tailNode.getNext());
tail = temp;
}
length--;
return tailNode.getData();
}
//a boolean to check if list contains the data
public boolean contains(int data){
if(tail == null){
return false;
}
CllNode temp = tail.getNext();
while(temp != tail && (temp.getData() != data)){
temp = temp.getNext();
}
return temp.getData() == data;
}
// to remove the element from the list of specified data
public int remove(int data){
if(tail == null) return Integer.MIN_VALUE;
CllNode temp = tail.getNext();
CllNode previous = tail;
int element;
for(element = 0 ; element < length && (!(temp.getData() == data)) ; element++){
previous = previous.getNext();
temp = temp.getNext();
}
if(temp.getData() == data){
if(tail == tail.getNext()){
tail = null;
}
else{
if(temp == tail){
tail = previous;
previous.setNext(previous.getNext().getNext());
}
}
temp.setNext(null);
length--;
return temp.getData();
}
else return Integer.MAX_VALUE;
}
public int size(){
return length;
}
public int length(){
return length;
}
public boolean isEmpty(){
return tail == null;
}
public String toString(){
if(tail == null){
return "[]";
}
String result = "[";
CllNode temp = tail.getNext();
for(int i = 0 ; i < length ; i++){
result += temp.getData();
if(i < length - 1 ){
result += " , ";
}
temp = temp.getNext();
}
return result;
}
public static void main(String[] args){
CircularLinkedList cll = new CircularLinkedList();
cll.addToHead(10);
cll.addToHead(20);
System.out.println(cll.toString());
cll.addToTail(30);
System.out.println(cll.toString());
cll.removeFromHead();
System.out.println(cll.toString());
cll.removeFromTail();
System.out.println(cll.toString());
cll.addToHead(20);
cll.addToHead(20);
System.out.println(cll.toString());
cll.remove(10);
System.out.println(cll.toString());
}
}