Set two reference domains in the node, one for storing the address of the direct precursor node, namely Prev, and the other for directly following the node, namely next. Such a linked list is a two-way linked list (doubly linked list ).
The node structure of the two-way linked list is as above. The definition of the two-way linked list node is similar to that of the single-chain table node. Therefore, the implementation of the two-way linked list node class can refer to the node class of the single-chain table.
C # implementation:
1 Interface
Reference the linear Table Interface ilistds <t>
2. Implementation
(1) Two-way linked list node class, refer to the node class of a single-chain table
Code
[Copy to clipboard]
Code:
1 public class dbnode <t>
2 {
3 private t data; // data domain
4 private dbnode <t> next; // successor
5 private dbnode <t> Prev; // Precursor
6 Public t data
7 {
8 get {return data ;}
9 set {DATA = value ;}
10}
11 Public dbnode <t> next
12 {
13 get {return next ;}
14 set {next = value ;}
15}
16 public dbnode <t> Prev
17 {
18 get {return Prev ;}
19 set {Prev = value ;}
20}
21 public dbnode ()
22 {
23 DATA = default (t );
24 Prev = NULL;
25 next = NULL;
26}
27 public dbnode (T Val)
28 {
29 Data = val;
30 next = NULL;
31 Prev = NULL;
32}
33}
(2) Two-way linked list Operation Class implementation
Note: Because the nodes in the two-way linked list have two references, it is more complicated to insert and delete nodes in the two-way linked list. In a two-way linked list, node insertion is divided into insertion before and after nodes. The insertion operation requires four references.
Similarly , Delete operations also need to pay more attention, other operations are similar to a single-chain table, do not go into details.
Code
[Copy to clipboard]
Code:
1 public class dblinklist <t>: ilistds <t>
2 {
3 private dbnode <t> header;
4 Public dbnode <t> Header
5 {
6 get
7 {
8 return header;
9}
10 set
11 {
12 header = value;
13}
14}
15 public dblinklist ()
16 {
17 header = new dbnode <t> ();
18 header. Next = NULL;
19 header. Prev = NULL;
20}
21/** // <summary>
22 // get the length
23 /// </Summary>
24 /// <returns> </returns>
25 public int getlength ()
26 {
27 dbnode <t> P = header;
28 int Len = 0;
29 While (P! = NULL)
30 {
31 + + Len;
32 P = P. Next;
33}
34 return Len;
35}
36/*** // <summary>
37 // clear operation
38 /// </Summary>
39 public void clear ()
40 {
41 header = NULL;
42}
43/*** // <summary>
44 // determine whether the linear table is empty
45 /// </Summary>
46 // <returns> </returns>
47 Public bool isempty ()
48 {
49 If (header. Data = NULL)
50 {
51 return true;
52}
53 else
54 {
55 return false;
56}
57}
58/*** // <summary>
59 // find a node
60 /// </Summary>
61 // <Param name = "I"> </param>
62 // <returns> </returns>
63 private dbnode <t> findnode (int I)
64 {
65 if (isempty ())
66 {
67 console. Write ("list is empty ");
68 return NULL;
69}
70 if (I <1)
71 {
72 console. Write ("index is error ");
73 return NULL;
74}
75 dbnode <t> current = new dbnode <t> ();
76 current = header;
77 Int J = 1;
78 while (current. Next! = NULL & J <I)
79 {
80 + + J;
81 current = current. Next;
82}
83 return current;
84}
85/*** // <summary>
86 // insert operation, before node I
87 /// </Summary>
88 // <Param name = "item"> </param>
89 // <Param name = "I"> </param>
90 public void insert (T item, int I)
91 {
92 dbnode <t> current = findnode (I );
93 dbnode <t> newnode = new dbnode <t> (item );
94 If (current! = NULL)
95 {
96 current. Prev. Next = newnode;
97 newnode. Next = current;
98 newnode. Prev = current. Prev;
99 current. Prev = newnode;
100}
101 else
102 {
103 console. Write ("the node is not exist! ");
104}
105}
106/*** // <summary>
107 // insert operation, insert item after node I
108 /// </Summary>
109 /// <Param name = "item"> </param>
110 /// <Param name = "I"> </param>
111 public void insertback( t item, int I)
112 {
113 dbnode <t> current = findnode (I );
114 dbnode <t> newnode = new dbnode <t> (item );
115 If (current! = NULL)
116 {
117 current. Next. Prev = newnode;
118 newnode. Prev = current;
119 newnode. Next = current. Next;
120 current. Next = newnode;
121}
122 else
123 {
124 console. Write ("the node is not exist! ");
125}
126}
127/*** // <summary>
128 // additional operation. If the linear table is not full, add the new element with the item value to the end.
129 /// </Summary>
130 /// <Param name = "item"> </param>
131 public void append (T item)
132 {
133 dbnode <t> newnode = new dbnode <t> (item );
134 dbnode <t> P = new dbnode <t> ();
135 if (header = NULL)
136 {
137 header = fig;
138 return;
139}
140 p = header;
141
142 while (P. Next! = NULL)
143 {
144 p = P. Next;
145}
146 p. Next = newnode;
147}
148
149/*** // <summary>
150 // delete operation
151 /// </Summary>
152 /// <Param name = "I"> </param>
153 /// <returns> </returns>
154 public t Delete (int I)
155 {
156 dbnode <t> current = findnode (I );
157 if (current! = NULL)
158 {
159 current. Prev. Next = current. Next;
160 current. Next. Prev = current. Prev;
161 current. Prev = NULL;
162 current. Next = NULL;
163 Return Current. Data;
164}
165 else
166 {
167 console. Write ("the node is not exist! ");
168 return default (t );
169}
170}
171/*** // <summary>
172 // retrieve the table element
173 /// </Summary>
174 /// <Param name = "I"> </param>
175 /// <returns> </returns>
176 public t getelem (int I)
177 {
178 dbnode <t> current = findnode (I );
179 If (current! = NULL)
180 {
181 Return Current. Data;
182}
183 else
184 {
185 console. Write ("the node is not exist! ");
186 return default (t );
187}
188}
189
190/*** // <summary>
191 // search by value
192 /// </Summary>
193 /// <Param name = "value"> </param>
194 /// <returns>-1: The linked list or parameter is abnormal; 0: the node does not exist; 1: The position represented by the value </returns>
195 public int locate (T value)
196 {
197 If (isempty ())
198 {
199 console. Write ("list is empty ");
200 return-1;
201}
202 If (value = NULL)
203 {
204 console. Write ("value is empty ");
205 return-1;
206}
207 dbnode <t> current = new dbnode <t> ();
208 current = header;
209 int result = 0;
210
211 while (current. Next! = NULL)
212 {
213 If (current. Data. Equals (value ))
214 {
215 result = 1;
217}
218}
219 return result;
220}
221}
In addition, Loop The basic operations of a linked list are roughly the same as those of a Single-linked list. The condition for determining the end of a linked list is not to determine whether the reference field of a node is null,
It is used to determine whether the reference domain of a node is a header reference. There are no major changes in other fields. Interest Can be written by friends.