-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDoublyLinked.java
More file actions
56 lines (49 loc) · 1.21 KB
/
DoublyLinked.java
File metadata and controls
56 lines (49 loc) · 1.21 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
class DoublyLinked {
static class Node {
int data;
Node next;
Node prev;
Node(int data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
private Node head;
public DoublyLinked() {
this.head = null;
}
public void append(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
return;
}
Node last = head;
while (last.next != null) {
last = last.next;
}
last.next = newNode;
newNode.prev = last;
}
public void printList() {
Node current = head;
while (current != null) {
System.out.print(current.data + " <-> ");
current = current.next;
}
System.out.println("NULL");
}
public static void main(String[] args) {
DoublyLinked list = new DoublyLinked();
list.append(1);
list.append(2);
list.append(3);
list.printList(); // 输出:1 <-> 2 <-> 3 <-> NULL
}
}
/*
jarry@MacBook-Pro linked % javac DoublyLinked.java
jarry@MacBook-Pro linked % java DoublyLinked
1 <-> 2 <-> 3 <-> NULL
*/