-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedList.cpp
250 lines (192 loc) · 4.77 KB
/
linkedList.cpp
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
/* This file contains an implementation of a singly list
* Code by: Humayun Kabir, [email protected] */
#include <iostream>
using namespace std;
//Node of a LinkedList
template<class T>
struct Node {
T val;
Node *next;
Node() {}
Node(const T& t, Node *p) : val(t), next(p) {}
//destructor
~Node() {
delete next;
}
};
//iterator classes for LinkedList
template<class T>
class ListIter;
template<class T>
class constListIter;
//class to represent linked list
template<class T>
class LinkedList {
friend class ListIter<T>;
friend class constListIter<T>;
private:
Node<T> *head;
int Size;
public:
typedef ListIter<T> iterator;
typedef constListIter<T> const_iterator;
typedef std::forward_iterator_tag iterator_category;
typedef std::ptrdiff_t difference_type;
typedef size_t size_type;
typedef T value_type;
typedef T * pointer;
typedef T & reference;
//default constructor
LinkedList();
//insert an element into the list
void insert(T inVal);
//adds an element into the list
void add(T inVal) { insert(inVal); }
//deletes the head and retuns the value it contains
T delVal();
//checks if the list is empty
bool isEmpty();
//returns the number of elements in the list
int size() { return Size; }
//returns iterator initializing to the beginning of the list
iterator begin() { return iterator(head); }
//returns iterator initializing to the end of the list
iterator end() { return iterator(NULL); }
//returns constant iterator initializing to the beginning of the list
const_iterator begin() const { return const_iterator(head); }
//returns constant iterator initializing to the end of the list
const_iterator end() const { return const_iterator(NULL); }
~LinkedList() {
delete head;
}
};
//default constructor
template<class T>
LinkedList<T>::LinkedList() {
head = NULL;
Size = 0;
}
//insert an element into the list
template<class T>
void LinkedList<T>::insert(T inVal) {
Node<T> *oldHead = head;
head = new Node<T>(inVal, oldHead);
Size ++;
}
//deletes the head and retuns the value it contains
template<class T>
T LinkedList<T>::delVal() {
try {
if( head == NULL ) {
throw string("Can't delete from empty list.");
}
else {
T retVal = head->val;
Node<T>* tempHead = head;
head = head->next;
tempHead->next = NULL;
delete tempHead;
Size--;
return retVal;
}
}
catch (string s) {
cout<<s<<endl;
}
catch(...) {
cout<<"Unhandled exception. \n";
}
}
//checks if the list is empty
template<class T>
bool LinkedList<T>::isEmpty() {
return head == NULL;
}
//ListIter class
template<class T>
class ListIter {
private:
// Node<T>* head;
public:
Node<T>* head;
//constructor
ListIter(Node<T>* x = 0) : head(x) {}
//equality operator
bool operator==(const ListIter<T> &rhs) {
if( head == rhs.head )
return true;
else
return false;
}
//not equal to operator
bool operator!=(const ListIter<T> &rhs) {
return !(*this == rhs);
}
//dereference operator
T & operator*() { return head->val; }
//Prefix increment -- ++iter
ListIter<T> & operator++() {
head = head->next;
return *this;
}
//Postfix increment -- iter++
ListIter<T> operator++(int) {
ListIter<T> clone(*this);
head = head->next;
return clone;
}
};
//constListIter class
template<class T>
class constListIter {
private:
const Node<T>* head;
typedef std::forward_iterator_tag iterator_category;
typedef std::ptrdiff_t difference_type;
typedef size_t size_type;
typedef T value_type;
typedef const T * pointer;
typedef const T & reference;
public:
//Constructor
constListIter(Node<T>* x = 0) : head(x) { }
//Copy constructor
constListIter(const ListIter<T> & rhs) : head(rhs.head) { }
//equality operator
bool operator==(const constListIter<T> & rhs) {
return head == rhs.head;
}
//not equal to operator
bool operator!=(const constListIter<T> & rhs) {
return !(*this == rhs);
}
//dereference operator
reference operator * () const { return head->val; }
pointer operator->() const { return &(head->val); }
//Prefix increment -- ++iter
constListIter& operator++() const {
head = head->next;
return *this;
}
//Postfix increment -- iter++
constListIter operator++(int) {
constListIter clone(*this);
head = head->next;
return clone;
}
};
//Test the data structure
int main() {
LinkedList<int> lList;
lList.insert(10);
lList.insert(5);
lList.insert(50);
cout<<"Values in the list: \n";
for(LinkedList<int>::iterator it = lList.begin(); it != lList.end(); it++)
cout<<*it<<endl;
cout<<"Deleted a Value: "<<lList.delVal()<<endl;
cout<<"Values in the list: \n";
for(LinkedList<int>::const_iterator it = lList.begin(); it != lList.end(); it++)
cout<<*it<<endl;
return 0;
}