-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.h
More file actions
136 lines (109 loc) · 2.65 KB
/
stack.h
File metadata and controls
136 lines (109 loc) · 2.65 KB
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
#ifndef _STACK_H_
#define _STACK_H_
#include <stdlib.h>
template <class type>
class stackCell {
private:
stackCell<type> *next;
stackCell<type> *prev;
type data;
public:
stackCell(type _data, stackCell<type> *_prev, stackCell<type> *_next);
~stackCell();
void destroy();
type& getData();
stackCell<type>* getNext() { return this->next; }
stackCell<type>* getPrev() { return this->prev; }
void setNext(stackCell<type>* _cell) { this->next = _cell; }
void setPrev(stackCell<type>* _cell) { this->prev = _cell; }
};
template <class type>
class stack {
private:
size_t size;
stackCell<type> *first;
stackCell<type> *last;
public:
stack();
~stack();
void push(type _object);
void pushBottom(type _object);
type pop();
type popBottom();
size_t getSize();
type& operator[](size_t _number);
};
// Implementation
template <class type>
stackCell<type>::stackCell(type _data, stackCell<type> *_next, stackCell<type> *_prev) {
this->data = _data;
this->next = _next;
this->prev = _prev;
}
template <class type>
stackCell<type>::~stackCell() {
if (this->next != NULL)
this->next->setPrev(NULL);
if (this->prev != NULL)
this->prev->setNext(NULL);
}
template <class type>
void stackCell<type>::destroy() {
if (this->prev != NULL)
this->prev->setNext(this->next);
if (this->next != NULL)
this->next->setPrev(this->prev);
}
template <class type>
type& stackCell<type>::getData() {
return this->data;
}
template <class type>
stack<type>::stack() {
this->size = 0;
this->first = NULL;
this->last = NULL;
}
template <class type>
stack<type>::~stack() {
// TODO
}
template <class type>
void stack<type>::push(type _object) {
this->last = new stackCell<type>(_object, this->last, NULL);
if (!this->size)
this->first = this->last;
this->size++;
}
template <class type>
void stack<type>::pushBottom(type _object) {
this->first = new stackCell<type>(_object, NULL, this->first);
this->size++;
}
template <class type>
type stack<type>::pop() {
type _temp = *this->last;
this->last = _temp.prev;
this->size--;
return _temp.getData();
}
template <class type>
type stack<type>::popBottom() {
type _temp = *this->first;
this->first = _temp.next;
this->size--;
return _temp.getData();
}
template <class type>
size_t stack<type>::getSize() {
return this->size;
}
template <class type>
type& stack<type>::operator[](size_t _number) {
bool exp = _number < (this->size - _number - 1);
stackCell<type> *iterator = exp ? this->first : this->last;
for (register size_t i(0); i < size_t(exp ? _number : (const size_t)this->size - _number - 1); i++)
iterator = exp ? iterator->getNext() : iterator->getPrev();
return iterator->getData();
}
#endif //_STACK_H_