-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.hpp
85 lines (70 loc) · 993 Bytes
/
Stack.hpp
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
#include <iostream>
#include "Node.hpp"
using namespace std;
template <class T>
class Stack
{
int count;
Node<T> *top;
public:
Stack()
{
count = 0;
top = NULL;
}
void push(T data)
{
Node<T> *newNode = new Node<T>(data);
newNode->next = top;
top = newNode;
count += 1;
}
T pop()
{
T returnVal = NULL;
// If stack is empty
if (isEmpty())
{
cout << "Stack is empty. Cannot be pulled." << endl;
return returnVal;
}
else
{
returnVal = top->data;
Node<T> *newTop = top->next;
delete top;
top = newTop;
}
count -= 1;
return returnVal;
}
T peak()
{
if (isEmpty())
{
cout << "Stack is empty. Cannot be pulled." << endl;
}
return top->data;
}
int size()
{
return count;
}
bool isEmpty()
{
if (count == 0)
return true;
return false;
}
void toString()
{
cout << "{ ";
Node<T> *temp = top;
while (temp)
{
cout << temp->data << ", ";
temp = temp->next;
}
cout << " }" << endl;
}
};