#include <iostream>
#define MAX 5
using namespace std;
/*
* Class Circular Queue
*/
class Circular_Queue
{
private:
int *cqueue_arr;
int front, rear;
public:
Circular_Queue()
{
cqueue_arr = new int [MAX];
rear = front = -1;
}
/*
* Insert into Circular Queue
*/
void push(int x)
{
if ((front == 0 && rear == MAX-1) || (front == rear+1))
{
cout<<"Overflow \n";
return;
}
if (front == -1)
{
front = 0;
rear = 0;
}
else
{
if (rear == MAX - 1)
rear = 0;
else
rear = rear + 1;
}
cqueue_arr[rear] = x ;
}
/*
* Delete from Circular Queue
*/
void del()
{
if (front == -1)
{
cout<<"Underflow\n";
return ;
}
if (front == rear)
{
front = -1;
rear = -1;
}
else
{
if (front == MAX - 1)
front = 0;
else
front = front + 1;
}
}
/*
* Display Circular Queue
*/
void display()
{
int front_pos = front, rear_pos = rear;
if (front == -1)
{
cout<<"Queue is empty\n";
return;
}
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++;
}
}
cout<<cqueue_arr[front]<<"->"<<endl;
}
};
/*
* Main
*/
int main()
{
int choice, x;
Circular_Queue cq;
do
{
cin>>choice;
switch(choice)
{
case 0: break;
case 1:
cin>>x;
cq.push(x);
break;
case 2:
cq.del();
break;
case 3:
cq.display();
break;
default:
cout<<"Wrong choice\n";
}
}
while(choice != 0);
return 0;
}
#define MAX 5
using namespace std;
/*
* Class Circular Queue
*/
class Circular_Queue
{
private:
int *cqueue_arr;
int front, rear;
public:
Circular_Queue()
{
cqueue_arr = new int [MAX];
rear = front = -1;
}
/*
* Insert into Circular Queue
*/
void push(int x)
{
if ((front == 0 && rear == MAX-1) || (front == rear+1))
{
cout<<"Overflow \n";
return;
}
if (front == -1)
{
front = 0;
rear = 0;
}
else
{
if (rear == MAX - 1)
rear = 0;
else
rear = rear + 1;
}
cqueue_arr[rear] = x ;
}
/*
* Delete from Circular Queue
*/
void del()
{
if (front == -1)
{
cout<<"Underflow\n";
return ;
}
if (front == rear)
{
front = -1;
rear = -1;
}
else
{
if (front == MAX - 1)
front = 0;
else
front = front + 1;
}
}
/*
* Display Circular Queue
*/
void display()
{
int front_pos = front, rear_pos = rear;
if (front == -1)
{
cout<<"Queue is empty\n";
return;
}
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++;
}
}
cout<<cqueue_arr[front]<<"->"<<endl;
}
};
/*
* Main
*/
int main()
{
int choice, x;
Circular_Queue cq;
do
{
cin>>choice;
switch(choice)
{
case 0: break;
case 1:
cin>>x;
cq.push(x);
break;
case 2:
cq.del();
break;
case 3:
cq.display();
break;
default:
cout<<"Wrong choice\n";
}
}
while(choice != 0);
return 0;
}
No comments:
Post a Comment