-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack_using_1_queue.cpp
More file actions
82 lines (69 loc) · 1.58 KB
/
Stack_using_1_queue.cpp
File metadata and controls
82 lines (69 loc) · 1.58 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
#include <iostream>
using namespace std;
void push();
void pop();
void peek();
int q1[5],f=-1,rear=-1,c=0;
int main()
{
int i=0;
int choice;
cout<<("1. Push\n");
cout<<("2. Pop\n");
cout<<("3. Peek\n\n");
while(1)
{
cout<<("Enter Choice: ");
cin>>choice;
switch(choice)
{
case 1: push();break;
case 2: pop();break;
case 3: peek();break;
default : cout<<("Invalid Choice\n");
}
}
}
void push()
{
if(f==rear+1)
cout<<("Stack Full\n");
else if(f==-1 && rear==-1)
{
cout<<("Enter Element: ");
rear=(rear+1)%5;
f=(f+1)%5;
cin>>q1[rear];
c++;
}
else
{
cout<<("Enter Element: ");
rear=(rear+1)%5;
cin>>q1[rear];
c++;
}
}
void pop()
{
int i,data;
if(f==-1 && rear==-1)
cout<<("Stack Empty\n");
else
{
for(i=1;i<c;i++)
{
data=q1[f];
f=(f+1)%5;
rear=(rear+1)%5;
q1[rear]=data;
}
data=q1[f];
f=(f+1)%5;
cout<<"Element Deleted: "<<data<<endl;
c--;
}
}
void peek(){
cout<<q1[rear]<<endl;
}