forked from aaronbloomfield/pdr
-
Notifications
You must be signed in to change notification settings - Fork 228
/
list.h.html
279 lines (257 loc) · 31.5 KB
/
list.h.html
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="GNU source-highlight
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite">
<title>list.h</title>
</head>
<body style="background-color:white">
<pre><i><span style="color:#9A1900">/* This file contains the List class, a singly linked unordered</span></i>
<i><span style="color:#9A1900"> * templated list class. struct List_Link is the data structure for</span></i>
<i><span style="color:#9A1900"> * the list links.</span></i>
<i><span style="color:#9A1900"> *</span></i>
<i><span style="color:#9A1900"> * Copyright (c) 1994, 2014 by Aaron Bloomfield</span></i>
<i><span style="color:#9A1900"> * Released under a CC BY-SA license</span></i>
<i><span style="color:#9A1900"> *</span></i>
<i><span style="color:#9A1900"> * Revision history</span></i>
<i><span style="color:#9A1900"> * 05-01-94: Main code written</span></i>
<i><span style="color:#9A1900"> * 07-12-95: Bug updates</span></i>
<i><span style="color:#9A1900"> * 01-13-14: Modified to fit modern C++ compilers; reformatted</span></i>
<i><span style="color:#9A1900"> *</span></i>
<i><span style="color:#9A1900"> * The tail of the list, in this implementation, is where the elements</span></i>
<i><span style="color:#9A1900"> * are pushed (added) to and popped (removed) from. For example:</span></i>
<i><span style="color:#9A1900"> *</span></i>
<i><span style="color:#9A1900"> * head <-- element <-- element <-- element <-- element <-- tail</span></i>
<i><span style="color:#9A1900"> *</span></i>
<i><span style="color:#9A1900"> *</span></i>
<i><span style="color:#9A1900"> * Methods:</span></i>
<i><span style="color:#9A1900"> *</span></i>
<i><span style="color:#9A1900"> * List() Constructor, initilized an empty list</span></i>
<i><span style="color:#9A1900"> * ~List() Destructor, deletes the entire list</span></i>
<i><span style="color:#9A1900"> * void push(T item); Adds an item to the tail of the list</span></i>
<i><span style="color:#9A1900"> * T* pop(); Removes an item from the tail of the list</span></i>
<i><span style="color:#9A1900"> * int size(); Returns the size (length) of the list</span></i>
<i><span style="color:#9A1900"> * void display(); Displays the list</span></i>
<i><span style="color:#9A1900"> * int empty(); Returns 1 if the list is empty, 0 otherwise</span></i>
<i><span style="color:#9A1900"> * void clear(); Clears (removes all elements from) the list</span></i>
<i><span style="color:#9A1900"> * T* push_head(); Adds an item to the head of the list</span></i>
<i><span style="color:#9A1900"> * T* tail(); Returns the tail data</span></i>
<i><span style="color:#9A1900"> * T* head(); Returns the head data</span></i>
<i><span style="color:#9A1900"> * int element (T item); Returns 1 if T is in the list, 0 otherwise</span></i>
<i><span style="color:#9A1900"> * pop_head (T item); Removes an item from the head of the list</span></i>
<i><span style="color:#9A1900"> * int remove (T item) Removes item, returns 1 if sucessful, else 0</span></i>
<i><span style="color:#9A1900"> * T* getptr (T item) Returns the pointer to the parameter or NULL</span></i>
<i><span style="color:#9A1900"> * void save(FILE *fp) Saves the list- calls the objects methods</span></i>
<i><span style="color:#9A1900"> *</span></i>
<i><span style="color:#9A1900"> *</span></i>
<i><span style="color:#9A1900"> * The List class requires <iostream> to be included, as it uses</span></i>
<i><span style="color:#9A1900"> * cout's in the display method.</span></i>
<i><span style="color:#9A1900"> *</span></i>
<i><span style="color:#9A1900"> * In order to use the List class, the data type T must either be a</span></i>
<i><span style="color:#9A1900"> * standard type, or be a class with the '==' operator overloaded. To</span></i>
<i><span style="color:#9A1900"> * use the display method, the data type must also have the '<<'</span></i>
<i><span style="color:#9A1900"> * operator overloaded. Examples follow, using a Complex class with</span></i>
<i><span style="color:#9A1900"> * two attributes, real and imag (both ints). The operator overloads</span></i>
<i><span style="color:#9A1900"> * need to be functions outside the class declaration, and the</span></i>
<i><span style="color:#9A1900"> * attributes used need to be public.</span></i>
<i><span style="color:#9A1900"> *</span></i>
<i><span style="color:#9A1900"> * class Complex {</span></i>
<i><span style="color:#9A1900"> * public:</span></i>
<i><span style="color:#9A1900"> * int real, imag;</span></i>
<i><span style="color:#9A1900"> * Complex () { real = imag = 0; }</span></i>
<i><span style="color:#9A1900"> * Complex (int r, int i) { real = r; imag = i; }</span></i>
<i><span style="color:#9A1900"> * ~Complex () { }</span></i>
<i><span style="color:#9A1900"> * void set (int r, int i) { real = r; imag = i; }</span></i>
<i><span style="color:#9A1900"> * };</span></i>
<i><span style="color:#9A1900"> *</span></i>
<i><span style="color:#9A1900"> * int operator == (Complex c1, Complex c2) {</span></i>
<i><span style="color:#9A1900"> * return ( (c1.real == c2.real) && (c1.imag == c2.imag) );</span></i>
<i><span style="color:#9A1900"> * }</span></i>
<i><span style="color:#9A1900"> *</span></i>
<i><span style="color:#9A1900"> * int operator != (Complex c1, Complex c2) {</span></i>
<i><span style="color:#9A1900"> * return ( (c1.real != c2.real) || (c1.imag != c2.imag) );</span></i>
<i><span style="color:#9A1900"> * }</span></i>
<i><span style="color:#9A1900"> *</span></i>
<i><span style="color:#9A1900"> * ostream& operator << (ostream& out, Complex c) {</span></i>
<i><span style="color:#9A1900"> * return (out << c.real << " + " << c.imag << "i");</span></i>
<i><span style="color:#9A1900"> * }</span></i>
<i><span style="color:#9A1900"> *</span></i>
<i><span style="color:#9A1900"> */</span></i>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><iostream></span>
<b><span style="color:#0000FF">using</span></b> <b><span style="color:#0000FF">namespace</span></b> std<span style="color:#990000">;</span>
<b><span style="color:#0000FF">template</span></b> <span style="color:#990000"><</span><b><span style="color:#0000FF">class</span></b> <span style="color:#008080">T</span><span style="color:#990000">></span>
<b><span style="color:#0000FF">struct</span></b> <span style="color:#008080">List_Link</span> <span style="color:#FF0000">{</span>
<span style="color:#008080">T</span> <span style="color:#990000">*</span>data<span style="color:#990000">;</span>
<span style="color:#008080">List_Link<T></span> <span style="color:#990000">*</span>next<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">template</span></b> <span style="color:#990000"><</span><b><span style="color:#0000FF">class</span></b> <span style="color:#008080">T</span><span style="color:#990000">></span>
<b><span style="color:#0000FF">class</span></b> <span style="color:#008080">List</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">friend</span></b> <b><span style="color:#0000FF">class</span></b> <span style="color:#008080">Fsm</span><span style="color:#990000">;</span>
<i><span style="color:#9A1900">/* head <-- element <-- element <-- element <-- element <-- tail */</span></i>
<b><span style="color:#0000FF">private</span></b><span style="color:#990000">:</span>
<span style="color:#008080">List_Link<T></span> <span style="color:#990000">*</span>_head<span style="color:#990000">,</span> <span style="color:#990000">*</span>_tail<span style="color:#990000">;</span>
<b><span style="color:#0000FF">public</span></b><span style="color:#990000">:</span>
<b><span style="color:#000000">List</span></b><span style="color:#990000">()</span> <span style="color:#FF0000">{</span>
_head <span style="color:#990000">=</span> _tail <span style="color:#990000">=</span> NULL<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span> <i><span style="color:#9A1900">// cout << "List Constructor called.\n"; }</span></i>
<span style="color:#990000">~</span><b><span style="color:#000000">List</span></b><span style="color:#990000">()</span> <span style="color:#FF0000">{</span>
<b><span style="color:#000000">clear</span></b><span style="color:#990000">();</span>
<span style="color:#FF0000">}</span>
<span style="color:#009900">void</span> <b><span style="color:#000000">push</span></b><span style="color:#990000">(</span><span style="color:#008080">T</span> item<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<i><span style="color:#9A1900">// cout << "push(): called with element: " << item << endl;</span></i>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> _head <span style="color:#990000">==</span> NULL <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
_head <span style="color:#990000">=</span> _tail <span style="color:#990000">=</span> <b><span style="color:#0000FF">new</span></b> List_Link<span style="color:#990000"><</span>T<span style="color:#990000">>;</span>
_head<span style="color:#990000">-></span>next <span style="color:#990000">=</span> NULL<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span> <b><span style="color:#0000FF">else</span></b> <span style="color:#FF0000">{</span>
<span style="color:#008080">List_Link<T></span> <span style="color:#990000">*</span>temp<span style="color:#990000">;</span>
temp <span style="color:#990000">=</span> _tail<span style="color:#990000">;</span>
_tail <span style="color:#990000">=</span> <b><span style="color:#0000FF">new</span></b> List_Link<span style="color:#990000"><</span>T<span style="color:#990000">>;</span>
_tail<span style="color:#990000">-></span>next <span style="color:#990000">=</span> temp<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
_tail<span style="color:#990000">-></span>data <span style="color:#990000">=</span> <b><span style="color:#0000FF">new</span></b> T<span style="color:#990000">;</span>
<span style="color:#990000">*</span>_tail<span style="color:#990000">-></span>data <span style="color:#990000">=</span> item<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
T<span style="color:#990000">*</span> <b><span style="color:#000000">pop</span></b><span style="color:#990000">()</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> _head <span style="color:#990000">==</span> NULL <span style="color:#990000">)</span>
<b><span style="color:#0000FF">return</span></b> NULL<span style="color:#990000">;</span>
<b><span style="color:#0000FF">else</span></b> <span style="color:#FF0000">{</span>
<span style="color:#008080">List_Link<T></span> <span style="color:#990000">*</span>temp<span style="color:#990000">;</span>
<span style="color:#008080">T</span> <span style="color:#990000">*</span>ret<span style="color:#990000">;</span>
temp <span style="color:#990000">=</span> _tail<span style="color:#990000">;</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> _head <span style="color:#990000">==</span> _tail <span style="color:#990000">)</span>
_tail <span style="color:#990000">=</span> _head <span style="color:#990000">=</span> NULL<span style="color:#990000">;</span>
<b><span style="color:#0000FF">else</span></b>
_tail <span style="color:#990000">=</span> _tail<span style="color:#990000">-></span>next<span style="color:#990000">;</span>
ret <span style="color:#990000">=</span> temp<span style="color:#990000">-></span>data<span style="color:#990000">;</span>
<b><span style="color:#0000FF">delete</span></b> temp<span style="color:#990000">;</span>
<b><span style="color:#0000FF">return</span></b> ret<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<span style="color:#FF0000">}</span>
<span style="color:#009900">int</span> <b><span style="color:#000000">size</span></b><span style="color:#990000">()</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> _tail <span style="color:#990000">==</span> NULL <span style="color:#990000">)</span>
<b><span style="color:#0000FF">return</span></b> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">else</span></b> <span style="color:#FF0000">{</span>
<span style="color:#009900">int</span> s <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<span style="color:#008080">List_Link<T></span> <span style="color:#990000">*</span>temp<span style="color:#990000">;</span>
<b><span style="color:#0000FF">for</span></b> <span style="color:#990000">(</span> temp <span style="color:#990000">=</span> _tail<span style="color:#990000">;</span> temp <span style="color:#990000">!=</span> NULL<span style="color:#990000">;</span> temp <span style="color:#990000">=</span> temp<span style="color:#990000">-></span>next <span style="color:#990000">)</span>
<span style="color:#990000">++</span>s<span style="color:#990000">;</span>
<b><span style="color:#0000FF">return</span></b> s<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<span style="color:#FF0000">}</span>
<span style="color:#009900">void</span> <b><span style="color:#000000">save</span></b><span style="color:#990000">(</span><span style="color:#008080">FILE</span> <span style="color:#990000">*</span>fp<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> _head <span style="color:#990000">==</span> NULL <span style="color:#990000">)</span> <span style="color:#FF0000">{}</span>
<b><span style="color:#0000FF">else</span></b> <span style="color:#FF0000">{</span>
<span style="color:#008080">List_Link<T></span> <span style="color:#990000">*</span>temp<span style="color:#990000">;</span>
<b><span style="color:#0000FF">for</span></b> <span style="color:#990000">(</span> temp <span style="color:#990000">=</span> _tail<span style="color:#990000">;</span> temp <span style="color:#990000">!=</span> NULL<span style="color:#990000">;</span> temp <span style="color:#990000">=</span> temp<span style="color:#990000">-></span>next <span style="color:#990000">)</span>
temp<span style="color:#990000">-></span>data<span style="color:#990000">-></span><b><span style="color:#000000">save</span></b><span style="color:#990000">(</span>fp<span style="color:#990000">);</span>
<span style="color:#FF0000">}</span>
<span style="color:#FF0000">}</span>
<span style="color:#009900">void</span> <b><span style="color:#000000">display</span></b><span style="color:#990000">()</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> _head <span style="color:#990000">==</span> NULL <span style="color:#990000">)</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"List is empty.</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">else</span></b> <span style="color:#FF0000">{</span>
<i><span style="color:#9A1900">// cout << "display(): List is printed reverse: List is not empty.\n\t";</span></i>
<span style="color:#008080">List_Link<T></span> <span style="color:#990000">*</span>temp<span style="color:#990000">;</span>
<b><span style="color:#0000FF">for</span></b> <span style="color:#990000">(</span> temp <span style="color:#990000">=</span> _tail<span style="color:#990000">;</span> temp <span style="color:#990000">!=</span> NULL<span style="color:#990000">;</span> temp <span style="color:#990000">=</span> temp<span style="color:#990000">-></span>next <span style="color:#990000">)</span>
cout <span style="color:#990000"><<</span> <span style="color:#990000">*</span>temp<span style="color:#990000">-></span>data <span style="color:#990000"><<</span> <span style="color:#FF0000">" "</span><span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<span style="color:#FF0000">}</span>
<span style="color:#009900">int</span> <b><span style="color:#000000">empty</span></b><span style="color:#990000">()</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">return</span></b> <span style="color:#990000">(</span> _head <span style="color:#990000">==</span> NULL <span style="color:#990000">);</span>
<span style="color:#FF0000">}</span>
T<span style="color:#990000">*</span> <b><span style="color:#000000">tail</span></b><span style="color:#990000">()</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> _tail <span style="color:#990000">==</span> NULL <span style="color:#990000">)</span>
<b><span style="color:#0000FF">return</span></b> NULL<span style="color:#990000">;</span>
<b><span style="color:#0000FF">else</span></b>
<b><span style="color:#0000FF">return</span></b> _tail<span style="color:#990000">-></span>data<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
T<span style="color:#990000">*</span> <b><span style="color:#000000">head</span></b><span style="color:#990000">()</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> _head <span style="color:#990000">==</span> NULL <span style="color:#990000">)</span>
<b><span style="color:#0000FF">return</span></b> NULL<span style="color:#990000">;</span>
<b><span style="color:#0000FF">else</span></b>
<b><span style="color:#0000FF">return</span></b> _head<span style="color:#990000">-></span>data<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<span style="color:#009900">int</span> <b><span style="color:#000000">element</span></b> <span style="color:#990000">(</span><span style="color:#008080">T</span> item<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<span style="color:#008080">List_Link<T></span> <span style="color:#990000">*</span>temp<span style="color:#990000">;</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> _tail <span style="color:#990000">==</span> NULL <span style="color:#990000">)</span>
<b><span style="color:#0000FF">return</span></b> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">for</span></b> <span style="color:#990000">(</span> temp <span style="color:#990000">=</span> _tail<span style="color:#990000">;</span> temp <span style="color:#990000">!=</span> NULL<span style="color:#990000">;</span> temp <span style="color:#990000">=</span> temp<span style="color:#990000">-></span>next <span style="color:#990000">)</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> <span style="color:#990000">*</span>temp<span style="color:#990000">-></span>data <span style="color:#990000">==</span> item <span style="color:#990000">)</span>
<b><span style="color:#0000FF">return</span></b> <span style="color:#993399">1</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">return</span></b> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
T<span style="color:#990000">*</span> <b><span style="color:#000000">getptr</span></b> <span style="color:#990000">(</span><span style="color:#008080">T</span> item<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<span style="color:#008080">List_Link<T></span> <span style="color:#990000">*</span>temp<span style="color:#990000">;</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> _tail <span style="color:#990000">==</span> NULL <span style="color:#990000">)</span>
<b><span style="color:#0000FF">return</span></b> NULL<span style="color:#990000">;</span>
<b><span style="color:#0000FF">for</span></b> <span style="color:#990000">(</span> temp <span style="color:#990000">=</span> _tail<span style="color:#990000">;</span> temp <span style="color:#990000">!=</span> NULL<span style="color:#990000">;</span> temp <span style="color:#990000">=</span> temp<span style="color:#990000">-></span>next <span style="color:#990000">)</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> <span style="color:#990000">*</span>temp<span style="color:#990000">-></span>data <span style="color:#990000">==</span> item <span style="color:#990000">)</span>
<b><span style="color:#0000FF">return</span></b> temp<span style="color:#990000">-></span>data<span style="color:#990000">;</span>
<b><span style="color:#0000FF">return</span></b> NULL<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<span style="color:#009900">void</span> <b><span style="color:#000000">clear</span></b><span style="color:#990000">()</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">while</span></b> <span style="color:#990000">(</span> <b><span style="color:#0000FF">this</span></b><span style="color:#990000">-></span><b><span style="color:#000000">pop</span></b><span style="color:#990000">()</span> <span style="color:#990000">!=</span> NULL <span style="color:#990000">)</span> <span style="color:#FF0000">{</span> <span style="color:#FF0000">}</span>
<span style="color:#FF0000">}</span>
<span style="color:#009900">void</span> <b><span style="color:#000000">push_head</span></b> <span style="color:#990000">(</span><span style="color:#008080">T</span> item<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> _head <span style="color:#990000">==</span> NULL <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
_head <span style="color:#990000">=</span> _tail <span style="color:#990000">=</span> <b><span style="color:#0000FF">new</span></b> List_Link<span style="color:#990000"><</span>T<span style="color:#990000">>;</span>
_head<span style="color:#990000">-></span>next <span style="color:#990000">=</span> NULL<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span> <b><span style="color:#0000FF">else</span></b> <span style="color:#FF0000">{</span>
<span style="color:#008080">List_Link<T></span> <span style="color:#990000">*</span>temp<span style="color:#990000">;</span>
temp <span style="color:#990000">=</span> _head<span style="color:#990000">;</span>
_head <span style="color:#990000">=</span> <b><span style="color:#0000FF">new</span></b> List_Link<span style="color:#990000"><</span>T<span style="color:#990000">>;</span>
temp<span style="color:#990000">-></span>next <span style="color:#990000">=</span> _head<span style="color:#990000">;</span>
_head<span style="color:#990000">-></span>next <span style="color:#990000">=</span> NULL<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
_head<span style="color:#990000">-></span>data <span style="color:#990000">=</span> <b><span style="color:#0000FF">new</span></b> T<span style="color:#990000">;</span>
<span style="color:#990000">*</span>_head<span style="color:#990000">-></span>data <span style="color:#990000">=</span> item<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
T<span style="color:#990000">*</span> <b><span style="color:#000000">pop_head</span></b><span style="color:#990000">()</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> _head <span style="color:#990000">==</span> NULL <span style="color:#990000">)</span>
<b><span style="color:#0000FF">return</span></b> NULL<span style="color:#990000">;</span>
<b><span style="color:#0000FF">else</span></b> <span style="color:#FF0000">{</span>
<span style="color:#008080">List_Link<T></span> <span style="color:#990000">*</span>temp<span style="color:#990000">;</span>
<span style="color:#008080">T</span> <span style="color:#990000">*</span>ret<span style="color:#990000">;</span>
<b><span style="color:#0000FF">for</span></b> <span style="color:#990000">(</span> temp <span style="color:#990000">=</span> _tail<span style="color:#990000">;</span> temp<span style="color:#990000">-></span>next <span style="color:#990000">!=</span> _head<span style="color:#990000">;</span> temp <span style="color:#990000">=</span> temp<span style="color:#990000">-></span>next <span style="color:#990000">)</span> <span style="color:#FF0000">{</span> <span style="color:#FF0000">}</span>
ret <span style="color:#990000">=</span> _head<span style="color:#990000">-></span>data<span style="color:#990000">;</span>
_head <span style="color:#990000">=</span> temp<span style="color:#990000">;</span>
temp <span style="color:#990000">=</span> temp<span style="color:#990000">-></span>next<span style="color:#990000">;</span>
<b><span style="color:#0000FF">delete</span></b> temp<span style="color:#990000">;</span>
_head<span style="color:#990000">-></span>next <span style="color:#990000">=</span> NULL<span style="color:#990000">;</span>
<b><span style="color:#0000FF">return</span></b> ret<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<span style="color:#FF0000">}</span>
<span style="color:#009900">int</span> <b><span style="color:#000000">remove</span></b> <span style="color:#990000">(</span><span style="color:#008080">T</span> item<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> <b><span style="color:#0000FF">this</span></b><span style="color:#990000">-></span><b><span style="color:#000000">element</span></b><span style="color:#990000">(</span>item<span style="color:#990000">)</span> <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"remove(): element '"</span> <span style="color:#990000"><<</span> item <span style="color:#990000"><<</span> <span style="color:#FF0000">"' exists.</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> _head <span style="color:#990000">==</span> _tail <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">delete</span></b> _head<span style="color:#990000">;</span>
_head <span style="color:#990000">=</span> _tail <span style="color:#990000">=</span> NULL<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span> <b><span style="color:#0000FF">else</span></b> <b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> <span style="color:#990000">*</span>_tail<span style="color:#990000">-></span>data <span style="color:#990000">==</span> item <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<span style="color:#008080">List_Link<T></span> <span style="color:#990000">*</span>temp<span style="color:#990000">;</span>
temp <span style="color:#990000">=</span> _tail<span style="color:#990000">;</span>
_tail <span style="color:#990000">=</span> _tail<span style="color:#990000">-></span>next<span style="color:#990000">;</span>
<b><span style="color:#0000FF">delete</span></b> temp<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span> <b><span style="color:#0000FF">else</span></b> <span style="color:#FF0000">{</span>
<span style="color:#008080">List_Link<T></span> <span style="color:#990000">*</span>temp<span style="color:#990000">,</span> <span style="color:#990000">*</span>temp2<span style="color:#990000">;</span>
<b><span style="color:#0000FF">for</span></b> <span style="color:#990000">(</span> temp <span style="color:#990000">=</span> _tail<span style="color:#990000">;</span>
<span style="color:#990000">*</span>temp<span style="color:#990000">-></span>next<span style="color:#990000">-></span>data <span style="color:#990000">!=</span> item<span style="color:#990000">;</span>
temp <span style="color:#990000">=</span> temp<span style="color:#990000">-></span>next <span style="color:#990000">)</span> <span style="color:#FF0000">{</span> <span style="color:#FF0000">}</span>
temp2 <span style="color:#990000">=</span> temp<span style="color:#990000">-></span>next<span style="color:#990000">;</span>
temp<span style="color:#990000">-></span>next <span style="color:#990000">=</span> temp<span style="color:#990000">-></span>next<span style="color:#990000">-></span>next<span style="color:#990000">;</span>
<b><span style="color:#0000FF">delete</span></b> temp2<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<b><span style="color:#0000FF">return</span></b> <span style="color:#993399">1</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span> <b><span style="color:#0000FF">else</span></b> <span style="color:#FF0000">{</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"remove(): element '"</span> <span style="color:#990000"><<</span> item <span style="color:#990000"><<</span> <span style="color:#FF0000">"' does not exist.</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">return</span></b> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<span style="color:#FF0000">}</span>
<span style="color:#FF0000">}</span><span style="color:#990000">;</span>
</pre>
</body>
</html>