forked from adityaharsh2001/assignments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstackusingLL.cpp
72 lines (67 loc) · 1.37 KB
/
stackusingLL.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
#include <iostream>
using namespace std;
struct Node{
int data;
Node *next;
};
class stack{
struct Node* top;
public:
stack(){
top=NULL;
}
void push(){
int value;
Node* ptr;
cout<<"\n Enter the no.. ";
cin>>value;
ptr=new Node;
ptr->data=value;
ptr->next=NULL;
if(top!=NULL)
ptr->next=top;
top=ptr;
}
void pop(){
Node* temp;
if(top == NULL){
cout<<"\n Empty stack";
return;
}
temp= top;
top=top->next;
cout<<"\n popped element is "<<temp->data;
delete temp;
}
void print(){
Node* temp1=top;
cout<<"\n The stack is ";
while (temp1!=NULL){
cout<<temp1->data<<" ";
temp1=temp1->next;
}
cout<<"\n";
}
};
int main() {
stack s;
int n;
do {
cout<<"\n 1. PUSH \n 2. POP \n 3. Print \n 4. Exit\n";
int choice;
cin>>choice;
switch(choice){
case 1: s.push();
s.print();
break;
case 2: s.pop();
break;
case 3: s.print();
break;
case 4: return 0;
break;
default: cout<<"\nWrong Choice";
}
} while(true);
return 0;
}