-
Notifications
You must be signed in to change notification settings - Fork 65
/
CircularLinkedList.java
249 lines (228 loc) · 5.18 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/**
* Data-Structures-In-Java
* CircularLinkedList.java
*/
package com.deepak.data.structures.LinkedList;
/**
* Implementation of Circular Linked List
*
* <br> Operations supported are :
*
* 1. Inserting a element in the list - This can be at beginning, at end or at a given position.
* 2. Traversing through linked list.
* 3. Display a list.
* 4. Delete an item at position.
* 5. Delete an item at beginning.
* 6. Search by index.
* 7. Search by value.
* 8. Get the size of the list.
* 9. Check is list is empty.
*
* </br>
*
* @author Deepak
*/
public class CircularLinkedList<E> {
/* Head is needed to keep track of first node */
private Node<E> head;
/* Size to keep track of number of elements in list.
* This should be increased by 1 when a element is added
* and should be reduced by 1 when a element is deleted */
private int size = 0;
/**
* Inserts a element into a linked list at beginning.
*
* @param value
*/
public void insertAtBeginning(E value) {
Node<E> newNode = new Node<E>(value);
if (head == null) {
head = newNode;
head.next = head;
} else {
Node<E> temp = head;
newNode.next = temp;
head = newNode;
}
size++;
}
/**
* Inserts a element into a linked list at tail position.
*
* @param value
*/
public void insertAtTail(E value) {
Node<E> newNode = new Node<E>(value);
if (null == head) {
/* When list is empty */
head = newNode;
} else {
Node<E> temp = head;
while (temp.next != head) {
temp = temp.next;
}
temp.next = newNode;
}
newNode.next = head;
size++;
}
/**
* Inserts a element into a linked list at a given position.
*
* @param value
* @param position
*/
public void insertAtPosition(E value, int position) {
if (position < 0 || position > size) {
throw new IllegalArgumentException("Position is Invalid");
}
/* Conditions check passed, let's insert the node */
Node<E> newNode = new Node<E>(value);
Node<E> tempNode = head;
Node<E> prevNode = null;
for (int i = 0; i < position; i++) {
if (tempNode.next == head) {
break;
}
prevNode = tempNode;
tempNode = tempNode.next;
}
prevNode.next = newNode;
newNode.next = tempNode;
size++;
}
/**
* Method to delete an element from the
* beginning of the circular linked list
*/
public void deleteFromBeginning() {
Node<E> temp = head;
while (temp.next != head) {
temp = temp.next;
}
temp.next = head.next;
head = head.next;
size--;
}
/**
* Method to delete an item from the circular
* linked list at a given position
*
* @param position
*/
public void deleteFromPosition(int position) {
if (position < 0 || position >= size) {
throw new IllegalArgumentException("Position is Invalid");
}
Node<E> current = head, previous = head;
for (int i = 0; i < position; i++) {
if (current.next == head) {
break;
}
previous = current;
current = current.next;
}
if (position == 0) {
deleteFromBeginning();
} else {
previous.next = current.next;
}
size--;
}
/**
* Method to find a node on a given index
*
* @param index
* @return {@link Node<E>}
*/
public Node<E> searchByIndex(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index is Invalid");
}
Node<E> temp = head;
for (int i = 0; i < index; i++) {
temp = temp.next;
}
return temp;
}
/**
* Method to find a node for a given value
*
* @param index
* @return {@link Node<E>}
*/
public Node<E> searchByValue(E value) {
Node<E> temp = head;
while (null != temp && temp.item != value) {
temp = temp.next;
}
if (temp.item == value) {
return temp;
}
return null;
}
/**
* Method to check size of the circular linked list
*
* @return {@link int}
*/
public int size() {
return size;
}
/**
* Method to check if circular linked list is empty
*
* @return {@link boolean}
*/
public boolean isEmpty() {
return size == 0;
}
/**
* Method to display the circular linked list
*/
public void display() {
if (head == null) {
System.out.println("List is Empty !!");
} else {
Node<E> temp = head;
while (temp.next != head) {
System.out.println("Data at Node = " + temp.item);
temp = temp.next;
}
System.out.println("Data at Node = " + temp.item);
}
System.out.println();
}
/**
* Node class of a circular linked list
* This is needed since entire linked list is a collection
* of nodes connected to each other through links
*
* <br> We are keeping it generic so that it can be used with
* Integer, String or something else </br>
*
* <br> Each node contains a data item and pointer to next node.
* Since this is a Circular linked list and each node points in
* one direction, we maintain only pointer to one (next) node.
* Last node again points to the first node </br>
*
* @author Deepak
*
* @param <T>
*/
public class Node<T> {
/* Data item in the node */
T item;
/* Pointer to next node */
Node<T> next;
/* Constructor to create a node */
public Node(T item) {
this.item = item;
}
/* toString implementation to print just the data */
@Override
public String toString() {
return "Data Item = " + item;
}
}
}