I'm trying to implement priority queue using a singly linked list, but the program is not running in the best way, n the display function is showing just first n last node's data , y???

#include<iostream.h>
#include<conio.h>

struct node

{   
	int n,p,b;   // Data members for getting Task no. ,Priority and Burst time respectively..//
	node * ptr; //Pointer pointing to next node .///
};

class pqueu

{
private:
	node * head; /// Header pointer //
	node * temp1;  //Pointer used for Searching ///
public:
	pqueu():head(NULL)  //Giving NULL value to head ///
	{}
	void insert()
	{    node* temp; // node for Getting Data //
		temp=new node;
		if (head==NULL)                  //if there is no node created first //
		{
			cout<<"\nEnter task num"<<endl;
			cin>>temp->n;
			cout<<"Enter priority"<<endl;
			cin>>temp->p;                                  //Getting data from user //
			cout<<"Enter burst time"<<endl;
			cin>>temp->b;
			temp->ptr=NULL;
			head=temp;
			cout<<"If case:"<<endl;
		}//If body ends//
		else
		{   node * temp;
		temp=new node;
	     	temp1=head;
			while (temp1->ptr!=NULL)   //at the end of list //
				temp1=temp1->ptr;         //moving pointer forward//
			{
			cout<<"\nEnter task num"<<endl;
			cin>>temp->n;
			cout<<endl<<"Enter priority"<<endl;
			cin>>temp->p;
			cout<<"Enter burst time"<<endl;
			cin>>temp->b;                                  ///Getting data //
			temp->ptr=NULL;
			head->ptr=temp;
			cout<<"Else case:"<<endl;		
		}//while body ends //
			

		}//else body ends //
	}

	int display()
	{      temp1=head;
		   while(1)
		   {
			   cout<<endl<<endl;
						cout << "NODE DATA" << endl;
						cout << "Task no. " << temp1->n<<endl;
						cout<<  "Priority."<<temp1->p<<endl;
						cout<<  "Burst Time "<<temp1->b;                   //displaying data values//
						cout << endl;
						temp1=temp1->ptr;
						
			   temp1=temp1->ptr;
			   if(temp1->ptr==NULL)
			   {return 0;}
			   
				}
		}


		 void sort(int nn)
		 {  cout<<"sort"<<endl;
			int var;
			node * temp1=head;
			for(temp1 =head; temp1->ptr != NULL; temp1= temp1->ptr)
						{
					 
				for (int x=0;x<=nn;x++)
				{   
					cout<<"for 1"<<endl;
					for(int y=0;y<=nn-1;y++)
					{
						
						if(temp1->p<temp1->ptr->p)         //cheking if former priority is less then later //
					{    
						cout<<"if"<<endl;
							var=temp1->p;
							temp1->p=temp1->ptr->p;
							temp1->ptr->p=var;
							
							var=temp1->n;
							temp1->n=temp1->ptr->n;
							temp1->ptr->n=var;
							
							var=temp1->b;                        //sortng priorities//
							temp1->b=temp1->ptr->b;
							temp1->ptr->b=var;
							cout<<"if2"<<endl;
							temp1=temp1->ptr;
						}
						else
						{
							temp1=temp1->ptr;                   //moving pointer //
							cout<<"else"<<endl;
						}
						
					}cout<<"Values replaced."<<endl;	
				}
			}
		}


		void pop()
		{    cout<<"pop"<<endl;
			int c;
			while(temp1->ptr!=NULL)
				temp1=temp1->ptr;
			{
		  		if(temp1->p > temp1->ptr->p)
				{
					for(c=temp1->b;c>0;c--)
					{
						cout<<"Working on task no. "<<temp1->n<<endl;          //completing task//
					}
					cout<<"Task No. "<<temp1->n<<"Completed"<<endl;
				}
				else 
					if( temp1->p=temp1->ptr->p)
				{
					if(temp1->n<temp1->ptr->n)
						for(c=temp1->b;c>0;c--)
						{
	                      cout<<"Working on task no. "<<temp1->n<<endl;
						}
					cout<<"Task No. "<<temp1->n<<"Completed"<<endl;
				}
			}
		}
		};


		main()
		
		{
			int t;
			pqueu pq;
			cout<<"How many tasks do u have? "<<endl;
			cin>>t;                                    //how many nodes user is going to create//
			for(int i=t;i>0;i--)
			{
			pq.insert();
			}
			pq.display();
			pq.sort(t);
			pq.pop();
			getch();

		}

1. Line 49 : It should be

temp1->ptr=temp;

2. Line 67 and 69 : You repeat the same statement. Delete both the lines and bring one of them after the if-condition.

if(temp1->ptr==NULL)
{return 0;}
temp1=temp1->ptr;

Now it's up to you to think and find what error you made and how these two corrections rectified it.

1. Line 49 : It should be

temp1->ptr=temp;

2. Line 67 and 69 : You repeat the same statement. Delete both the lines and bring one of them after the if-condition.

if(temp1->ptr==NULL)
{return 0;}
temp1=temp1->ptr;

Now it's up to you to think and find what error you made and how these two corrections rectified it.

thnku :)

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.