-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyLinkedList.java
53 lines (45 loc) · 909 Bytes
/
MyLinkedList.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
package myLinkedList;
public class MyLinkedList {
private MyNode head;
private MyNode tail;
public MyLinkedList () {
this.head = null;
this.tail = null;
}
public boolean empty() {
return this.head == null;
}
public MyNode getHead() {
return this.head;
}
public MyNode getTail() {
return this.tail;
}
public void addNode(MyNode node) {
if (this.empty())
this.head = this.tail = node;
else {
this.tail.prev = node;
node.next = this.tail;
this.tail = node;
}
}
public void addNode(int v) {
this.addNode(new MyNode(v));
}
public void printList() {
MyNode n = this.tail;
while (n != null) {
n.printNode();
if (n.getNext() != null)
System.out.print(" -> ");
n = n.getNext();
}
}
public static void main(String [] args) {
MyLinkedList l = new MyLinkedList();
for (int i = 0; i < 10; i++ )
l.addNode(i);
l.printList();
}
}