Repository navigation
Expand file tree
/
Copy pathCircularLinkedList.java
More file actions
169 lines (154 loc) · 4.69 KB
/
Copy pathCircularLinkedList.java
File metadata and controls
169 lines (154 loc) · 4.69 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
/**
* CircularLinkedList
* This class is used to create a circular linked list and contains all the methods to perform operations on the circular linked list.
*/
public class CircularLinkedList {
/** Data Members */
Node first; // first node in the circular linked list
int size; // number of elements in the circular linked list
/**
* Constructor
* Creates a circular linked list that is empty
*/
public CircularLinkedList() {
first = new Node();
size = 0;
}
/**
* Method isEmpty
* @return true if the circular linked list is empty
* false if the circular linked list is not empty
*/
public boolean isEmpty() {
return size == 0;
}
/**
* Method getSize
* @return the number of elements in the circular linked list
*/
public int getSize() {
return size;
}
/**
* Method checkIndex
* Checks if the index is valid
* @throws IndexOutOfBoundsException when index is not between 0 and size - 1
*/
public void checkIndex(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
}
/**
* Method get
* @param index the index of the element to be returned
* @return the element at the given index
*/
public Object get(int index) {
checkIndex(index);
Node current = first;
for(int i = 0; i < index; i++)
current = current.next;
return current.data;
}
/**
* Method indexOf
* @param data the data to be searched
* @return the index of the data in the linked list
* -1 if the data is not found
*/
public int indexOf(Object data) {
Node current = first;
for(int i = 0; i < size; i++) {
if(current.data.equals(data))
return i;
current = current.next;
}
return -1;
}
/**
* Method remove
* @param index the index of the element to be removed
* @return the element that is removed
* @throws IndexOutOfBoundsException when index is not between 0 and size - 1
*/
public Object remove(int index) {
checkIndex(index);
Node current = first;
for(int i = 0; i < index; i++)
current = current.next;
Object removedElement = current.next.data;
current.next = current.next.next;
size--;
return removedElement;
}
/**
* Method insert
* @param index the index at which the data is to be inserted
* @param data the data to be inserted
* @throws IndexOutOfBoundsException when index is not between 0 and size
*/
public void insert(Object data, int index) {
if(index < 0 || index > size) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
Node newNode = new Node(data);
if(index == 0) {
if(size == 0) {
first = newNode;
first.next = first;
} else {
newNode.next = first;
Node last = first;
for(int i = 0; i < size-1; i++) {
last = last.next;
}
last.next = newNode;
first = newNode;
}
} else {
Node current = first;
for(int i = 0; i < index-1; i++) {
current = current.next;
}
newNode.next = current.next;
current.next = newNode;
}
size++;
}
/**
* Method display
* Displays the elements in the circular linked list
*/
public void display() {
Node current = first;
System.out.print("[");
for(int i = 0; i < size-1; i++) {
System.out.print(current.data + ", ");
current = current.next;
}
System.out.print(current.data +"]");
}
/**
* Method Main
* The main method is used to test the CircularLinkedList class
*/
public static void main(String[] args) {
CircularLinkedList list = new CircularLinkedList();
list.insert(1, 0);
list.insert(2, 1);
list.insert(3, 2);
list.insert(4, 3);
list.insert(5, 4);
list.display(); // [1, 2, 3, 4, 5]
System.out.println();
list.remove(2); // remove 3
list.display(); // [1, 2, 4, 5]
System.out.println();
list.insert(6, 2); // insert 6 at index 2
list.display(); // [1, 2, 6, 4, 5]
System.out.println();
System.out.println(list.indexOf(6)); // 2
System.out.println(list.get(2)); // 6
}
}