Asked in
Database Programming
C Programming
C++ Programming

Write a program in C language to implement the insertion and deletion operations in a circular queue?

Answer

User Avatar
Wiki User
December 05, 2010 3:29PM

ARRAY-BASED CIRCULAR QUEUE

#include<iostream.h>

#include<iomanip.h>

class person

{

public:

int arr_tym, trans_tym, wait_tym, deprt_tym;

};

class queue

{

private:

person data[5];

int front , back , count;

public:

queue()

{

front=0;

back=0;

count=0;

}

void Front()

{

cout<<" \n The arrival time of the first customer is :"<<data[front].arr_tym<<endl;

cout<<" \n And the transaction time of the first customer is :"<<data[front].trans_tym<<endl;

}

void enqueue(int a_tym, int t_tym)

{

if(count==5)

cout<<"\n The queue is full. \n";

else if(count<5)

{

data[back].arr_tym=a_tym;

data[back].trans_tym=t_tym;

count++;

if(back==4)

back=0;

else if(back<4)

back++;

}

Front();

}

void dequeue()

{

if(count==0)

cout<<"\n The queue is already empty. \n";

else if(count>0)

{

data[front].arr_tym=0;

data[front].trans_tym=0;

if(front<4)

front+=1;

else if(front==4)

front=0;

cout<<"\n The element has been deleted. \n";

count--;

Front();

}

}

void Is_Empty()

{

if(count==0)

cout<<"\n The queue is empty. \n";

else if(count<5&&count>0)

cout<<"\n The queue has "<<count<<" elements. \n";

else if(count==5)

cout<<"\n The queue is full. \n";

}

void cal_w8_tym()

{

for(int i=front;i<5;i++)

{

if(i==1)

{

data[i].deprt_tym=data[i].arr_tym+data[i].trans_tym;

data[i].wait_tym=0;

}

else

{

if(data[i].deprt_tym<data[i].arr_tym)

{

data[i].deprt_tym=data[i].arr_tym+data[i].trans_tym;

data[i].wait_tym=0;

}

else

{

data[i].deprt_tym=data[i-1].deprt_tym+data[i].trans_tym;

data[i].wait_tym=data[i-1].deprt_tym-data[i].arr_tym;

}

}

}

}

void display()

{

cout<<"\n\tARRIVAL TIME\tTRANSACTION TIME\tWAITING TIME\tDEPARTING TIME\n";

for(int i=front;i<5;i++)

{

cout<<"\n\t"<<setw(12)<<data[i].arr_tym;

cout<<"\t"<<setw(16)<<data[i].trans_tym;

cout<<"\t"<<setw(12)<<data[i].wait_tym;

cout<<"\t"<<setw(14)<<data[i].deprt_tym<<endl;

}

}

void avg_w8_tym()

{

}

};

void main()

{

queue q;

int arr_time,trans_time;

char opt;

do

{

cout<<"\n Enter arrival time of the customer: ";

cin>>arr_time;

cout<<" \n Enter transaction time of the customer: ";

cin>>trans_time;

q.enqueue(arr_time,trans_time);

cout<<"\n Do you want to continue? ";

cin>>opt;

}while(opt=='y');

q.Front();

q.dequeue();

q.Is_Empty();

q.dequeue();

q.cal_w8_tym();

q.display();

}

LINKED LIST BASED CIRCULAR QUEUE

#include<iostream.h>

class node

{

public:

int data;

node* link;

};

class queue

{

node* front;

node* back;

public:

queue()

{

front=NULL;

back=NULL;

}

void enqueue(int d)

{

node* ptr;

ptr=new node;

ptr->data=d;

ptr->link=NULL;

if(front==NULL)

{

front=ptr;

back=ptr;

}

else

{

back->link=ptr;

back=back->link;

}

}

void dequeue()

{

node* ptr;

ptr=front;

if(front==NULL)

cout<<"\n Nothing can be deleted. \n";

else

{

ptr=front;

front=front->link;

delete ptr;

ptr=NULL;

cout<<"\n The first element has been deleted.\n";

Front();

}

}

void If_Empty()

{

if(front==NULL)

cout<<"\n The queue is empty.\n ";

}

void Front()

{

if(front!=NULL)

cout<<"\n The first element is "<<front->data<<endl;

}

};

void main()

{

queue q;

int data;

char opt;

do

{

cout<<" Enter your data:\t";

cin>>data;

q.enqueue(data);

cout<<"\n Do you want to continue:\t";

cin>>opt;

}while(opt=='y'opt=='Y');

q.Front();

q.dequeue();

q.If_Empty();

q.dequeue();

q.dequeue();

q.If_Empty();

q.dequeue();

}