#include <iostream.h>
class cqueue
{
private :
int *arr ;
int front, rear ;
int MAX;
public :
cqueue( int maxsize = 10 ) ;
void addq ( int item ) ;
int delq( ) ;
void display( ) ;
} ;
cqueue :: cqueue( int maxsize )
{
MAX = maxsize ;
arr = new int [ MAX ];
front = rear = -1 ;
for ( int i = 0 ; i < MAX ; i++ )
arr[i] = 0 ;
}
void cqueue :: addq ( int item )
{
if ( ( rear + 1 ) % MAX == front )
{
cout << "\nQueue is full" ;
return ;
}
rear = ( rear + 1 ) % MAX;
arr[rear] = item ;
if ( front == -1 )
front = 0 ;
}
int cqueue :: delq( )
{
int data ;
if ( front == -1 )
{
cout << "\nQueue is empty" ;
return NULL ;
}
data = arr[front] ;
arr[front] = 0 ;
if ( front == rear )
{
front = -1 ;
rear = -1 ;
}
else
front = ( front + 1 ) % MAX;
return data ;
}
void cqueue :: display( )
{
cout << endl ;
for ( int i = 0 ; i < MAX ; i++ )
cout << arr[i] << " " ;
cout << endl ;
}
void main( )
{
cqueue a ( 10 ) ;
a.addq ( 14 ) ;
a.addq ( 22 ) ;
a.addq ( 13 ) ;
a.addq ( -6 ) ;
a.addq ( 25 ) ;
cout << "\nElements in the circular queue: " ;
a.display( ) ;
int i = a.delq( ) ;
cout << "Item deleted: " << i ;
i = a.delq( ) ;
cout << "\nItem deleted: " << i ;
cout << "\nElements in the circular queue after deletion: " ;
a.display( ) ;
a.addq ( 21 ) ;
a.addq ( 17 ) ;
a.addq ( 18 ) ;
a.addq ( 9 ) ;
a.addq ( 20 ) ;
cout << "Elements in the circular queue after addition: " ;
a.display( ) ;
a.addq ( 32 ) ;
cout << "Elements in the circular queue after addition: " ;
a.display( ) ;
} |
#include <iostream.h>
class cqueue
{
private :
int *arr ;
int front, rear ;
int MAX;
public :
cqueue( int maxsize = 10 ) ;
void addq ( int item ) ;
int delq( ) ;
void display( ) ;
} ;
cqueue :: cqueue( int maxsize )
{
MAX = maxsize ;
arr = new int [ MAX ];
front = rear = -1 ;
for ( int i = 0 ; i < MAX ; i++ )
arr[i] = 0 ;
}
void cqueue :: addq ( int item )
{
if ( ( rear + 1 ) % MAX == front )
{
cout << "\nQueue is full" ;
return ;
}
rear = ( rear + 1 ) % MAX;
arr[rear] = item ;
if ( front == -1 )
front = 0 ;
}
int cqueue :: delq( )
{
int data ;
if ( front == -1 )
{
cout << "\nQueue is empty" ;
return NULL ;
}
data = arr[front] ;
arr[front] = 0 ;
if ( front == rear )
{
front = -1 ;
rear = -1 ;
}
else
front = ( front + 1 ) % MAX;
return data ;
}
void cqueue :: display( )
{
cout << endl ;
for ( int i = 0 ; i < MAX ; i++ )
cout << arr[i] << " " ;
cout << endl ;
}
void main( )
{
cqueue a ( 10 ) ;
a.addq ( 14 ) ;
a.addq ( 22 ) ;
a.addq ( 13 ) ;
a.addq ( -6 ) ;
a.addq ( 25 ) ;
cout << "\nElements in the circular queue: " ;
a.display( ) ;
int i = a.delq( ) ;
cout << "Item deleted: " << i ;
i = a.delq( ) ;
cout << "\nItem deleted: " << i ;
cout << "\nElements in the circular queue after deletion: " ;
a.display( ) ;
a.addq ( 21 ) ;
a.addq ( 17 ) ;
a.addq ( 18 ) ;
a.addq ( 9 ) ;
a.addq ( 20 ) ;
cout << "Elements in the circular queue after addition: " ;
a.display( ) ;
a.addq ( 32 ) ;
cout << "Elements in the circular queue after addition: " ;
a.display( ) ;
}
Editorial Team
We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.
this code is wrong it doesn’t move the item inside it
bkwass mariiii hoi haiiiiiiiiiiiiiiiiii
/*
* C++ Program to Implement Circular Queue
*/
#include
#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 insert(int item)
{
if ((front == 0 && rear == MAX-1) || (front == rear+1))
{
cout<<"Queue Overflow \n";
return;
}
if (front == -1)
{
front = 0;
rear = 0;
}
else
{
if (rear == MAX – 1)
rear = 0;
else
rear = rear + 1;
}
cqueue_arr[rear] = item ;
}
/*
* Delete from Circular Queue
*/
void del()
{
if (front == -1)
{
cout<<"Queue Underflow\n";
return ;
}
cout<<"Element deleted from queue is : "<<cqueue_arr[front]<<endl;
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;
}
cout<<"Queue 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++;
}
}
cout<<endl;
}
};
/*
* Main
*/
int main()
{
int choice, item;
Circular_Queue cq;
do
{
cout<<"1.Insert\n";
cout<<"2.Delete\n";
cout<<"3.Display\n";
cout<<"4.Quit\n";
cout<>choice;
switch(choice)
{
case 1:
cout<>item;
cq.insert(item);
break;
case 2:
cq.del();
break;
case 3:
cq.display();
break;
case 4:
break;
default:
cout<<"Wrong choice\n";
}/*End of switch*/
}
while(choice != 4);
return 0;
}
I Hope , This Code will Help you!!! 🙂
the above code is correct 100%. just one mistake there , in void main( ). type int main () . then program will run correctly.. i run it .. it works with all operation like is Full , is Empty , Dequeue and enqueue etc.. it works all .. in c-Free or Dev
in above comment i read that there is no concept of Full… but i run it There is concept of IS FULL… just type Int in place of void and run it,,,
in above comment i read that there is no concept of Full… but i run it There is concept of IS FULL… just type Int in place of void and run it,,,