-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha_Stack_linklist_withoutInheritence.cpp
More file actions
97 lines (84 loc) · 1.66 KB
/
a_Stack_linklist_withoutInheritence.cpp
File metadata and controls
97 lines (84 loc) · 1.66 KB
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
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
class Stack{
private:
struct node{
int item;
node *next;
};
node *top;
public:
Stack(){
top=NULL;
}
void push(int data){
node *t=new node;
t->item=data;
t->next=top;
top=t;
}
void pop(){
if(top==NULL){
cout<<"\n Underflow";
}
else{
node *t;
t=top;
top=t->next;
delete t;
}
}
int peek(){
if(top==NULL){
cout<<"\n Empty Stack";
return -1;
}
else{
return top->item;
}
}
~Stack(){
while(top!=NULL){
pop();
}
}
};
int driver(){
int choice;
cout<<"\n OPERATIONS : ";
cout<<"\n 1. PUSH " ;
cout<<"\n 2. POP ";
cout<<"\n 3. PEEK / to see topmost element ";
cout<<"\n PRESS ZERP FOR EXIST ";
cout<<"\n Enter your choice : ";
cin>>choice;
return choice;
}
int main(){
int data;
Stack obj;
while(1){
switch(driver()){
case 1:
cout<<"\n enter data : ";
cin>>data;
obj.push(data);
break;
case 2:
obj.pop();
break;
case 3:
cout<<obj.peek();
break;
case 0:
exit(0);
break;
default:
cout<<"Please enter valid choice , Thank you !";
break;
}
}
return 0;
}