-
Notifications
You must be signed in to change notification settings - Fork 0
/
deque.h
52 lines (50 loc) · 1.09 KB
/
deque.h
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
#pragma once
#include <iostream>
using namespace std;
template <class T>
class Deque
{
private:
struct Node
{
T data;
Node* prev;
Node* next;
Node(const T& d, Node* p = nullptr, Node* n = nullptr) : data(d), prev(p), next(n) {}
};
Node* head;
Node* tail;
int size;
public:
Deque() : head(nullptr), tail(nullptr), size(0) {}
~Deque()
{
Node* current = head;
while (current!= nullptr)
{
delete current;
current = current->next;
}
}
void push_front(const T& value);
void push_back(const T& value);
template <class... Args>
void emplace_back(Args&&... args)
{
if (size == 0) {
tail = new Node(std::forward<Args>(args)...);
head = tail;
}
else {
tail->next = new Node(std::forward<Args>(args)..., tail, nullptr);
tail = tail->next;
}
size++;
}
void pop_front();
void pop_back();
T front() const;
T back() const;
bool empty() const;
int getSize() const;
};