-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.h
65 lines (51 loc) · 1.22 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
#ifndef STACK_H
#define STACK_H
namespace de300 {
template <class T> class Stack {
private:
class Item {
public:
T val;
Item* next;
Item(T v) : val(v), next(0) {}
};
Item* head;
public :
Stack() : head(0) {}
~Stack();
void pop();
void push(T val);
T getHead();
};
template <class T>
Stack<T>::~Stack() {
//deallocate everything
Item *current;
current = head;
while (current != 0) {
Item *temp = current;
current = temp->next;
delete temp;
}
}
template <class T>
void Stack<T>::pop() {
//remove top element and deallocate
Item *cull = head;
head = head->next;
delete cull;
}
template <class T>
void Stack<T>::push(T val) {
//add a new element to the top of the stack
Item *newItem = new Item(val);
newItem->next = head;
head = newItem;
}
template <class T>
T Stack<T>::getHead() {
//return the value of the top element
return head->val;
}
}
#endif