-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.h
79 lines (67 loc) · 1.32 KB
/
stack.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
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
#ifndef STACK_H
#define STACK_H
template <class type>
class StackNode
{
public:
type data;
StackNode *last;
};
template <class type> class Stack {
private:
long size;
StackNode <type>*head;
public:
Stack();
void push(type data);
type *pop(type &data);
type last();
bool isEmpty();
long getSize();
~Stack();
};
template <class type> Stack<type>::Stack()
{
this->size = 0;
this->head = nullptr;
}
template <class type> void Stack<type>::push(type data)
{
size++;
StackNode <type>*newNode = new StackNode<type>;
newNode->data = data;
newNode->last = this->head;
this->head = newNode;
}
template <class type> type *Stack<type>::pop(type &data)
{
size--;
data = head->data;
StackNode <type>*lastTemp = head->last;
delete head;
head = lastTemp;
return &data;
}
template <class type> type Stack<type>::last()
{
return (head->data)? head->data: nullptr;
}
template <class type> bool Stack<type>::isEmpty()
{
return this->size == 0;
}
template <class type> long Stack<type>::getSize()
{
return this->size;
}
template <class type> Stack<type>::~Stack()
{
StackNode<type> *pointer = this->head;
while(pointer)
{
StackNode<type> *temp = pointer;
delete temp;
pointer = pointer->last;
}
}
#endif // STACK_H