-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoubleLinkedListDemo.java
More file actions
175 lines (148 loc) · 4.74 KB
/
DoubleLinkedListDemo.java
File metadata and controls
175 lines (148 loc) · 4.74 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
package com.yijie.linkedlist;
public class DoubleLinkedListDemo {
public static void main(String[] args) {
//test
System.out.println("The test of DoubleLinkedList: ");
HeroNode2 hero1 = new HeroNode2(1, "superman", "sup");
HeroNode2 hero2 = new HeroNode2(2, "batman", "bat");
HeroNode2 hero3 = new HeroNode2(3, "antman", "ant");
HeroNode2 hero4 = new HeroNode2(4, "ironman", "iron");
//add to Linkedlist
DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
doubleLinkedList.add(hero1);
doubleLinkedList.add(hero2);
doubleLinkedList.add(hero3);
doubleLinkedList.add(hero4);
doubleLinkedList.list();
System.out.println();
//test the update
HeroNode2 hero5 = new HeroNode2(4, "dogman", "dog");
doubleLinkedList.update(hero5);
doubleLinkedList.list();
System.out.println();
//test the delete
doubleLinkedList.del(3);
doubleLinkedList.list();
}
}
//create a DoubleLinkedList class
class DoubleLinkedList {
//initialize the head node
private HeroNode2 head = new HeroNode2(0, "", "");
//return the head node
private HeroNode2 getHead() {
return head;
}
//traverse the double linked list
public void list() {
//determine if null
if (head.next == null) {
System.out.println("linkedlist is empty!");
return;
}
//build an auxiliary variable
HeroNode2 temp = head.next;
while (true) {
if (temp == null) {
break;
}
//output the info of the node
System.out.println(temp);
temp = temp.next;
}
}
//add Node to the end of the double linked list
//1. find the last node of the linked list
//2. let the "next" of the last node point to the new node
public void add(HeroNode2 heroNode) {
HeroNode2 temp = head;
//traverse to find the last node
while (true) {
//if the last node
if (temp.next == null) {
break;
}
//if node the last node
temp = temp.next;
}
temp.next = heroNode;
heroNode.pre = temp;
}
//update the data of the node, keep the no of the node unchanged
public void update(HeroNode2 newHeroNode) {
//determine if empty
if (head.next == null) {
System.out.println("the Linkedlist is null!");
return;
}
HeroNode2 temp = head.next;
boolean flag = false;//indicates whether the node is found
while (true) {
if (temp == null) {
//at the rear of the linked list
break;
}
if (temp.no == newHeroNode.no) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
temp.name = newHeroNode.name;
temp.nickname = newHeroNode.nickname;
} else {
System.out.printf("no node found, node %d can't be updated!", newHeroNode.no);
}
}
//delete the node
//1. for double linked list we can directly find the node
//2. delete the node
public void del(int no) {
//determine if empty
if (head.next == null) {
System.out.println("linked list is empty, the node can't be deleted");
return;
}
HeroNode2 temp = head.next;
boolean flag = false;//indicated whether the node is found
while (true) {
if (temp == null) {//rear of the linked list
break;
}
if (temp.no == no) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
temp.pre.next = temp.next;
//RISK!!!IF THE TEMP MOVES TO THE END OF THE LINKED LIST
if (temp.next != null) {
temp.next.pre = temp.pre;
}
} else {
System.out.printf("the node %no doesn't exist!", no);
}
}
}
//define a HeroNode2, every HeroNode is a node
class HeroNode2 {
public int no;
public String name;
public String nickname;
public HeroNode2 next;//pointing to the next node
public HeroNode2 pre;//pointing to the previous node
//constructor
public HeroNode2(int hNo, String hName, String hNickname) {
this.no = hNo;
this.name = hName;
this.nickname = hNickname;
}
//for display purpose, redefine the toString method
@Override
public String toString() {
return "HeroNode2 [no= " + no + ", name= " + name + ", nickname= " + nickname + "]";
}
}