Nov 20, 2012 at 4:38am UTC
I am trying to make priority ques in this way , everything is going fine except for the priority assigning , i have made a function named assignPriority(); to set priority to the nodes in Que, can somebody please help me with this ??????
#include <iostream>
using namespace std;
int count = 0;
class Node
{
public:
int data;
int priority;
Node* next;
void setData()
{
cout<<"Enter data: \n";
cin>>data;
cout<<"Set Priority :\n";
cin>>priority;
}
void getData()
{
cout<<"Data is :"<<data<<endl;
}
Node()
{
next = NULL;
}
};
class Que
{
public:
Node* front;
Node* rear;
Que()
{
front = NULL;
rear = NULL;
}
void Enque();
void Deque();
void display();
void assignPriority();
bool isEmpty()
{
if(front==NULL && rear==NULL)
return true;
else
return false;
}
};
void Que::Enque()
{
Node *pnew;
pnew = new Node;
pnew->setData();
Que q1;
if(isEmpty())
{
front = pnew;
rear = pnew;
count++;
q1.assignPriority();
}
else
{
rear->next = pnew;
rear = pnew;
count++;
q1.assignPriority();
}
}
void Que::Deque()
{
if(isEmpty())
{
cout<<"The que is Empty !";
}
else
{
Node *temp;
temp = front;
front = temp->next;
cout<<temp->data<<" is dequed !\n";
delete temp;
count--;
}
}
void Que::display()
{
Node *temp;
temp = front;
if(isEmpty())
{
cout<<"The que is empty \n";
}
else
{
cout<<"Queue : \n";
for(int i =0; i<count; i++)
{
cout<<"\tData : "<<temp->data<<"\tPriority :"<<temp->priority<<endl;
temp = temp->next;
}
}
}
void Que::assignPriority()
{
Node *i;
Node *j;
int temp;
for(i=front; i!=NULL; i=i->next)
{
for(j=i->next; j!=NULL; j= j->next)
{
if(i->priority>j->priority)
{
temp = i->priority;
i->priority=j->priority;
j->priority = temp;
}
}
}
}
void main()
{
Que q1;
int choice = 0;
do
{
cout<<"\nEnter your choice \n";
cout<<"\t1-Enqueue.\n";
cout<<"\t2-Dequeue.\n";
cout<<"\t3-Display.\n";
cin>>choice;
cout<<"\n\n";
switch(choice)
{
case 1:
q1.Enque();
break;
case 2:
q1.Deque();
break;
case 3:
q1.display();
break;
default:
cout<<"Wrong choice entered ! try again !";
break;
}
}while(choice);
}