forked from raj-ravan/Hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.cpp
More file actions
99 lines (87 loc) · 1.61 KB
/
Queue.cpp
File metadata and controls
99 lines (87 loc) · 1.61 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
#include<iostream>
#include<stdlib.h>
using namespace std;
#define size 10
struct queue
{
int front;
int rear;
int a[size];
};
void display(struct queue *q)
{
if(q->rear == -1)
cout<<"Queue is Empty!!!";
else
{
for(int i=q->front; i<=q->rear; i++)
cout<<q->a[i]<<" ";
}
}
void enqueue(struct queue *q,int data)
{
if(q->rear == size-1)
cout<<"Queue is Full...!!"<<endl;
else
{
if(q->front == -1)
q->front = 0;
q->rear++;
q->a[q->rear] = data;
}
}
int dequeue(struct queue *q)
{
if ((q->front == -1 && q->rear == -1) || q->front > q->rear)
{
cout << "Queue is Empty ,Nothing to Dequeue" << endl;
return 0;
}
else if (q->front == q->rear)
{
int p = q->a[q->front];
q->front = -1;
q->rear = -1;
return p;
}
else
{
int p = q->a[q->front];
q->front++;
return p;
}
}
int main()
{
int d;
struct queue q;
int data,choice;
q.front = -1;
q.rear = -1;
cout<<"1)Enqueue the element\n2)Dequeue the element\n3)Exit"<<endl;
while(1)
{
cout<<"\nEnter your choice : "<<endl;
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter element to enqueue : ";
cin>>data;
enqueue(&q,data);
display(&q);
break;
case 2:
d = dequeue(&q);
cout<<d<<" is dequed from the queue."<<endl;
display(&q);
break;
case 3:
exit(0);
break;
default:
cout<<"Invalid choice...!!";
}
}
return 0;
}