-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamic_Stack.cpp
81 lines (79 loc) · 1.47 KB
/
dynamic_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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#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 is empty";
else
{
temp=top;
cout<<"popped value="<<temp->info;
top=top->next;
free(temp);
}
}
void display()
{
node*temp;
if(top==NULL)
cout<<"stack is empty";
else
{
temp=top;
while(temp!=NULL)
{
cout<<temp->info<<endl;
temp=temp->next;
}
}
}
int main()
{
node*top=NULL;
int num,choice;
while(true)
{
cout<<"\n******************* MENU ***********************"<<endl;
cout<<"1.Push"<<endl;
cout<<"2.Pop"<<endl;
cout<<"3.Display"<<endl;
cout<<"4.Exit"<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"enter element to push : ";
cin>>num;
push(num);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
cout<<"invalid input";
break;
}
}
}