-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathqueue.cpp
More file actions
83 lines (68 loc) · 1.38 KB
/
queue.cpp
File metadata and controls
83 lines (68 loc) · 1.38 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
#include<bits/stdc++.h>
#define SIZE 100 //default stack size
using namespace std;
//Queue implementation
class Queue {
int *arr; //pointer to queue
int front,rear,size,capacity;
public:
Queue(int s = SIZE);
~Queue();
void enqueue(int x);
int dequeue();
int qsize();
bool isEmpty();
bool isFull();
};
//Constructor for class Queue initialized with size of queue
Queue::Queue(int s) {
arr = new int[s];
capacity = s;
front = 0;
rear = -1;
size = 0;
}
//destructor for class Queue
Queue::~Queue() {
delete arr;
}
//function to push element into queue
void Queue::enqueue(int x) {
if(isFull()) {
cout<<"Queue Overflow!!!"<<endl;
}
else {
cout<<"Inserting "<<x<<" into the queue..."<<endl;
size++;
rear = (rear+1)%capacity;
arr[rear]=x;
}
}
//function to remove element from queue
int Queue::dequeue() {
if(isEmpty()) {
cout<<"Queue Empty!!!"<<endl;
}
else {
cout<<"Removing "<<arr[front]<<" from the queue..."<<endl;
int temp = arr[front];
size--;
front = (front+1)%capacity;
return temp;
}
}
//function to return size of queue
int Queue::qsize() {
return size;
}
//function to check if queue is empty or not
bool Queue::isEmpty() {
return(size==0);
}
//function to check if queue is full or not
bool Queue::isFull() {
return(size==capacity);
}
int main() {
return 0;
}