forked from Anirudh-1149/CS204-datastructures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.cpp
86 lines (73 loc) · 1.43 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
#include <bits/stdc++.h>
using namespace std;
class Queue
{
int front, rear, size;
unsigned capacity;
int* array;
public:
Queue(int capacity);
int Full();
int Empty();
void Enqueue(int item);
void Dequeue();
void Front();
void Rear();
};
Queue::Queue(int capacity)
{
this->capacity = capacity;
front = size = 0;
rear = capacity - 1;
array = new int[capacity];
}
int Queue::Full()
{ return (size == capacity); }
int Queue::Empty()
{ return (size == 0); }
void Queue::Enqueue(int item)
{
if (Full())
cout<<"Queue is Full";
else{
rear = (rear + 1) % capacity;
array[rear] = item;
size = size + 1;
cout << item << " enqueued to queue\n";
}
}
void Queue::Dequeue()
{
if (Empty())
cout<<"Queue is Empty\n";
else{
int item = array[front];
front = (front + 1) % capacity;
size = size - 1;
cout<<item<<" is Dequeued\n";
}
}
void Queue::Front()
{
if (Empty())
cout<<"Queue is Empty";
else{
cout<<array[front]<<"\n";
}
}
void Queue::Rear()
{
if (Empty())
cout<<"Queue is Empty";
else
{
cout<<array[rear]<<"\n";
}
}
int main()
{
Queue q(6);
q.Enqueue(1);
q.Front();
q.Rear();
}