-
Notifications
You must be signed in to change notification settings - Fork 0
/
deque.cpp
119 lines (110 loc) · 1.74 KB
/
deque.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
#include "deque.h"
using namespace std;
template <class T>
void Deque<T>::push_front(const T& value)
{
if (size == 0) {
head = new Node(value);
tail = head;
}
else {
head->prev = new Node(value, nullptr, head);
head = head->prev;
}
size++;
}
template <class T>
void Deque<T>::push_back(const T& value)
{
if (size == 0)
{
tail = new Node(value);
head = tail;
}
else
{
tail->next = new Node(value, tail, nullptr);
tail = tail->next;
}
size++;
}
template <class T>
void Deque<T>::pop_front()
{
if (size == 0)
{
cout << "Deque is empty" << endl;
}
else
{
Node* temp = head;
head = head->next;
delete temp;
if (head != nullptr)
{
head->prev = nullptr;
}
else
{
tail = nullptr;
}
size--;
}
}
template <class T>
void Deque<T>::pop_back()
{
if (size == 0)
{
cout << "Deque is empty" << endl;
}
else
{
Node* temp = tail;
tail = tail->prev;
delete temp;
if (tail != nullptr)
{
tail->next = nullptr;
}
else
{
head = nullptr;
}
size--;
}
}
template <class T>
T Deque<T>::front() const
{
if (size == 0)
{
throw "Deque is empty";
}
else
{
return head->data;
}
}
template <class T>
T Deque<T>::back() const
{
if (size == 0)
{
throw "Deque is empty";
}
else
{
return tail->data;
}
}
template <class T>
bool Deque<T>::empty() const
{
return size == 0;
}
template <class T>
int Deque<T>::getSize() const
{
return size;
}