-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoubleLink.java
More file actions
149 lines (149 loc) · 2.76 KB
/
DoubleLink.java
File metadata and controls
149 lines (149 loc) · 2.76 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
public class DoubleLink<Item> {
private Node head=null;
private Node tail=null;
private int N=0;
private class Node{
Node prior;
Node next;
Item item;
}
/*
* 在后面加入数据
*/
public void addTailData(Item data){
Node newNode=new Node();
newNode.item=data;
if(N==0){
head=newNode;
tail=newNode;
}
else{
tail.next=newNode;
newNode.prior=tail;
tail=newNode;
}
N++;
}
/*
* 在后面加入数据
*/
public void addHeadData(Item data){
Node newNode=new Node();
newNode.item=data;
if(N==0){
head=newNode;
tail=newNode;
}
else{
newNode.next=head;
head.prior=newNode;
head=newNode;
}
N++;
}
/*
* 删除第i个位置的元素(i从0开始算的)并返还该元素
*/
public Item deleteData(int i){
Node reData;//要返回的节点
if(i<0||i>N-1){
System.out.println("delete fail!");
return null;
}
else if(i==0){
head=null;
tail=null;
return null;
}
else{
Node p=head;
int j=0;
while(j!=i-1){
p=p.next;
j++;
}
reData=p.next;
p.next=p.next.next;
p.next.prior=p;
}
N--;
return reData.item;
}
/*
* 在第i个节点后面插入data
*/
public void innersetData(int i,Item data){
if(i>=0&&i<N-1){
Node newNode=new Node();
newNode.item=data;
//System.out.println("进来啦");
Node p=head;
int j=0;
while(j!=i){
p=p.next;
j++;
}
newNode.next=p.next;
p.next.prior=newNode;
p.next=newNode;
newNode.prior=p;
N++;
}
else if(i==N-1){
addTailData(data);
}
else{
System.out.println("插入为位置不对!");
}
}
/*
* 返回链表长度N
*/
public int size(){
return N;
}
/*
* 从前向后遍历
*/
public void printHeadToTail(){
Node p=head;
System.out.print("head-->tail: ");
while(p!=null){
System.out.printf("%s->>",p.item);
p=p.next;
}
System.out.println();
}
/*
* 从后向遍历
*/
public void printTailToHead(){
Node p=tail;
System.out.print("tail-->head: ");
while(p!=null){
System.out.printf("%s->>",p.item);
p=p.prior;
}
System.out.println();
}
public static void main(String[] args){
DoubleLink<Integer> doubleLink=new DoubleLink<Integer>();
doubleLink.addHeadData(1);
doubleLink.addHeadData(2);
doubleLink.addHeadData(3);
doubleLink.addTailData(4);
doubleLink.addTailData(5);
doubleLink.addTailData(6);
doubleLink.addTailData(7);
doubleLink.addTailData(8);
doubleLink.addTailData(9);
int linkSize=doubleLink.size();
System.out.println("链表大小 "+linkSize);
int deleteIndex=2;
int data=doubleLink.deleteData(deleteIndex);
System.out.println("删除的下标是 "+deleteIndex+" 删除的数据是 "+data);
doubleLink.innersetData(7,10);
doubleLink.printHeadToTail();
doubleLink.printTailToHead();
}
}