C++ program to implement Queue using Formula Based Representation

#include <iostream.h>
#include <conio.h>
class queue
{
private :
 
int *arr ;
int front, rear ;
int MAX ;
public :
queue( int maxsize = 10 ) ;
void addq ( int item ) ;
int delq( ) ;
} ;
queue :: queue( int maxsize )
{
MAX = maxsize ;
arr = new int [ MAX ];
front = -1 ;
rear = -1 ;
}
void queue :: addq ( int item )
{
if ( rear == MAX - 1 )
{
cout << "\nQueue is full" ;
return ;
}
rear++ ;
arr[rear] = item ;
if ( front == -1 )
front = 0 ;
}
int queue :: delq( )
{
int data ;
 
if ( front == -1 )
{
cout << "\nQueue is Empty" ;
return NULL ;
}
 
data = arr[front] ;
arr[front] = 0 ;
if ( front == rear )
front = rear = -1 ;
else
front++ ;
 
return  data ;
}
void main( )
{
queue a (10 ) ;
clrscr();
a.addq ( 23 ) ;
a.addq ( 9 ) ;
a.addq ( 11 ) ;
a.addq ( -10 ) ;
a.addq ( 25 ) ;
a.addq ( 16 ) ;
a.addq ( 17 ) ;
a.addq ( 22 ) ;
a.addq ( 19 ) ;
a.addq ( 30 ) ;
a.addq ( 32 ) ;
int i = a.delq( ) ;
cout << "\nItem deleted: " << i ;
i = a.delq( ) ;
cout << "\nItem deleted: " << i ;
i = a.delq( ) ;
cout << "\nItem deleted: " << i ;
}
Editorial Team
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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in