-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircularlyLinkedList.java
148 lines (131 loc) · 3.46 KB
/
CircularlyLinkedList.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
class CircularLikedList<E> {
// create a node
private static class Node<E> {
private E element;
private Node<E> next;
public Node(E e, Node<E> n) {
element = e;
next = n;
}
public E getElement() {
return element;
}
public Node<E> getNext() {
return next;
}
public void setNext(Node<E> n) {
next = n;
}
}
private Node<E> tail = null;
private int size = 0;
public CircularLikedList() {
}
// access methods
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public E getFirst() {
if (isEmpty()) {
return null;
}
return tail.getNext().getElement();
}
public E getLast() {
if (isEmpty()) {
return null;
}
return tail.getElement();
}
// update methods
public void rotate() {
if (tail != null) {
tail = tail.getNext();
}
}
public void addFirst(E e) {
if (size == 0) {
tail = new Node<>(e, null);
tail.setNext(tail);
} else {
Node<E> newest = new Node<>(e, tail.getNext());
tail.setNext(newest);
}
size++;
}
public void addLast(E e) {
addFirst(e);
tail = tail.getNext();
}
public E removeFirst() {
if (isEmpty()) {
return null;
}
Node<E> head = tail.getNext();
if (head == tail) {
tail = null;
} else {
tail.setNext(head.getNext());
}
size--;
return head.getElement();
}
public E removeLast() {
if (isEmpty()) {
System.out.println("Empty!");
return null;
}
Node<E> head = tail.getNext();
if (head == tail) {
E removedElement = tail.getElement();
tail = null;
size--;
return removedElement;
} else {
Node<E> current = head;
// Traverse until the second last node
while (current.getNext() != tail) {
current = current.getNext();
}
E removedElement = tail.getElement();
current.setNext(head);
tail = current;
size--;
return removedElement;
}
}
public void display() {
if (tail == null) {
System.out.print("Circular Linked List is empty!");
return;
}
Node<E> temp = tail;
do {
System.out.print(temp.element + " ");
temp = temp.next;
} while (temp != tail);
System.out.println();
}
}
// main class
public class CircularlyLinkedList<E> {
public static void main(String[] args) {
CircularLikedList<Integer> cll = new CircularLikedList<>();
cll.addFirst(4);
cll.addFirst(3);
cll.addFirst(2);
cll.addFirst(1);
cll.addLast(5);
System.out.println("First Element: " + cll.getFirst());
System.out.println("Last Element: " + cll.getLast());
System.out.println("Total Element: " + cll.size());
cll.display();
System.out.println("remove Last Element: " + cll.removeLast());
System.out.println("After remove first element.");
System.out.println("Total Element: " + cll.size());
cll.display();
}
}