-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist1.h
More file actions
203 lines (151 loc) · 4.47 KB
/
Copy pathlist1.h
File metadata and controls
203 lines (151 loc) · 4.47 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
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
//doubly linked list
template <typename T>
struct Node
{ //structure for a list's node; it has the node's info and 2 pointers
T info;
Node <T> *next; //pointer to the next node
Node <T> *prev; //pointer to the previous node
};
template <typename T>
class LinkedList
{
public:
Node <T> *pfirst; //pointer to the first node
Node <T> *plast; //pointer to the last node
void addFirst(T x)
{
//add info x on the first position of the list
Node <T> *paux = new Node<T>;
paux->info = x; /// or (*paux).info = x;
paux->prev = NULL;
paux->next = pfirst;
if (pfirst != NULL)
pfirst->prev = paux;
pfirst = paux; //we link the old "first" with the new one
if (plast==NULL)
plast=pfirst;
}
void addLast(T x)
{
//add info x on the last position of the list
Node<T> *paux = new Node <T>;
paux->info = x;
paux->prev = plast;
paux->next = NULL;
if (plast != NULL)
plast->next = paux;
plast = paux;
if (pfirst == NULL)
pfirst = plast;
}
T getInfo (Node<T>* p)
{
//return info from a specific node
return p->info;
}
void removeFirst()
{
//removes the first element of the list
Node<T>* paux;
if (pfirst != NULL)
{
paux = pfirst->next;
if (pfirst == plast) //un seul element dans la l
plast = NULL;
delete pfirst; //delete the first node of the list
pfirst = paux; //the following node becomes the new first
if (pfirst != NULL)
pfirst->prev = NULL;
}
else
cout<<"The list is empty"<<endl;
}
void removeLast()
{
//removes the last element of the list
Node <T> *paux;
if (plast != NULL)
{
paux = plast->prev;
if (pfirst == plast)
pfirst = NULL;
delete plast; //delete the last node of the list
plast = paux; //the previous node becomes the new last
if (plast != NULL)
plast->next = NULL;
}
else
cout<<"The list is empty"<<endl;
}
Node <T>* findFirstOccurrence(T x)
{
//search for the first node which has the info x
Node <T> *paux = pfirst;
while (paux != NULL)
{
if (paux->info == x)
return paux;
paux = paux->next;
}
return NULL;
}
Node <T>* findLastOccurrence(T x)
{ //search for the last node which has the info x
Node <T> *paux;
paux = plast;
while (paux != NULL) {
if (paux->info == x)
return paux;
paux = paux->prev;
}
return NULL;
}
void removeFirstOccurrence(T x)
{
//remove the first node which has the info x
Node <T> *px;
px = findFirstOccurrence(x); //px must be removed
if (px != NULL)
{//we must re-link the pointers
if (px->prev != NULL)
px->prev->next = px->next;
if (px->next != NULL)
px->next->prev = px->prev;
if (px->prev == NULL) // if px == pfirst
pfirst = px->next;
if (px->next == NULL) // if px == plast
plast = px->prev;
delete px; //now we can delete it
}
}
void removeLastOccurrence(T x)
{
//remove the last node which has the info x
Node <T> *px;
px = findLastOccurrence(x);
if (px != NULL)
{
if (px->prev != NULL)
px->prev->next = px->next;
if (px->next != NULL)
px->next->prev = px->prev;
if (px->prev == NULL) // if px == pfirst
pfirst = px->next;
if (px->next == NULL) // if px == plast
plast = px->prev;
delete px;
}
}
int isEmpty()
{ //check if the list is empty
return (pfirst == NULL);
}
LinkedList()
{ //constructor
pfirst = plast = NULL;
}
};