i'm trying to make this program which simulates a printing queue, a very basic level program that implements priority queues. i'm stuck in the last function, i.e. the simulate function, there's that while(temp!=NULL){...} loop in simulate function which works perfect the first time but turns into an infinite loop the next time it is in action, although i have double checked that temp==NULL condition does happen, but still loop continues, even putting an if(...){break;} statement does not stop this loop (of course the if(...) condition is satisfied the second time this loop is in action). please help, i am really stuck.

#include <conio>
#include <iostream>
#include <dos>
class pqueue
{
   private:
      enum status{Waiting,Printing,Printed};
		struct node
   	{
   		int num;
      	int pri;
         status state;
      	node *pre;
   	};
      typedef node* pnode;
      pnode front;
      pnode rear;
      int page;
   public:
   	pqueue();
      void enqueue();
      void dequeue();
      bool empty();
      void display();
      void null();
      void simulate(int);
      void update(int);
};
pqueue::pqueue()
{
	front=NULL;
   rear=NULL;
}
void pqueue::enqueue()
{
	typedef node* pnode;
   pnode temp;
   temp=new node;
   if(front==NULL)
   {front=temp;}
   cout<<"Enter number of pages to be printed: ";
   cin>>temp->num;
   do
   {
   	cout<<"Enter priority (0-10) where 10 is highest: ";
   	cin>>temp->pri;
      if((temp->pri<0)||(temp->pri>10))
      {cout<<"Wrong priority\n";}
   }while((temp->pri<0)||(temp->pri>10));
   temp->state=Waiting;
   temp->pre=NULL;
   if(rear!=NULL)
   {rear->pre=temp;}
   rear=temp;
}
void pqueue::dequeue()
{
   if(!empty())
   {
      typedef node* pnode;
   	pnode temp=front;
   	pnode ptr=NULL;
   	int pr=-1;
   	while(temp!=NULL)
   	{
   		if(temp->pri>pr)
      	{pr=temp->pri;}
      	temp=temp->pre;
   	}
      temp=front;
   	while(temp->pri!=pr)
   	{
   		ptr=temp;
      	temp=temp->pre;
   	}
      cout<<"Data deleted is: "<<temp->num<<endl;
      cout<<"With priority: "<<temp->pri<<endl;
      getch();
   	if(ptr==NULL)
   	{
   		front=temp->pre;
      	delete temp;
   	}
   	else if(temp->pre==NULL)
   	{
   		ptr->pre=NULL;
      	rear=ptr;
      	delete temp;
   	}
   	else
   	{
   		ptr->pre=temp->pre;
      	delete temp;
   	}
   }
}
bool pqueue::empty()
{
	if(front==NULL)
   {
   	cout<<"Queue is empty\n";
      getch();
      return true;
   }
   else
   {return false;}
}
void pqueue::display()
{
	if(!empty())
   {
   	typedef node* pnode;
      pnode temp=front;
      while(temp!=NULL)
      {
      	cout<<"Total pages: "<<temp->num
             <<"  Priority value: "<<temp->pri
             <<"  Status: ";
         if(temp->state==0)
         {cout<<"|  Waiting   |\n";}
         else if(temp->state==1)
         {cout<<"|  Printing  |\n";}
         else if(temp->state==2)
         {cout<<"|  Printed   |\n";}
         temp=temp->pre;
      }
      getch();
   }
}
void pqueue::null()
{
   while(!empty())
   {
   	dequeue();
   }
}
void pqueue::simulate(int tim)
{
   if(!empty())
   {
      while(tim!=0)
      {
   		typedef node* pnode;
   		pnode temp=front;
   		int pr=-1;
			while(temp!=NULL)
      	{
      		if((temp->pri>pr)&&(temp->state!=2))
         	{pr=temp->pri;}
   			temp=temp->pre;
   		}
      	temp=front;
      	while(temp->pri!=pr)
      	{
      		temp=temp->pre;
      	}
         if(temp->state==0)
         {page=temp->num;}
      	if(page!=0)
      	{
      		temp->state=Printing;
      		while((tim!=0)&&(page!=0))
      		{
            	update(tim);
         		sleep(1);
      			page--;
            	tim--;
      		}
      	}
      	else if(page==0)
      	{temp->state=Printed;}
      }
   }
}
void pqueue::update(int tim)
{
	clrscr();
   cout<<"\n\t\tSIMULATION OF PRINTING QUEUE\n\n";
   typedef node* pnode;
   pnode temp=front;
   while(temp!=NULL)
   {
   	cout<<"Total pages: "<<temp->num
          <<"  Priority value: "<<temp->pri
          <<"  Status: ";
      if(temp->state==0)
      {cout<<"|  Waiting   |\n";}
      else if(temp->state==1)
      {cout<<"|  Printing  |\n";}
      else if(temp->state==2)
      {cout<<"|  Printed   |\n";}
      temp=temp->pre;
   }
   cout<<"\n\t\tWait for "<<tim<<" seconds";
}
void main()
{
   pqueue pq;
   int sw_var,tim;
   do
   {
      clrscr();
      cout<<"contact: threat.alive@gmail.com\n";
      cout<<"Copyright (C) 2009 Zain ul Abideen\n";
   	cout<<"\n\t\tIMPLEMENTATION OF PRINTING USING PRIORITY QUEUE\n\n"
          <<"\tMENU\n"
          <<"1. Enqueue\n"
          <<"2. Dequeue\n"
          <<"3. Display Queue\n"
          <<"4. Make Null\n"
          <<"5. Simulate Printing\n"
          <<"6. Exit\n";
      cin>>sw_var;
      switch(sw_var)
      {
      	case 1:
         	clrscr();
         	pq.enqueue();
         	break;
         case 2:
         	clrscr();
         	pq.dequeue();
         	break;
         case 3:
         	clrscr();
         	pq.display();
         	break;
         case 4:
         	clrscr();
         	pq.null();
         	break;
         case 5:
         	cout<<"Enter the duration of simulation (in seconds): ";
            cin>>tim;
         	pq.simulate(tim);
         	break;
         case 6:
         	break;
         default:
         	cout<<"Invalid choice";
            getch();
      }
   }while(sw_var!=6);
}

Recommended Answers

All 2 Replies

It goes into an infinite loop when page count goes down to 0 but time is greater than zero. When at line 159 the value of page is 0 the program executes the else statement at line 171. Notice that the value of tim does not get decremented there.

I just glanced through this really quick, but on line 148 you have

if((temp->pri>pr)&&(temp->state!=2))
{
    pr=temp->pri;
}

You also have the declaration

int pr = -1;

. Does temp->pri ever get set to -1? From what I saw it doesn't, so might that be part or your problem?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.