Google Search

Wednesday, October 27, 2010

QUEUE APPLICATION – FIRST COME FIRST SERVE

QUEUE APPLICATION – FIRST COME FIRST SERVE

#include
#include
class queue
{
struct link
{
int burst;
char process[10];
struct link *next;
}*front,*rear;
public:
int limit,count;
queue(int n)
{front=NULL;rear=NULL;limit=n;count=0;}
void enqueue();
void allocate();
void search();
int IsEmpty();
void display();
~queue();
};


queue::~queue()
{
struct link *temp;
while(front!=NULL)
{
temp=front;
front=front->next;
delete front;
}
}


void queue::enqueue()
{
struct link *node;
char p[10];
int b;
node=new struct link;
cout<<"\nEnter the process name :"; cin>>p;
cout<<"\nEnter the bursttime :"; cin>>b;
strcpy(node->process,p);
node->burst=b;
node->next=NULL;
if(rear==NULL)
{rear=node;front=node;}
else
{
rear->next=node;
rear=node;
}
count++;
}



void queue::allocate()
{
struct link *temp;
if(rear==NULL)
cout<<"\nQueue Empty..No process to be executed\n"; else { temp=front; cout<<"\nThe Process allocated is : "<process<<"\t"<burst;
front=front->next;
delete temp;
}
}


void queue::search()
{
struct link *current=front;
char val[10];
if(IsEmpty())
cout<<"\nQueue is Empty\n"; else { cout<<"\nEnter the process to be searched\n"; cin>>val;
while(current!=NULL)
{
if(strcmp(current->process,val)==0)
break;
current=current->next;
}
if(current==NULL)
cout<<"\nThe process "<process<<"with bursttime "<burst<<" found in the Queue\n"; } } int queue::IsEmpty() { if(rear==NULL) return 1; else return 0; } void queue::display() { struct link *current; current=front; if(current==NULL) cout<<"\nQueue is empty"; else { cout<<"\n\tThe prcoesses in queue are :\n"; cout<<"\t------------------------------\n"; while(current!=NULL) { cout<<"\t\t"<process<<"\t"<burst<next;
}
cout<<"\t-------------------------------\n"; cout<>size;
queue q(size);
do
{
cout<<"\n\t\tMENU\n\n"; cout<<"\t1.ENQUEUE"; cout<<"\t2.ALLOCATE"; cout<<"\t3.SEARCH"; cout<<"\n\t4.ISEMPTY"; cout<<"\t5.DISPLAY"; cout<<"\t6.EXIT"; cout<<"\n\t Enter ur choice : "; cin>>ch;
switch(ch)
{
case 1:if(q.count==q.limit)
cout<<"\nQueue full\n";
else
q.enqueue();
break;
case 2:q.allocate();
break;
case 3:q.search();break;
case 4:if(q.IsEmpty())
cout<<"\nQueue is Empty\n";
else
cout<<"\nQueue is not Empty\n";
break;
case 5:cout<<"\n The Queue is \n";
q.display();
break;
case 6:exit(0);
}
}
while(ch!=6);
}

0 comments:

Post a Comment