forked from anurag1802/Data-Structure-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue1.cpp
81 lines (78 loc) · 1.23 KB
/
queue1.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
#include<iostream>
#define size 5
using namespace std;
int front=-1,rear=-1,queue[size];
//enqueue function declaration
int enqueue(int x)
{
if(rear==size-1)
{
cout<<"Queue Overflow!!!"<<endl;
}
else if(front==-1 && rear==-1)
{
front=rear=0;
queue[rear]=x;
cout<<"your element "<<x<<" is enqueued"<<endl;
}
else
{
rear++;
queue[rear]=x;
cout<<"your element "<<x<<" is enqueued"<<endl;
}
};
//dequeue function declaration
void dequeue(void)
{
if(front==-1 && rear==-1)
{
cout<<"Queue is empty!!!"<<endl;
}
else if(front==rear)
{
cout<<"your element "<<queue[front]<<" is deleted"<<endl;
front=rear=-1;
}
else
{
cout<<"your element "<<queue[front]<<" is deleted"<<endl;
front++;
}
}
//display function
int display (void)
{
int i;
cout<<"Display ->";
if(front==-1 && rear==-1)
{
cout<<"queue empty\n";
}
else for(i=front; i<=rear; i++)
{
cout<<"\t"<<queue[i];
}
}
int peek (void)
{
if(front==-1 && rear==-1)
{
cout<<"queue empty"<<endl;
}
else
{
cout<<"data at front = "<<queue[front]<<endl;
}
}
int main()
{
enqueue(10);
enqueue(20);
enqueue(30);
dequeue();
dequeue();
peek();
display();
return 0;
}