-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.cpp
126 lines (116 loc) · 3.25 KB
/
list.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
#ifndef LIST_CPP
#define LIST_CPP
#include <cstdlib>
template<typename T> // type of variable that's stored in every cell of a list
class List {
struct Node {
T value; // value stored in node
Node* next; // pointer to the next element in a list
Node* prev; // pointer to the previous element in a list
};
Node* first; // pointer to the first element
Node* last; // pointer to the last element
unsigned int length; // number of elements in list
public:
List() { // constructor
first = NULL;
last = NULL;
length = 0;
}
T front() { // returns value stored in the first element
return first -> value;
}
T back() { // returns value stored in the last element
return last -> value;
}
void push_front(T v) { // pushes given value to the front of a list, so that it will be the first element
Node* node = new Node();
node -> value = v;
node -> next = first;
node -> prev = NULL;
if(length > 0) first -> prev = node;
else last = node;
first = node;
length++;
}
void push_back(T v) { // pushes given value to the back of a list, so that it will be the last element
Node* node = new Node();
node -> value = v;
node -> prev = last;
node -> next = NULL;
if(length > 0) last -> next = node;
else first = node;
last = node;
length++;
}
void pop_front() {
if(length==0) return;
if(length==1) {
last = NULL;
first = NULL;
length--;
return;
}
Node* node = first;
first = first -> next;
first -> prev = NULL;
delete node;
length--;
}
void pop_back() {
if(length==0) return;
if(length==1) {
last = NULL;
first = NULL;
length--;
return;
}
Node* node = last;
last = last -> prev;
last -> next = NULL;
delete node;
length--;
}
/*
* TODO:
* rbegin(), rend()
* size()
* empty()
*/
// class iterator is used to iterate over elements of a List
class iterator {
Node* curr; // pointer to the current element
public:
iterator(Node* node) {
curr = node;
}
iterator(const iterator& other) {
this -> curr = other.curr;
}
T operator*() { // operator * is used to get value which is pointed by the iterator
return curr -> value;
}
bool operator!=(const iterator& other) { // not equal operator overload - checks whether iterators point on different elements
return this -> curr != other.curr;
}
iterator& operator++() { // prefix increment operator overload - moves iterator to the next element
curr = curr -> next;
return *this;
}
/*
* TODO:
* constructor(List)
* postfix ++ operator
* postfix and prefix -- operator
* is equal to operator
*/
};
iterator begin() { // returns iterator to the first element
return iterator(first);
}
iterator end() { // returns iterator to one past the last element
return iterator(NULL);
}
};
#include "list.cpp"
#endif