tonglin0325的个人主页

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
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
class DoublyLinkedList{				//双向链表
private Link_long first;
private Link_long last;

public DoublyLinkedList(){ //构造函数
this.first = null;
this.last = null;
}

public boolean isEmpty(){
return first==null;
}

public void insertFirst(long dd){ //从链表的头开始插入
Link_long newLink = new Link_long(dd);
if(isEmpty()){
last = newLink; //不用改变first
}else{
first.previous = newLink; //插入新的元素
}
newLink.next = first; //插入新的元素
first = newLink; //修改first的位置
}

public void insertLast(long dd){ //从链表的尾开始插入
Link_long newLink = new Link_long(dd);
if(isEmpty()){
first = newLink; //不用改变last
}else{
last.next = newLink; //在last后面添加新元素,并修改last的位置
newLink.previous = last;
}
last = newLink; //注意:只有一个元素的时候,插入要把last也赋为newLink
}

public Link_long deleteFirst(){ //从链表的头删除一个元素
Link_long temp = first; //暂存first
if(first.next == null){ //如果只有一个元素,把last也赋为null
last = null;
}else{
first.next.previous = null;
}
first = first.next; //把next设为first
return temp; //返回原来的first
}

public Link_long deleteLast(){ //从链表的头删除一个元素
Link_long temp = last; //暂存last
if(first.next == null){ //如果只有一个元素,把first也赋为null
first = null;
}else{
last.previous.next = null;
}
last = last.previous; //把previous设为last
return temp; //返回原来的last
}

public boolean insertAfter(long key,long dd){ //在特定元素后面插入
Link_long current = first;
while(current.dData != key){
current = current.next;
if(current == null){
return false;
}
}
Link_long newLink = new Link_long(dd);

if(current == last){
newLink.next = null;
last = newLink;
}else{
newLink.next = current.next;
current.next.previous = newLink;
}
newLink.previous = current;
current.next = newLink;
return true;
}

public Link_long deleteKey(long key){ //删除指定元素
Link_long current = first;
while(current.dData != key){
current = current.next;
if(current == null){
return null;
}
}
if(current == first){ //如果第一个元素匹配,则删除,并把first向后移动
first = current.next;
}else{
current.previous.next = current.next; //改变current上一个元素的next的指向
}
if(current == last){
last = current.previous; //如果最后一个元素匹配,则删除,并把previous向前移动
}else{
current.next.previous = current.previous; //改变current后一个元素的previous的指向
}
return current;
}

public void displayForward(){
System.out.println("List(first-->last):");
Link_long current = first; //用于不断改变位置实现遍历
while(current != null){
current.displayLink();
current = current.next;
}
}

public void displayBackward(){
System.out.println("List(last-->first):");
Link_long current = last; //用于不断改变位置实现遍历
while(current != null){
current.displayLink();
current = current.previous;
}
}

}

public class DoublyLinked_demo {

public static void main(String[] args) {
// TODO 自动生成的方法存根
DoublyLinkedList theList = new DoublyLinkedList();

theList.insertFirst(40);
theList.insertFirst(30);
theList.insertFirst(20);
theList.insertFirst(10);
theList.displayForward();
theList.displayBackward();

theList.insertAfter(20, 25);
theList.displayForward();
theList.displayBackward();

theList.deleteFirst();
theList.displayForward();

theList.deleteKey(20);
theList.displayForward();
}

}