-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue_using_array.cpp
92 lines (83 loc) · 1.7 KB
/
queue_using_array.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
// enqueue and dequeue operations
// TC = O(1) for enqueue and O(n) fro dequeue
//SC = O(n)
#include <bits/stdc++.h>
using namespace std;
class Queue {
int rear;
int front;
int capacity;
int *queue;
public:
Queue(int capacity) {
this -> capacity = capacity;
rear = 0;
front = 0;
queue = new int[capacity];
}
~Queue() {
delete[] queue;
}
//insert element at rear of queue;
void qEnqueue(int x) {
if(rear == capacity) {
cout << "Queue is full"<< endl;
return;
}
else {
queue[rear] = x;
rear++;
}
}
// function to delete an element from front
void qDequeue() {
if(rear == front) {
cout << "Queue is empty" << endl;
return;
}
else{
//shift all elements to left by 1 position after removing one element
for(int i =0; i<rear -1 ; i++) {
queue[i] = queue[i + 1];
}
//decreament rear
rear--;
}
}
void qDisplay() {
if(front == rear) {
cout << "queue is empty" << endl;
return;
}
else {
//traverse front to rear
for(int i = front; i<rear; i++) {
cout << queue[i] << endl;
}
}
return;
}
void qFront() {
if(rear == front) {
cout << "queue is empty" <<endl;
return;
}
cout << "front element of queue: " << queue[front] <<endl;
}
};
int main() {
Queue q(4);
q.qDisplay();
q.qEnqueue(5);
q.qEnqueue(3);
q.qEnqueue(15);
q.qEnqueue(17);
//display queue
q.qDisplay();
//insert more than capacity
q.qFront();
q.qDequeue();
q.qFront();
q.qEnqueue(20);
q.qDisplay();
}