-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGenericLinkedList.java
More file actions
200 lines (176 loc) · 3.53 KB
/
GenericLinkedList.java
File metadata and controls
200 lines (176 loc) · 3.53 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
package lecture9a21;
public class GenericLinkedList<T> {
private class Node {
T data;
Node next;
}
private Node head;
private Node tail;
private int size;
//o(1)
public T getFirst() throws Exception {
if (this.size == 0)
throw new Exception("LL is Empty");
return this.head.data;
}
//O(1)
public T getLast() throws Exception {
if (this.size == 0)
throw new Exception("LL is Empty");
return this.tail.data;
}
//O(n)
public T getAt(int idx) throws Exception {
if (this.size == 0)
throw new Exception("LL is Empty");
if (idx > this.size || idx < 0)
throw new Exception("Invalid Index");
Node temp = new Node();
temp = this.head;
for (int i = 0; i < idx; i++) {
temp = temp.next;
}
return temp.data;
}
//O(1)
public void addLast(T item) {
Node nn = new Node();
nn.data = item;
nn.next = null;
// attach
if (this.size > 0)
tail.next = nn;
if (this.size == 0) {
this.head = nn;
this.tail = nn;
this.size++;
} else {
this.tail = nn;
this.size++;
}
}
//O(n)
public void display() {
Node temp = head;
System.out.println("-------------------");
while (temp.next != null) {
System.out.println(temp.data);
temp = temp.next;
}
System.out.println(temp.data);
System.out.println(".");
System.out.println("---------------------");
}
//O(1)
public void addFirst(T item) {
Node nn = new Node();
nn.data = item;
nn.next = null;
// update
if (this.size == 0) {
this.head = nn;
this.tail = nn;
this.size++;
} else {
nn.next = head;
this.head = nn;
this.size++;
}
}
//O(n)
private Node getNodeAt(int idx) throws Exception {
if (this.size == 0)
throw new Exception("LL is Empty");
if (idx > this.size || idx < 0)
throw new Exception("Invalid Index");
Node temp = new Node();
temp = head;
for (int i = 0; i < idx; i++) {
temp = temp.next;
}
return temp;
}
//O(n)
public void addAt(int idx, T item) throws Exception {
if (idx > this.size || idx < 0)
throw new Exception("Invalid Index");
if (idx == 0)
addFirst(item);
else if (idx == this.size)
addLast(item);
else {
Node nn = new Node();
Node pn = getNodeAt(idx - 1);
nn.data = item;
nn.next = pn.next;
pn.next = nn;
this.size++;
}
}
//O(1)
public T removeFirst() throws Exception {
if (this.size == 0) {
throw new Exception("LL is Empty");
}
T rv = this.head.data;
if (this.size == 1) {
this.head = null;
this.tail = null;
this.size--;
} else {
this.head = head.next;
this.size--;
}
return rv;
}
// O(n)
public T removeLast() throws Exception {
if (this.size == 0) {
throw new Exception("LL is Empty");
}
T rv = this.tail.data;
if (this.size == 0) {
this.head = null;
this.tail = null;
this.size--;
} else {
Node pn = getNodeAt(this.size - 2);
this.tail = pn;
pn.next = null;
this.size--;
}
return rv;
}
//O(n)
public T removeAt(int idx) throws Exception {
if (idx > this.size || idx < 0)
throw new Exception("Invalid Index");
if (idx == 0)
return removeFirst();
else if (idx == this.size - 1)
return removeLast();
else {
Node pn = getNodeAt(idx - 1);
Node cn = pn.next;
Node nn = cn.next;
T rv = cn.data;
pn.next = nn;
this.size--;
return rv;
}
}
public int find(T data) {
int idx =0 ;
Node node = this.head ;
while(node!=null && !node.data.equals(data) ) {
idx++;
node = node.next;
}
if(node!=null && node.data.equals(data))
return idx;
return -1;
}
public boolean isEmpty() {
return this.size==0;
}
}