-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstack_linklist_implemented.cpp
107 lines (101 loc) · 1.4 KB
/
stack_linklist_implemented.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
/* Program to display list list implemented stack
made by : rakesh kumar
*/
#include<iostream>
#include<conio.h>
using namespace std;
struct node{
int data;
node *ptr;
};
class stack{
node *x,*y,*temp;
public:
stack(){
x=NULL;
}
void push();
void pop();
void show_data();
};
void stack::push(){
if(x==NULL)
{
x = new(node);
cout<<"Enter value :";
cin>>x->data;
x->ptr=NULL;
}
else
{
temp = new(node);
cout<<"Enter value :";
cin>>temp->data;
temp->ptr=x;
x= temp;
}
return;
}
void stack::pop(){
if(x==NULL)
{
cout<<"Stack empty";
getch();
}
else
{
temp = x;
x= x->ptr;
delete(temp);
}
return;
}
void stack::show_data()
{
if(x==NULL)
{
cout<<"Stack empty";
getch();
}
else
{
y = x;
while(y!=NULL)
{
cout<<y->data<<"\t";
y = y->ptr;
}
getch();
}
return;
}
int main(){
stack l;
int choice;
do{
system("cls");
cout<<"\n\n\n\t\t STACK MENU";
cout<<"\n\n\n\t\t1. Push";
cout<<"\n\n\n\t\t2. POP";
cout<<"\n\n\n\t\t3. Show contents";
cout<<"\n\n\n\t\t4. Exit";
cout<<"\n\n\n\n\t\t Enter your choice (1..4) :";
cin>>choice;
switch(choice)
{
case 1: l.push();
break;
case 2:
l.pop();
break;
case 3:
l.show_data();
break;
case 4:
break;
default:
cout<<"\n\n Wrong choice...Try again";
}
}while(choice!=4);
return 0;
}