Website Summary Files Commits Branches @commit @diff @stats
path: root / Seminar05 / LinkedList.hpp
blob: 610d999be132ba326de87636bf770ab6b17c17c2 (plain) (blame)
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#ifndef _SINGLY_LINKED_LIST_IMPL
#define _SINGLY_LINKED_LIST_IMPL

#include<stdexcept>

template<class T>
class LinkedList {
private:
    struct LinkedListNode {
        T data;
        LinkedListNode* next;

        LinkedListNode(const T& elem, LinkedListNode* n = nullptr) : data{ elem }, next { n } {} 
    };

    void free();
    void copy(const LinkedList<T>&);
public:
    class Iterator {
        private:
            // Текущия възел в обхождането
            LinkedListNode* currentNode;

            Iterator(LinkedListNode* arg) : currentNode { arg } {}
        public:
        
        Iterator& operator++() {
            if(currentNode == nullptr)
                return *this;
            currentNode = currentNode->next;
            return *this;
        }

        Iterator operator++(int) {
            Iterator temp = *this;
            ++(*this);
            return temp;
        }

        T& operator*() {
            return currentNode->data;
        }

        bool operator==(const Iterator& other) const {
            return other.currentNode == currentNode;
        }

        bool operator!=(const Iterator& other) const {
            return !(*this == other);
        }

        friend class LinkedList;
    };

    class ConstIterator {
        private:
            const LinkedListNode* currentNode;    // Това вече е указател към константна памет!

            ConstIterator(LinkedListNode* arg) : currentNode { arg } {}  
        public:

        ConstIterator& operator++() {
            if(currentNode == nullptr)
                return *this;
            
            currentNode = currentNode->next;
            return *this;
        }

        ConstIterator operator++(int) {
            ConstIterator temp = *this;
            ++(*this);
            return temp;
        }

        const T& operator*() const {
            return currentNode->data;
        }

        bool operator==(const ConstIterator& other) const {
            return other.currentNode == currentNode;
        }

        bool operator!=(const ConstIterator& other) const {
            return !(*this == other);
        }

        friend class LinkedList;
    };

    LinkedList();

    LinkedList(const LinkedList<T>&);
    LinkedList(LinkedList<T>&&);

    LinkedList& operator=(const LinkedList<T>&);
    LinkedList& operator=(LinkedList<T>&&);

    Iterator begin() {
        return Iterator(head);
    }

    Iterator end() {
        return Iterator(nullptr);
    }

    ConstIterator begin() const {
        return ConstIterator(head);
    }

    ConstIterator end() const {
        return ConstIterator(nullptr);
    }

    ConstIterator cbegin() const {
        return ConstIterator(head);
    }

    ConstIterator cend() const {
        return ConstIterator(nullptr);
    }

    void pushBack(const T&);
    void popBack();

    void pushFront(const T&);
    void popFront();

    const T& front() const;
    const T& back() const;

    T& front();
    T& back();

    bool empty() const;
    size_t size() const;

    ~LinkedList();

private:
    LinkedListNode* head;
    LinkedListNode* tail;
    size_t sizeOfList;
};

template<class T>
LinkedList<T>::LinkedList() : head {nullptr}, tail {nullptr}, sizeOfList { 0 } {}

template<class T>
void LinkedList<T>::free() {
    while(head) {
        LinkedListNode* toDelete = head;
        head = head->next;
        delete toDelete;
    }
    sizeOfList = 0;
}

template<class T>
void LinkedList<T>::copy(const LinkedList<T>& other) {
    LinkedListNode* iter = other.head;

    while(iter) {
        pushBack(iter->data);
        iter = iter->next;
    }

    sizeOfList = other.sizeOfList;
}

template<class T>
LinkedList<T>::LinkedList(const LinkedList<T>& other) : head {nullptr}, tail{ nullptr } {
    copy(other);
}

template<class T>
LinkedList<T>::LinkedList(LinkedList<T>&& other) {
    head = other.head;
    tail = other.tail;
    sizeOfList = other.sizeOfList;

    other.head = other.tail = nullptr;
    other.sizeOfList = 0;
}

template<class T>
LinkedList<T>& LinkedList<T>::operator=(const LinkedList<T>& other) {
    if(this != & other) {
        free();
        copy(other);
    }
    return *this;
}

template<class T>
LinkedList<T>& LinkedList<T>::operator=(LinkedList<T>&& other) {
    if(this != &other) {
        free();
        
        head = other.head;
        tail = other.tail;
        sizeOfList = other.sizeOfList;

        other.head = other.tail = nullptr;
        other.sizeOfList = 0;
    }
    return *this;
}

template<class T>
void LinkedList<T>::pushBack(const T& elem) {
    LinkedListNode* toInsert = new LinkedListNode(elem);
    ++sizeOfList; 

    if(empty()) {
        head = tail = toInsert;
    }
    else {
        tail->next = toInsert;
        tail = toInsert;
    }
}

template<class T>
void LinkedList<T>::popBack() {
    if(empty()) {
        throw std::runtime_error("Tried to pop back on empty list!");
    }
    --sizeOfList;

    if(head == tail) {
        delete tail;
        head = tail = nullptr;
    }
    else {
        LinkedListNode* newTail = head;

        // O(n) за n дължината на свързания списък
        while (newTail->next != tail) { 
            newTail = newTail->next;
        }

        delete tail;
        tail = newTail;
        tail->next = nullptr;
    }
}

template<class T>
void LinkedList<T>::pushFront(const T& elem) {
    LinkedListNode* toInsert = new LinkedListNode(elem, head);
    
    if(empty())
        tail = toInsert;
    
    head = toInsert;

    ++sizeOfList;
}

template<class T>
void LinkedList<T>::popFront() {
    if(empty()) 
        throw std::runtime_error("Cannot pop front from empty list!");
    
    if(head == tail) {
        delete tail;
        head = tail = nullptr;
    }
    else {
        LinkedListNode* toDelete = head;
        head = head->next;
        delete toDelete;
    }

    --sizeOfList;
}

template<class T>
bool LinkedList<T>::empty() const {
    return head == nullptr;
}

template<class T>
size_t LinkedList<T>::size() const {
    return sizeOfList;
}

template<class T>
const T& LinkedList<T>::front() const {
    if(empty()) 
        throw std::runtime_error("Empty list!");

    return head->data;
}

template<class T>
T& LinkedList<T>::front() {
    if(empty())
        throw std::runtime_error("Empty list!");
    
    return head->data;
}

template<class T>
const T& LinkedList<T>::back() const {
    if(empty()) 
        throw std::runtime_error("Empty list!");

    return tail->data;
}

template<class T>
T& LinkedList<T>::back() {
    if(empty()) 
        throw std::runtime_error("Empty list!");

    return tail->data;
}

template<class T>
LinkedList<T>::~LinkedList() {
    free();
}

#endif