-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patharray_imple_of_queue.cpp
88 lines (83 loc) · 1.58 KB
/
array_imple_of_queue.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
#include<iostream>
# define MAX_SIZE 101
int A[MAX_SIZE];
int front=-1;
int rear=-1;
using namespace std;
bool isEmpty(){
if(front== -1 && rear==-1)
return true;
else
return false;
}
//Using concept of circular array so as to reuse the cells of array..
void Enqueue(int x)
{
if((rear+1)%MAX_SIZE==front)
{
std::cout<<"Cannot enter more no"<<"\n";
return;
}
else if (isEmpty())
{
front=0;
rear=0;
A[rear]=x;
}
else
{
rear=(rear+1)%MAX_SIZE;
A[rear]=x;
}
}
void Dequeue( )
{
if (isEmpty())
{ std::cout<<"Cannot remove element from a vaccant queue"<<"\n";
return ;
}
else if(front==rear)
{
front=-1;
rear=-1;
std::cout<<"Cannot remove element from a vaccant queue"<<"\n";
}
else{
front=(front+1)%MAX_SIZE;
}
}
int Front()
{
if(front == -1)
{
std::cout<<"Error: cannot return front from empty queue\n";
return -1 ;
}
return A[front];
}
void print(){
int i;
std::cout<<"The queue is currently"<<"\n";
for(i=0;i<(((rear-front+MAX_SIZE)%MAX_SIZE)+1);i++){
std::cout<<A[((front+i)%MAX_SIZE)]<<" ";
}
std::cout<<"\n";
}
int main(){
int top;
bool isempty;
Enqueue(2);
print();
Enqueue(5);
print();
Enqueue(10);
print();
Dequeue();
print();
Enqueue(12);
print();
top= Front();
std::cout<<top<<"\n";
isempty=isEmpty();
std::cout<<isempty<<"\n";
}