-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdlist.cpp
More file actions
executable file
·292 lines (256 loc) · 5.19 KB
/
dlist.cpp
File metadata and controls
executable file
·292 lines (256 loc) · 5.19 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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
//
// dlist.cpp
// Project 3 Spring 14
//
// Created by Ibrahima Niang on 4/18/14.
// Copyright (c) 2014 Ibrahima Niang. All rights reserved.
//
#include "dlist.h"
template <class xtype>
dlist<xtype> :: dlist()
{
head = last=NULL;
length=0;
}
template <class xtype>
dlist<xtype> :: ~dlist()
{
makeEmpty();
}
template <class xtype>
void dlist<xtype> :: makeEmpty()
{
node<xtype> *p;
while ( head != NULL )
{
p = head;
head = head->next;
delete p;
}
length=0;
}
template <class xtype>
bool dlist<xtype> :: isEmpty()
{
return head == NULL;
}
template <class xtype>
bool dlist<xtype> :: isFull()
{
node<xtype> *p = new node<xtype>;
if ( p == NULL )
return true;
else{
delete p;
return false;
}
}
template <class xtype>
dlist<xtype> :: dlist ( const dlist<xtype> & other )
{
head = NULL;
copyList (other);
}
template <class xtype>
dlist<xtype>& dlist<xtype>:: operator= ( const dlist<xtype> & other )
{
copyList (other);
return *this;
}
template <class xtype>
void dlist<xtype>:: copyList ( const dlist<xtype> & other )
{
node<xtype> *temp;
node<xtype> *p;
if ( head != NULL )
makeEmpty();
if ( other.head == NULL )
head = NULL;
else
{
length=other.length;
p = other.head;
head = new node<xtype>;
head->info = p->info;
head->next = head->back = NULL;
temp = head;
p = p->next;
while ( p != NULL )
{
temp->next = new node<xtype>;
temp->next->info = p->info;
temp->next->next = NULL;
temp->next->back = temp;
temp = temp->next;
p = p->next;
}
last=temp;
}
}
template <class xtype>
void dlist<xtype> :: printList()
{
if ( head == NULL )
cout<<"LIST IS EMPTY";
else
{
node<xtype> *p = head;
while ( p != NULL )
{
cout<<p->info<<" ";
p = p->next;
}
cout<<endl<<getlength()<<endl;
p=last;
while ( p != NULL )
{
cout<<p->info<<" ";
p = p->back;
}
}
}
template <class xtype>
void dlist<xtype>::insertFront(xtype item)
{
if ( isFull() )
cout<<"\nLIST IS FULL";
else
{
node<xtype> *temp = new node<xtype>;
temp->info = item;
temp->next = head;
temp->back = NULL;
if ( head != NULL )
head->back = temp;
if(last==NULL)
last=head;
head = temp;
length++;
}
}
template <class xtype>
void dlist<xtype> :: insertBack( xtype item )
{
if ( isFull() )
cout<<"\nLIST IS FULL";
else
{
node<xtype> *temp = new node<xtype>;
temp->info = item;
if ( head == NULL )
{
temp->next =temp->back = NULL;
head=last=temp;
}
else
{
node<xtype> *p = head;
while ( p->next != NULL)
p = p->next;
temp->next = NULL;
p->next=temp;
temp->back = p;
last=temp;
}
length++;
}
}
template <class xtype>
bool dlist<xtype> :: searchItem( xtype item )
{
if ( head == NULL )
cout<<"LIST IS EMPTY";
else
{
node<xtype> *p = head;
while ( p != NULL && p->info <= item )
{
if ( p->info == item )
return true;
p = p->next;
}
}
return false;
}
template <class xtype>
void dlist<xtype> :: deleteItem( xtype item )
{
if ( head == NULL )
cout<<"\nLIST IS EMPTY";
else
{
node<xtype> * r = head;
if ( head->info == item )
{
delete r;
head = head->next;
length--;
}
else
{
while ( r != NULL && r->info < item )
r = r->next;
if ( r != NULL && r->info == item )
{
r->back->next = r->next;
if ( r->next != NULL )
r->next->back = r->back;
if(r->next==NULL)
last=r->back;
delete r;
length--;
}
else cout <<"\nITEM NOT IN THE LIST";
}
}
}
template <class xtype>
int dlist<xtype>::getlength()
{
return length;
}
template <class xtype>
void dlist<xtype>::frontIterator()
{
temp=temp->next;
}
template <class xtype>
xtype dlist<xtype> ::getTempInfo()
{
return temp->info;
}
template <class xtype>
xtype dlist<xtype> ::getCurrentInfo()
{
return current->info;
}
template <class xtype>
void dlist<xtype>::resetTempHead()
{
temp=head;
}
template <class xtype>
void dlist<xtype>::backIterator()
{
temp=temp->back;
}
template <class xtype>
void dlist<xtype>::resetTempLast()
{
temp=last;
}
template <class xtype>
void dlist<xtype>::resetCurrent()
{
current=temp;
}
template <class xtype>
void dlist<xtype>::currentBackIterator()
{
current=current->back;
}
template <class xtype>
void dlist<xtype>::currentFrontIterator()
{
current=current->next;
}