-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha_Queue_Array.cpp
More file actions
141 lines (123 loc) · 2.88 KB
/
a_Queue_Array.cpp
File metadata and controls
141 lines (123 loc) · 2.88 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
class Queue{
private:
int capacity;
int front,rear;
int *ptr;
public:
Queue(){
capacity=0;
front=-1;
rear=-1;
ptr=NULL;
}
void createQueue(int cap){
capacity=cap;
front=-1;
rear=-1;
ptr=new int[cap];
}
void Insertion(int data){
if(ptr==NULL){
cout<<"\n Invalid Operation ";
}
else if(front==0 && rear==capacity-1 || rear+1==front){ // when queue is full
cout<<"\n Overflow or Queue if Full";
}
else if(rear+1==capacity){ // when rear is at lastindex but front is not at 0
rear=0;
ptr[rear]=data;
}
else{
rear++;
ptr[rear]=data;
}
}
void Deletion(){
if(ptr==NULL){
cout<<"\n Queue does not exits or Invalid Operation ";
}
else if(rear==-1){
cout<<"\n Empty Queue !";
}
else if(front==rear){ // when queue having only one item in it
front=-1;
rear=-1;
// delete []ptr; --> no need of this
}
else if(front==capacity-1){
front=0;
}
else{
front++;
}
}
int frontElementView(){
if(rear==-1){
cout<<"\n UnderFlow ! ";
}
return ptr[front];
}
int RearElementView(){
if(rear==-1){
cout<<"\n UnderFlow ! ";
}
return ptr[rear];
}
~Queue(){
while(front!=-1){
Deletion();
}
}
};
int driver(){
int choice;
cout<<"\n OPERATIONS : ";
cout<<"\n 1.Create a queue ";
cout<<"\n 2.Insertion ";
cout<<"\n 3. Deletion";
cout<<"\n 4. To view Front Element in Queue";
cout<<"\n 5. To view Last Element in Queue";
cout<<"\n Press 0 for exit ";
cout<<"\n Please Enter your choice : ";
cin>>choice;
return choice;
}
int main(){
int capacity,data;
Queue obj;
while(1){
switch (driver())
{
case 1:
cout<<"\n Enter capacity : ";
cin>>capacity;
obj.createQueue(capacity);
break;
case 2:
cout<<"\n Enter data : ";
cin>>data;
obj.Insertion(data);
break;
case 3:
obj.Deletion();
break;
case 4:
cout<<obj.frontElementView();
break;
case 5:
cout<<obj.RearElementView();
break;
case 0:
exit(0);
break;
default:
cout<<"Please enter valid choice ,thank you !";
break;
}
}
return 0;
}