1 package com.liu.Link;
2
3 class DoublyLinkedApp
4 {
5 public static void main(String[] args)
6 {
7 DoubleLink theList = new DoubleLink();
8
9 theList.insertFirst(22);
10 theList.insertFirst(44);
11 theList.insertFirst(66);
12
13 theList.inesrtLast(11);
14 theList.inesrtLast(33);
15 theList.inesrtLast(55);
16
17 theList.displayForward();
18 theList.displayBackward();
19
20 theList.deleteFirst();
21 theList.deleteLast();
22 theList.deleteKey(11);
23
24 theList.displayForward();
25
26 theList.insertAfter(22, 77);
27 theList.insertAfter(33, 88);
28
29 theList.displayForward();
30 }
31 }
32
33
34 public class DoubleLink {
35 private Link6 first;
36 private Link6 last;
37 public DoubleLink()
38 {
39 first = null;
40 last = null;
41 }
42 public boolean isEmpty()
43 {
44 return first == null;
45 }
46 public void insertFirst(long d)
47 {
48 Link6 newLink = new Link6(d);
49 if(isEmpty())
50 {
51 last = newLink;
52 }else
53 first.previous = newLink;
54 newLink.next = first;
55 first = newLink;
56 }
57 public void inesrtLast(long d)
58 {
59 Link6 newLink = new Link6(d);
60 if(isEmpty())
61 first = newLink;
62 else
63 {
64 last.next = newLink;;
65 newLink.previous = last;
66 }
67 last = newLink;
68 }
69
70 //插入到某个值的后面
71 public boolean insertAfter(long key,long d)
72 {
73 Link6 current = first;
74 while(current.dData != key)
75 {
76 current = current.next;
77 if(current == null)
78 {
79 return false;
80 }
81
82 }
83 Link6 newLink = new Link6(d);
84 if(current == last)
85 {
86 newLink.next = null;
87 last = newLink;
88 }else
89 {
90 newLink.next = current.next;
91 current.next.previous = newLink;
92 }
93 newLink.previous = current;
94 current.next = newLink;
95 return true;
96 }
97
98 public Link6 deleteFirst()
99 {
100 Link6 temp = first;
101 if(first.next == null)//这种情况是只存在一个数据的时候
102 last = null;
103 else
104 first.next.previous = null;
105 first = first.next;
106 return temp;
107 }
108
109 public Link6 deleteLast()
110 {
111 Link6 temp = last;
112 if(first.next == null)
113 first = null;
114 else
115 last.previous.next = null;
116 last = last.previous;
117 return temp;
118 }
119
120 public Link6 deleteKey(long key)
121 {
122 Link6 current = first;
123 while(current.dData != key)
124 {
125 current = current.next;
126 if(current == null)
127 return null;
128 }
129 if(current == first)
130 first = current.next;
131 else
132 current.previous.next = current.next;
133 if(current == last)
134 last = current.previous;
135 else
136 current.next.previous = current.previous;
137 return current;
138 }
139
140 public void displayForward()
141 {
142 System.out.print("List (first-->last): ");
143 Link6 current = first;
144 while(current != null)
145 {
146 current.displayLink();
147 current = current.next;
148 }
149 System.out.println("");
150 }
151
152 public void displayBackward()
153 {
154 System.out.print("List (last-->first): ");
155 Link6 current = last;
156 while(current != null)
157 {
158 current.displayLink();
159 current = current.previous;
160 }
161 System.out.println("");
162
163 }
164
165 }
166
167 class Link6
168 {
169 public long dData;
170 public Link6 next;
171 public Link6 previous;
172
173 public Link6(long d)
174 {
175 dData = d;
176 }
177
178 public void displayLink()
179 {
180 System.out.print(dData+" ");
181 }
182 }