-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.cpp
58 lines (52 loc) · 806 Bytes
/
stack.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
#include "node.cpp"
using namespace std;
template <typename T>
struct stack
{
node<T>* top;
int length;
stack()
{
top = new node<T>();
length = 0;
}
void push(node<T>* input)
{
input->next = top->next;
top->next = input;
length++;
}
void push(T input)
{
push(new node<T>(input));
}
node<T>* pop()
{
node<T>* return_node;
if (top->next != nullptr)
return_node = top->next;
else
return new node<T>(-1);
if (length > 1)
top->next = top->next->next;
else
top->next = nullptr;
length--;
return return_node;
}
void print()
{
node<T>* current = top;
if (length == 0)
{
cout << "stack is empty" << endl;
return;
}
while (current->next != nullptr)
{
current = current->next;
cout << current->value << " ";
}
cout << endl;
}
};