-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqueue_using_array.cpp
More file actions
166 lines (128 loc) · 3.16 KB
/
queue_using_array.cpp
File metadata and controls
166 lines (128 loc) · 3.16 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
//# Data-Structures-And-Algorithms
//Here I will post my regular DSA problems
//Queue implementation using array
#include<iostream>
#include<conio.h>
#include<string.h>
#define n 5
using namespace std;
class queue{
int q[n];
int front;
int rear;
public:
queue()
{
front=-1; //Setting front and rear value to -1
rear=-1;
}
void enqueue () //Inserting value from rear
{
int x;
if(rear==n-1){
cout<<"Queue is Full"<<endl;
}
else if(front==-1&&rear==-1)
{
cout<<"Enter element: ";
cin>>x;
front=rear=0;
q[rear]=x;
}
else{
cout<<"Enter element: ";
cin>>x;
rear++;
q[rear]=x;
}
}
void dequeue () //Removing element from the front
{
if(front==-1&&rear==-1){
cout<<"Queue is empty"<<endl;
}
else if(front==rear){
cout<<"Dequeued Element: "<<q[front]<<endl;
front=rear=-1;
}
else{
cout<<"Dequeued Element: "<<q[front]<<endl;
front++;
}
}
void display () //Displaying the elements in the queue from front to rear
{
if(front==-1&&rear==-1){
cout<<"Queue is empty"<<endl;
}
else{
cout<<"Elements in Queue are: ";
for(int i=front;i<=rear;i++){
cout<<q[i]<<" ";
}
cout<<endl;
}
}
void peek() //Displaying the front element without removing it
{
if(front==-1&&rear==-1){
cout<<"Queue is empty"<<endl;
}
else{
cout<<"Top element : "<<q[front]<<endl;
}
}
void is_empty() //Check whether the queue is empty or not
{
if(front==-1&&rear==-1){
cout<<"Empty"<<endl;
}
else{
cout<<"Not empty"<<endl;
}
}
void is_full() //Check whether the queue is full or not
{
if(rear==n-1){
cout<<"Full"<<endl;
}
else{
cout<<"Not full"<<endl;
}
}
};
int main (){
class queue que;
int e,op;
char a;
do{
cout<<"Enter option:";
cin>>op;
switch(op){
case 1:
que.enqueue();
break;
case 2:
que.dequeue();
break;
case 3:
que.display();
break;
case 4:
que.peek();
break;
case 5:
que.is_empty();
break;
case 6:
que.is_full();
break;
default:
cout<<"Invalid choice"<<endl;
break;
}
cout<<"Continue? :";
cin>>a;
} while (a=='y');
return 0;
}