-
Notifications
You must be signed in to change notification settings - Fork 18
/
Stacks_LLImplementation.cpp
113 lines (94 loc) · 2.07 KB
/
Stacks_LLImplementation.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <cstdlib>
using namespace std;
struct Node{ // Structure of the node
int val;
struct Node* next;
};
struct Node* top;
int ssize = 0; // Number of nodes in stack
bool isEmpty(){
return top == NULL;
}
void push(int val){
struct Node* temp;
temp = new Node();
if (!temp){
cout << "Stack Overflow!" << endl;
}
else{
temp->val = val;
temp->next = top;
top = temp;
cout << val << " pushed!" << endl;
ssize++;
}
}
void pop(){
struct Node* temp;
if (isEmpty()){
cout << "Stack Underflow!" << endl;
}
else{
temp = top;
int data = temp->val;
top = top->next;
temp->next = NULL;
free(temp);
cout << data << " popped!" << endl;
ssize--;
}
}
void StackTop(){
if (isEmpty()){
cout << "Stack Underflow!" << endl;
}
else{
cout << "Element at top: " << top->val << endl;
}
}
void StackSize(){
cout << "Stack Size: " << ssize <<endl;
}
int main(){
int q = 1;
while(q != 5){
cout << endl;
cout << "1. Push" << endl;
cout << "2. Pop" << endl;
cout << "3. Top" << endl;
cout << "4. Size" << endl;
cout << "5. Exit" << endl;
cout << "Enter your query: ";
cin >> q;
switch(q){
case 1:{
int val;
cout << "Enter the value: ";
cin >> val;
push(val);
break;
}
case 2:{
pop();
break;
}
case 3:{
StackTop();
break;
}
case 4:{
StackSize();
break;
}
case 5:{
exit(1);
break;
}
default:{
cout << "Enter a valid operation!" << endl;
}
}
}
return 0;
}