forked from raolokesh126/hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.cpp
96 lines (94 loc) · 1.56 KB
/
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
89
90
91
92
93
94
95
96
#include<bits/stdc++.h>
using namespace std;
class Queue{
public:
//properties
int rear;
int front;
int size;
int *arr;
//behaviours
//making constructor
Queue()
{
rear=-1;
front=-1;
size=5;
arr=new int[size];
}
void enqueue(int x)
{
if(rear==size-1) //Queue is not empty
{
cout<<"queue is full for:"<< x <<endl;
}
else //queue if full
{
rear++;
arr[rear]=x;
}
}
void dequeue()
{
if(front==rear) //either for (front=rear=-1) and for another index also
{
cout<<"queue is empty!"<<endl;
}
front++;
}
int Front()
{
if(front==rear)
{
cout<<"queue is empty!"<<endl;
return -1;
}
else
{
return arr[front];
}
}
bool isEmpty()
{
if(front==rear)
return true;
return false;
}
bool isFull()
{
if(rear==size-1)
return true;
return false;
}
void print()
{
if(front==rear)
{
cout<<"queue is empty!"<<endl;
}
int tmp=front;
while (tmp!=rear)
{
cout<<arr[tmp]<<" ";
tmp++;
}
}
};
int main()
{
//follow codewith hary vedio for references
Queue q;
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
q.enqueue(4);
q.enqueue(5);
q.enqueue(6);
q.dequeue();
cout<<"queue front element is: "<<q.Front()<<endl;
cout<<"queue is: ";
q.print();
cout<<"is queue empty: "<<q.isEmpty()<<endl;
cout<<"is queue is full: "<<q.isFull()<<endl;
return 0;
}