#include <iostream>
#include <cassert>
using namespace std;
struct node {
int number;
bool high_priority;
node *link;
};
class linkedPriorityQueue {
private:
node *high_priority_front,
*high_priority_rear,
*queue_front,
*queue_rear;
public:
linkedPriorityQueue();
void addQueue( int number, bool high_priority );
void deleteQueue();
int front();
int back();
bool empty();
bool full();
void print();
int size( bool high_priority);
~linkedPriorityQueue();
};
int main() {
bool t;
int i, j;
linkedPriorityQueue q1;
for( i = 0; i < 9; i++ ) {
t = bool( rand() % 2 );
q1.addQueue( i, t );
cout<<"Customer "<<i<<( t ? " with high-priority " : " " )<<"aded to the queue."<<endl;
}
q1.addQueue( i, true );
cout<<"Customer "<<i<<" with high-priority aded to the queue."<<endl;
cout<<"\nThe number of high-priority customers is "<<q1.size( true )
<<",\nwhile the number of remaining customers is "<<q1.size( false )<<"."<<endl;
cout<<"\nCustomer number "<<q1.front()<<" is at the front of the queue,"
<<"\nwhile customer number "<<q1.back()<<" is at the end of the queue."<<endl;
cout<<endl;
q1.print();
j = q1.size( true ) + q1.size( false ) / 2;
for( i = 0; i < j; i++ )
q1.deleteQueue();
cout<<"\nAfter a few removals, the number of high-priority customers is "<<q1.size( true )
<<",\nwhile the number of remaining customers is "<<q1.size( false )<<"."<<endl;
cout<<"\nCustomer number "<<q1.front()<<" is at the front of the queue,"
<<"\nwhile customer number "<<q1.back()<<" is at the end of the queue."<<endl;
cout<<endl;
q1.print();
return 0;
}

Edited 5 Years Ago by WaltP: Added CODE Tags -- please use them.

my question is if i can summerization the solution more than this? and how ? and i need to implement full and empty and the destructor functions ..

// linkedPriorityQueue.cpp: implementation of the linkedPriorityQueue class.
//
//////////////////////////////////////////////////////////////////////

#include <iostream>
#include "linkedPriorityQueue.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

linkedPriorityQueue::linkedPriorityQueue()
{
	queue_front=queue_rear=NULL;
	high_priority_front=high_priority_rear=NULL;
	

}


linkedPriorityQueue::addQueue( int number, bool high_priority )
{
	node *p=new node;
	p->number =number;
	p->high_priority =high_priority;
	p->link =NULL;
	if(high_priority)
	{
		if (high_priority_front==NULL)
			high_priority_front=high_priority_rear=p;
		else
		{
			high_priority_rear->link=p;
		    high_priority_rear=p;
		}


	}
	else // regular priority
	{
			if (queue_front==NULL)
				queue_front=queue_rear=p;
			else
				{
					queue_rear->link =p;
					queue_rear=p;
				}

	}
      high_priority_rear->link =queue_front;	

	
}
linkedPriorityQueue::deleteQueue()
{
	Node *L;
	L=high_priority_front;
	if (L!=NULL)
		if (L==high_priority_rear)
		{
			high_priority_front =high_priority_rear=NULL;
			delete L;
		}
		else
		{
			high_priority_front=high_priority_front->link;
			delete L;
		}
		else
		{
			L=queue_front;
			if (L!=NULL)
				if (L==queue_rear )
				{
					queue_front =queue_rear=NULL;
					delete L;
				}
				else
				{
					queue_front=queue_front->link;
					delete L;
				}

		}
		
			
}

linkedPriorityQueue::front()
{
	if(high_priority_front!=NULL)
		return high_priority_front->number ;
	else
		return -1;
}

linkedPriorityQueue::back()
{
	if(queue_rear !=NULL)
		return queue_rear->number ;
	else
		return -1;
}

linkedPriorityQueue::print()
{
	node *ptr;
	ptr=high_priority_front;
	cout<<"High-Priority Customers in the Queue:"<<endl;
	while(ptr!=NULL || ptr!=queue_front)
	{
		cout<<ptr->number <<"  ";
		ptr=ptr->link;
	}
	cout<<endl;
	ptr=queue_front ;
	cout<<"Reming Customers in the Queue:"<<endl;
	while(ptr!=NULL)
	{
		cout<<ptr->number <<"  ";
		ptr=ptr->link;
	}
	cout<<endl;

}

linkedPriorityQueue::size(bool high_priority)
{
	node *ptr;
	int c=0;
	if (high_priority)
	{ 
		ptr=high_priority_front;
		while(ptr!=NULL || ptr!=queue_front)
		{
			c++;
			ptr=ptr->link;
		}
		
	}
	else
	{
		ptr=queue_front ;
		cout<<"Reming Customers in the Queue:"<<endl;
		while(ptr!=NULL)
		{
			c++;
			ptr=ptr->link;
		}
	}
	return c;


}

linkedPriorityQueue::~linkedPriorityQueue()
{

}

Edited 5 Years Ago by WaltP: Added CODE Tags again -- please use them.

This article has been dead for over six months. Start a new discussion instead.