Website Summary Files Commits Branches @commit @diff @stats
path: root / Seminar03 / Linked-Queue.hpp
blob: 325f4c45f5726233c518884cf7230b4c5af07c3d (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
#ifndef LINKED_QUEUE_IMPL
#define LINKED_QUEUE_IMPL

#include<cstddef>

template<class T>
class LinkedQueue {
private:
    struct LinkedNode {
        T data;
        LinkedNode* next;

        LinkedNode(const T& argData, LinkedNode* argNext = nullptr) : data {argData}, next{argNext} {} 
    };

    LinkedNode* head;                   // Първия добавен елемент в опашката
    LinkedNode* tail;                   // Последния добавен елемент в опашката
    size_t queueSize;

    void copy(const LinkedQueue<T>&);
    void free();

public:
    LinkedQueue();
    
    LinkedQueue(const LinkedQueue<T>&);
    LinkedQueue& operator=(const LinkedQueue<T>&);

    LinkedQueue(LinkedQueue<T>&&) noexcept;
    LinkedQueue& operator=(LinkedQueue<T>&&) noexcept;

    void push(const T&);
    void pop();
    const T& front() const;

    bool empty() const;

    size_t size() const;

    ~LinkedQueue();
};

template<class T>
LinkedQueue<T>::LinkedQueue() : head {nullptr}, tail{nullptr}, queueSize{0} {}

template<class T>
LinkedQueue<T>::LinkedQueue(const LinkedQueue<T>& other) {
    copy(other);
}

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

    return *this;
}

template<class T>
LinkedQueue<T>::LinkedQueue(LinkedQueue<T>&& other) noexcept {
    head = other.head;
    tail = other.tail;
    size = other.size;

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

template<class T>
LinkedQueue<T>& LinkedQueue<T>::operator=(LinkedQueue<T>&& other) noexcept {
    if(this != &other) {
        head = other.head;
        tail = other.tail;
        size = other.size;

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

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

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

template<class T>
void LinkedQueue<T>::free() {
    LinkedNode* iter = head;
    while(iter) {
        LinkedNode* removeMe = iter;
        iter = iter->next;
        delete removeMe;
    }
}

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

template<class T>
size_t LinkedQueue<T>::size() const {
    return queueSize;
}

template<class T>
void LinkedQueue<T>::push(const T& elem) {
    LinkedNode* toPush = new LinkedNode(elem);
    if(empty()) {
        tail = toPush;
        head = toPush;
    }
    else {
        tail->next = toPush;
        tail = toPush;
    }

    ++queueSize;
}

template<class T>
void LinkedQueue<T>::pop() {
    if(empty()) {
        throw "Empty queue";
    }
    else if(head == tail) {
        delete head;
        head = nullptr;
        tail = nullptr;
    }
    else {
        LinkedNode* deleteMe = head;
        head = head->next;
        delete deleteMe;
    }

    --queueSize;
}

template<class T>
const T& LinkedQueue<T>::front() const {
    if(empty()){
        throw "Empty queue";
    }

    return head->data;
}

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

#endif