-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Stacks.cpp
80 lines (63 loc) · 1.17 KB
/
Stacks.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
using namespace std;
struct Stack
{
int data;
Stack *next;
} *TOS = NULL;
void Push(int value)
{
Stack *new_node = new Stack();
new_node -> data = value;
new_node -> next = TOS;
TOS = new_node;
}
void Pop()
{
if(TOS == NULL)
cout << "Stack is empty" << endl;
else
{
Stack *temp;
temp = TOS;
TOS = TOS -> next;
temp -> next = NULL;
delete temp;
}
}
void Print_Stack()
{
if(TOS == NULL)
cout << "Stack is empty" << endl;
else
{
cout << "Top of stack is " << TOS -> data << endl;
if(TOS -> next != NULL)
{
cout << "Other Elements are : ";
Stack *current = TOS -> next;
while(current)
{
cout << current -> data << " ";
current = current -> next;
}
cout << endl;
}
}
}
int main()
{
for(int i = 0; i < 5; i++)
Push(i);
Print_Stack();
for(int i = 0; i < 6; i++)
Pop();
Print_Stack();
return 0;
}
/* Output
Top of stack is 4
Other Elements are : 3 2 1 0
Stack is empty
Stack is empty
*/