-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue using Array.cpp
118 lines (114 loc) · 1.57 KB
/
Queue using Array.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
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
#include<iostream>
using namespace std;
class queue
{
private:
int *head;
int *front;
int *rear;
int *temp;
int noofelements;
public:
queue()
{
head=new int[6];
front=rear=head;
noofelements=0;
}
void enqueue()
{
if(rear==head && noofelements==0)
{
cin>>*rear;
noofelements++;
return;
}
else if(rear==(head+5) && noofelements<=6)
{
rear=(head+0);
cin>>*rear;
noofelements++;
}
else if(rear!=(head+5) && noofelements<6)
{
rear++;
cout<<"enter value"<<endl;
cin>>*rear;
noofelements++;
return;
}
else if(noofelements>=6)
{
cout<<"Please Delete a Queue Element and then enqueue";
return;
}
}
void dequeue()
{
if(front==head && noofelements==0)
{
rear=head;
return;
}
else if(front==(head+5))
{
front=(head+0);
noofelements--;
}
else
{
front++;
--noofelements;
}
}
void print()
{
temp=front;
if(noofelements==0)
{
cout<<"Nothing to show";
}
else if(front==rear)
{
cout<<*rear;
}
else if(noofelements<=6)
{
do
{
cout<<*temp;
if(temp==(head+5))
{
temp=(head+0);
}
else
{
temp++;
}
}while(temp!=rear);
cout<<*rear;
}
}
};
int main()
{
queue q1;int opt;char sec;
cout<<"Welcome";
cout<<endl<<"Enter 1 For ENQueue ,2 for DeQueue , 3 for Print"<<endl;
do{
cin>>opt;
switch(opt)
{
case 1:
{q1.enqueue();break;}
case 2:
{q1.dequeue();break;}
case 3:
{q1.print();break;}
default:
{ cout<<"Error";break;}
}
cout<<"Enter Y to Retry N to exit";
cin>>sec;
}while(sec=='y' || sec=='Y');
}