-
Notifications
You must be signed in to change notification settings - Fork 0
/
CLL.java
178 lines (163 loc) · 4.26 KB
/
CLL.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
public class CLL {
Node head;
public class Node {
int data;
Node next;
Node(int d) {
data = d;
}
}
void Push(int data) {
Node new_Node = new Node(data);
if (head == null) {
head = new_Node;
head.next = head;
}
Node temp = head;
while (temp.next != head) {
temp = temp.next;
}
new_Node.next = head;
head = new_Node;
temp.next = head;
}
void PushAtIndex(int num, int data) {
Node temp = head;
Node new_Node = new Node(data);
if (head == null) {
head = new_Node;
new_Node.next = head;
return;
}
for (int i = 1; i < num - 1; i++) {
temp = temp.next;
}
new_Node.next = temp.next;
temp.next = new_Node;
}
void PushAtEnd(int data) {
Node new_Node = new Node(data);
if (head == null) {
head = new_Node;
new_Node.next = head;
return;
}
Node temp = head;
while (temp.next != head) {
temp = temp.next;
}
new_Node.next = head;
temp.next = new_Node;
}
void AfterNode(Node previousNode, int data) {
if (previousNode == null) {
System.out.println("Previous Node Can't be NULL");
return;
}
Node new_Node = new Node(data);
new_Node.next = previousNode.next;
previousNode.next = new_Node;
}
void Removehead() {
if (head == null) {
return;
}
if (head.next == head) {
// System.out.println("CLL s Head");
head = null;
return;
}
Node temp = head;
while (temp.next != head) {
temp = temp.next;
}
head = head.next;
temp.next = head;
if (head.next == head) {
head.next = null;
}
}
void RemoveNode(int key) {
if (head.data == key) {
Removehead();
return;
}
Node temp = head, prev = null;
while (temp.next != head && temp.data != key) {
prev = temp;
temp = temp.next;
}
if (temp == head) {
System.out.println("Key Doesn't Exist In CLL");
return;
}
prev.next = temp.next;
}
void RemoveIndex(int index) {
Node temp = head, prev = null;
if (index == 1) {
Removehead();
return;
}
for (int i = 1; i < index; i++) {
prev = temp;
temp = temp.next;
}
prev.next = temp.next;
}
int Listlength(Node node) {
int count = 0;
if (node == null) {
return count;
}
Node temp = head;
while (temp.next != head) {
count++;
temp = temp.next;
}
return count + 1;
}
int ListlengthRecursive(Node node) {
if (node == null) {
return 0;
}
if (node.next == head) {
return 1;
}
return 1 + ListlengthRecursive(node.next);
}
void PrintCLL() {
if (head == null) {
System.out.println("CLL is Empty");
return;
}
Node temp = head;
do {
System.out.print(temp.data + " ");
temp = temp.next;
} while (temp != head);
System.out.println();
}
public static void main(String[] args) {
CLL new_Cll = new CLL();
// new_Cll.Push(6);
// new_Cll.Push(5);
// new_Cll.Push(4);
// new_Cll.Push(3);
// new_Cll.Push(2);
// new_Cll.Push(1);
// new_Cll.PushAtIndex(10, 0);
// new_Cll.PushAtEnd(7);
// new_Cll.PushAtEnd(8);
// new_Cll.AfterNode(new_Cll.head.next, 0);
// new_Cll.AfterNode(new_Cll.head.next.next.next, 0);
// new_Cll.Removehead();
// new_Cll.RemoveNode(6);
// new_Cll.PrintCLL();
// new_Cll.RemoveIndex(6);
// new_Cll.RemoveIndex(2);
// System.out.println(new_Cll.head.next.data);
new_Cll.PrintCLL();
System.out.println(new_Cll.ListlengthRecursive(new_Cll.head));
}
}