-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStack.cpp
More file actions
100 lines (96 loc) · 1.69 KB
/
Stack.cpp
File metadata and controls
100 lines (96 loc) · 1.69 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
98
99
100
#include <iostream>
using namespace std;
class Node{
int data;
Node* next;
public:
Node(){
};
void sdata(int adata){
data=adata;
};
void snext(Node* anext){
next=anext;
};
int Data(){
return data;
};
Node* Next(){
return next;3
};
};
class Stacklist{
Node* top;
public:
Stacklist(){
};
Node* push(Node* top,int data);
Node* pop(Node* top);
void traverse(Node* top);
};
Node* Stacklist::push(Node* top,int data)
{
Node* temp = new Node();
temp->sdata(data);
temp->snext(top);
top = temp;
return top;
}
Node* Stacklist::pop(Node* top)
{
Node* temp;
if(top==NULL)
cout<<"Stack is empty\n";
else
{
temp=top;
cout<<"Element popped: "<<temp->Data()<<"\n";
top=top->Next();
delete(temp);
}
return top;
}
void Stacklist::traverse(Node* top)
{
Node* temp;
temp=top;
if(top==NULL)
cout<<"NULL\n";
else
{
cout<<"Stack elements are \n";
while(temp!=NULL)
{
cout<<temp->Data()<<"\n";
temp=temp->Next();
}
}
}
int main()
{
Stacklist stack;
int n=0,data;
Node* top=NULL;
while(n!=4){
cout<<"Options:\n[1] Push element into stack\n[2] Pop element from stack\n[3] Traverse the stack\n";
cout<<"[4] Exit\nEnter your choice\n";
cin>>n;
switch(n)
{
case 1:cout<<"Enter element to be pushed\n";
cin>>data;
top=stack.push(top,data);
break;
case 2:cout<<"Popping element....\n";
top=stack.pop(top);
break;
case 3:stack.traverse(top);
break;
case 4:cout<<"Exiting...\n";
break;
default:cout<<"Invalid Option\n";
break;
}
}
return 0;
}