-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularList.cpp
More file actions
154 lines (139 loc) · 3.85 KB
/
Copy pathCircularList.cpp
File metadata and controls
154 lines (139 loc) · 3.85 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
#include <iostream>
#include <stdexcept>
using namespace std;
template<typename N>
class Node {
private:
N element;
Node<N>* next_node;
public:
Node(N e = 0, Node<N>* n = nullptr) : element(e), next_node(n) {}
N retrieve() const { return element; }
Node<N>* next() const { return next_node; }
void setnext(Node<N>* n) { next_node = n; }
void setelement(N e) { element = e; }
};
template<typename C>
class CircularList {
private:
Node<C>* tail; // points to the last node
public:
CircularList() : tail(nullptr) {}
bool emptyList() const { return tail == nullptr; }
int listSize() const {
if (tail == nullptr) return 0;
int s = 0;
Node<C>* ptr = tail->next();
do {
s++;
ptr = ptr->next();
} while (ptr != tail->next());
return s;
}
C frontElement() const {
if (tail == nullptr) throw "underflow";
return tail->next()->retrieve();
}
C lastElement() const {
if (tail == nullptr) throw "underflow";
return tail->retrieve();
}
void pushAtFront(C e) {
Node<C>* newNode = new Node<C>(e);
if (tail == nullptr) {
newNode->setnext(newNode);
tail = newNode;
} else {
newNode->setnext(tail->next());
tail->setnext(newNode);
}
}
void pushAtEnd(C e) {
pushAtFront(e);
tail = tail->next();
}
C popAtFront() {
if (tail == nullptr) throw "underflow";
Node<C>* head = tail->next();
C e = head->retrieve();
if (head == tail) {
delete head;
tail = nullptr;
} else {
tail->setnext(head->next());
delete head;
}
return e;
}
C popAtEnd() {
if (tail == nullptr) throw "underflow";
Node<C>* head = tail->next();
C e;
if (head == tail) {
e = tail->retrieve();
delete tail;
tail = nullptr;
return e;
}
Node<C>* ptr = head;
while (ptr->next() != tail) {
ptr = ptr->next();
}
e = tail->retrieve();
ptr->setnext(head);
delete tail;
tail = ptr;
return e;
}
void display() const {
if (tail==nullptr) {
cout<<"List is empty\n";
return;
}
Node<C>* ptr = tail->next();
do {
cout<<ptr->retrieve()<<" ";
ptr=ptr->next();
} while(ptr != tail->next());
cout << endl;
}
void eraseElement(C e) {
if (tail == nullptr) {
cout << "List is empty\n";
return;
}
Node<C>* ptr = tail->next();
Node<C>* previous = tail;
bool found = false;
do {
if (ptr->retrieve() == e) {
found = true;
Node<C>* erasePtr = ptr;
if (erasePtr == tail->next()) {
popAtFront();
ptr = tail ? tail->next() : nullptr;
previous = tail;
} else if (erasePtr == tail) {
popAtEnd();
ptr = tail ? tail->next() : nullptr;
} else {
previous->setnext(erasePtr->next());
ptr = erasePtr->next();
delete erasePtr;
}
} else {
previous = ptr;
ptr = ptr->next();
}
} while (ptr && ptr!=tail->next());
if (!found) cout << "Element not found\n";
}
~CircularList() {
while (!emptyList()) {
popAtFront();
}
}
};
int main() {
return 0;
}