-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamicStack.cpp
83 lines (79 loc) · 1.46 KB
/
DynamicStack.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
// Dynamic Stack Implementation :
#include<iostream>
using namespace std;
struct stack
{
int info;
struct stack *next;
};
typedef struct stack node;
node *top;
void push(int num)
{
node *newnode;
newnode = (node*)malloc(sizeof(node));
newnode->info = num;
newnode->next = top;
top = newnode;
}
void pop()
{
node *temp;
if(top == NULL)
cout<<"Stack Underflow"<<endl;
else
{
temp = top;
top = top->next;
free(temp);
}
}
void display()
{
node *temp;
if(top == NULL)
cout<<"Stack Underflow"<<endl;
else
{
temp = top;
while(temp != NULL)
{
cout<<temp->info<<endl;
temp = temp->next;
}
}
}
int main()
{
int choice, num;
top = NULL;
while(true)
{
cout<<"MENU"<<endl;
cout<<"1. Push"<<endl;
cout<<"2. Pop"<<endl;
cout<<"3. Display"<<endl;
cout<<"4. Exit"<<endl;
cout<<"Choose an Option :"<<endl;
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter number :"<<endl;
cin>>num;
push(num);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
cout<<"Invalid Input"<<endl;
}
}
return 0;
}