-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircularQ2.cpp
More file actions
146 lines (145 loc) · 2.69 KB
/
circularQ2.cpp
File metadata and controls
146 lines (145 loc) · 2.69 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
142
143
144
145
146
#include<conio.h>
#include<process.h>
#include<iostream>
#define MAX 50
using namespace std;
//A class is created for the circular queue
class circular_queue
{
int cqueue_arr[MAX];
int front,rear;
public:
//a constructor is created to initialize the variables
circular_queue(){
front=rear=-1;
}
//public function declarations
void insert();
void del();
void display();
};
//Function to insert an element to the circular queue
void circular_queue::insert()
{
int added_item;
//Checking for overflow condition
if ((front == 0 && rear == MAX-1) || (front == rear +1))
{
cout<<"Overflow";
//return;
}
if (front==-1) /*If queue is empty */
{
front = 0;
rear = 0;
}
else
if (rear == MAX-1)/*rear is at last position of queue */
rear = 0;
else
rear = rear + 1;
cout<<"\nInput the element for insertion in queue:";
cin>>added_item;
cqueue_arr[rear] = added_item;
}/*End of insert()*/
//This function will delete an element from the queue
void circular_queue::del()
{
//Checking for queue underflow
if (front==-1)
{
cout<<"\nQueue Underflow\n";
return;
}
cout<<"\nElement deleted from queue is:"<<cqueue_arr[front]<<endl;
if (front == rear) /* queue has only one element */
{
front=rear=-1;
}
else
if(front == MAX-1)
front = 0;
else
front = front + 1;
}/*End of del()*/
//Function to display the elements in the queue
void circular_queue::display(){
int front_pos = front,rear_pos = rear;
if(front_pos==-1)
cout<<"\n\tQueue is Empty.";
else{
for(int i=front_pos;i<=rear_pos;i++){
cout<<cqueue_arr[i]<<" : \t";
}
}
}
/*void circular_queue::display()
{
int front_pos = front,rear_pos = rear;
//Checking whether the circular queue is empty or not
if (front==-1)
{
cout<<"\nQueue is empty\n";
return;
}
//Displaying the queue elements
cout<<"\nQueue elements:\n";
if(front_pos <= rear_pos )
while(front_pos <= rear_pos)
{
cout<<cqueue_arr[front_pos]<<", ";
front_pos++;
}
else
{
while(front_pos <= MAX-1)
{
cout<<cqueue_arr[front_pos]<<", ";
front_pos++;
}
front_pos = 0;
while(front_pos <= rear_pos)
{
cout<<cqueue_arr[front_pos]<<", ";
front_pos++;
}
}/*End of else*/
//cout<<"\n";
//}/*End of display() */
int main()
{
int choice;
//Creating the objects for the class
circular_queue co;
while(1)
{
//clrscr();
//Menu options
cout <<"\n1.Insert\n";
cout <<"2.Delete\n";
cout <<"3.Display\n";
cout <<"4.Quit\n";
cout <<"\nEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
co.insert();
break;
case 2 :
co.del();
getch();
break;
case 3:
co.display();
getch();
break;
case 4:
exit(1);
default:
cout<<"\nWrong choice\n";
getch();
}/*End of switch*/
}/*End of while*/
return 0;
}/*End of main()*/