-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericSinglyLinkedList.Java
More file actions
200 lines (176 loc) · 4.68 KB
/
GenericSinglyLinkedList.Java
File metadata and controls
200 lines (176 loc) · 4.68 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
public class GenericSinglyLinkedList<E extends Comparable<E>> implements Iterable<E> {
Node<E> head = new Node<E>();
public Iterator<E> iterator() {}
void insert (int index, E value) {
if (index > length()) throw new ArrayIndexOutOfBoundsException(index);
else {
var node = this.head;
for (var i = 0; i < index; i++) {
node = node.next;
}
node.next = new Node<E>(value, node.next);
}
}
void delete(int index) {
if (index >= length()) throw new ArrayIndexOutOfBoundsException(index);
else {
var node = this.head;
for (var i = 0; i < index; i++) {
node = node.next;
}
node.next = node.next.next;
}
}
E get(int index) {
if (index >= length()) throw new ArrayIndexOutOfBoundsException(index);
else {
var node = this.head;
for (var i = 0; i < index; i++) {
node = node.next;
}
return node.next.value; //
}
}
void set(int index, E value) {
if (index >= length()) throw new ArrayIndexOutOfBoundsException(index);
else {
delete(index);
insert(index, value);
}
}
int find(E value) {
var node = this.head;
int a = -1;
int index = 0;
for (var i = 0; i < length(); i++) {
if (node.next.value == value) {
a = index;
break;
}
node = node.next;
index++;
}
return a; //
}
E front() {
if (this.head.next ==null) throw new ArrayIndexOutOfBoundsException();
return this.head.next.value;
}
E back () {
var node = this.head;
for (var i = 0; i < length(); i++) {
node = node.next;
}
return node.value;
}
void pushFront(E value) {
this.head.next = new Node<E>(value, this.head.next);
}
void pushBack(E value) {
var node = this.head;
while (node.next != null) {
node = node.next;
}
node.next = new Node<E>(value);
}
void popFront() {
if (this.head.next ==null) throw new ArrayIndexOutOfBoundsException();
this.head.next.next = this.head.next;
}
void popBack() {
var prev = this.head;
var node = prev.next;
while (node != null) {
prev = node;
node = node.next;
}
prev.next = null;
}
boolean empty() {
return (this.head.next == null);
}
int length() {
int len = 0;
var node = this.head.next;
while (node != null) {
len = len+1;
node = node.next;
}
return len;
}
void clear() {
head.next = null;
}
// void sort() {
// Node node = this.head;
// for (var i = length()-1; i > 0; i--) {
// for (var j = 0; j < i; j++) {
// if (node.next.value > node.next.next.value) {
// var t = node.next.value;
// node.next.value = node.next.next.value;
// node.next.next.value = t;
// }
// node = node.next;
// }
// }
// }
// void reverse() {
// Node node = this.head.next;
// this.head.next = null;
// while(node != null) {
// var t = node.next;
// node.next = this.head.next;
// this.head.next = node;
// node = t;
// }
// }
void print() {
var node = this.head.next;
while (node != null) {
System.out.printf("%3d" ,node.value);
node = node.next;
}
System.out.println();
}
public static void main(String[] args) {
var l = new SinglyLinkedList2<Integer>();
// var head = new Node();
// head.next = new Node(1);
// head.next.next = new Node(2);
// head.next.next.next = new Node(3);
l.insert(0,4);
l.insert(1,5);
l.insert(2,6);
l.insert(3,3);
// l.delete(2);
// System.out.printf("%3d\n", l.get(2));
// l.set(2,7);
// System.out.printf("%3d\n", l.find(2));
// System.out.printf("%3d\n", l.find(6));
// System.out.printf("%3d\n", l.front());
// System.out.printf("%3d\n", l.back());
// l.pushFront(7);
// l.pushBack(8);
// l.popFront();
// l.popBack();
l.print();
System.out.printf("%3d\n", l.length());
// var node = head.next;
// while (node != null) {
// System.out.printf("%3d", node.value);
// node = node.next;
// }
}
}
class Node<E> {
E value = null;
Node<E> next = null;
Node() {}
Node(E value) {
this.value = value;
}
Node(E value, Node<E> next) {
this.value = value;
this.next = next;
}
}