-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlab_DS3_stack.cpp
More file actions
169 lines (146 loc) · 2.4 KB
/
lab_DS3_stack.cpp
File metadata and controls
169 lines (146 loc) · 2.4 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node* next=NULL;
};
class stck
{
public:
node* head=NULL;
int max_size=-1;
int size=0;
stck(int num)
{
max_size=num;
}
stck()
{
}
int pop()
{
if(isempty()==true)
{
cout<<"stack is empty\n";
return INT_MIN;
}
else
{
node* temp=head;
if(temp->next==NULL)
{
int r=head->data;
free(head);
head=NULL;
size--;
return r;
}
else
{
int r=head->data;
node *temp=head->next;
free(head);
head=temp;
return r;
}
}
}
void push(int data)
{
if(isfull()==true&&max_size!=-1)
{
cout<< "stack is already full\n";
}
else if(head==NULL)
{
node* q=new node;
q->data=data;
q->next=NULL;
head=q;
}
else
{
node* q=new node;
q->data=data;
q->next=NULL;
node* temp=head;
head=q;
q->next=temp;
}
size++;
}
bool isempty()
{
if(head==NULL)
return true;
else
return false;
}
bool isfull()
{
if(size==max_size)
return true;
else
return false;
}
void print_stack()
{
node* temp= head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<"\n";
}
};
int main()
{
char p;
cout<<"you want to decide a maximum size for stack or not? if yeas then type y if no then type n\n";
cin>>p;
if(p=='y')
{
cout<<"enter size:";
int size;
cin>>size;
stck s1(size);
cout<<" encoding is \n 1 push \n 2 pop \n 3 print_que \n 4 isempty \n 5 isfull \n -1 exit enter the option u want to do \n ";
while(1)
{
int r;
cin>>r;
if(r==-1) break;
switch(r)
{
case 1: cout<<"enter element:" ; int e; cin>>e; s1.push(e);break;
case 2: cout<<s1.pop()<<"\n";break;
case 3: s1.print_stack();break;
case 4: if(s1.isempty()==true) cout<<"yes empty\n"; else cout<<"no\n" ;break;
case 5: if(s1.isfull()==true) cout<<"yes full\n"; else cout<<"no\n";break;
case 6: break;
}
}
}
else
{
stck s1;
cout<<" encoding is \n 1 push \n 2 pop \n 3 print_stack \n 4 isempty \n 5 isfull \n -1 exit enter the option u want to do \n";
while(1)
{
int r;
cin>>r;
if(r==-1) break;
switch(r)
{
case 1: cout<<"enter element:" ; int e; cin>>e; s1.push(e);break;
case 2: cout<<s1.pop()<<"\n";break;
case 3: s1.print_stack();break;
case 4: if(s1.isempty()==true) cout<<"yes\n"; else cout<<"no\n" ;break;
case 5: if(s1.isfull()==true) cout<<"yes full\n"; else cout<<"no\n";break;
case 6: break;
}
}
}
}