answersLogoWhite

0


Best Answer

A priority queue is a type of data structure that allows for elements to be inserted in any order, and to be retrieved in the order of some priority, defined by the creator. It can be implemented in any programming language, including C. For more details, see related links.

User Avatar

Wiki User

16y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

#include

#include

enum{FALSE=0,TRUE=-1};

/////////////////////////////////

///// Implements Priority Queue

/////////////////////////////////

class PriorityQueue // Class Prioriry Queue

{

private:

struct Node // Node of Priority Queue

{

struct Node *Previous;

int Data;

struct Node *Next;

}Current;

struct Node *head; // Pointer to Head

struct Node *ptr;

// Pointer for travelling through Queue

static int NumOfNodes;

// Keeps track of Number of nodes

public:

PriorityQueue(void);

int Maximum(void);

int Minimum(void);

void Insert(int);

int Delete(int);

void Display(void);

int Search (int);

~PriorityQueue(void);

};

// First Nodes Created With Constructor

int PriorityQueue::NumOfNodes=1;

// Constructor

PriorityQueue::PriorityQueue(void)

{

Current.Previous=NULL;

cout<<"Enter First Element of Queue"<

cin>>Current.Data;

Current.Next=NULL;

head=&Current;

ptr=head;

}

// Function Finding Maximum Priority Element

int PriorityQueue::Maximum(void)

{

int Temp;

ptr=head;

Temp=ptr->Data;

while(ptr->Next!=NULL)

{

if(ptr->Data>Temp)

Temp=ptr->Data;

ptr=ptr->Next;

}

if(ptr->Next==NULL && ptr->Data>Temp)

Temp=ptr->Data;

return(Temp);

}

// Function Finding Minimum Priority Element

int PriorityQueue::Minimum(void)

{

int Temp;

ptr=head;

Temp=ptr->Data;

while(ptr->Next!=NULL)

{

if(ptr->Data

Temp=ptr->Data;

ptr=ptr->Next;

}

if(ptr->Next==NULL && ptr->Data

Temp=ptr->Data;

return(Temp);

}

// Function inserting element in Priority Queue

void PriorityQueue::Insert(int DT)

{

struct Node *newnode;

newnode=new Node;

newnode->Data=DT;

while(ptr->Next!=NULL)

ptr=ptr->Next;

if(ptr->Next==NULL)

{

newnode->Next=ptr->Next;

ptr->Next=newnode;

}

NumOfNodes++;

}

// Function deleting element in Priority Queue

int PriorityQueue::Delete(int DataDel)

{

struct Node *mynode,*temp;

ptr=head;

if(NumOfNodes==1)

{

cout<<"Cannot Delete the only Node"<

return FALSE;

}

if(ptr->Data==DataDel)

{

/*** Checking condition for deletion of first node ***/

temp=ptr;

ptr=ptr->Next;

ptr->Previous=NULL;

//delete temp;

head=ptr;

NumOfNodes--;

return(TRUE);

}

else

{

while(ptr->Next->Next!=NULL)

{

/*** Checking condition for deletion of ***/

/*** all nodes except first and last node ***/

if(ptr->Next->Data==DataDel)

{

mynode=ptr;

temp=ptr->Next;

mynode->Next=mynode->Next->Next;

mynode->Next->Previous=ptr;

delete temp;

NumOfNodes--;

return(TRUE);

}

ptr=ptr->Next;

}

if(ptr->Next->Next==NULL && ptr->Next->Data==DataDel)

{

/*** Checking condition for deletion of last node ***/

temp=ptr->Next;

delete temp;

ptr->Next=NULL;

NumOfNodes--;

return(TRUE);

}

}

return(FALSE);

}

// Function Searching element in Priority Queue

int PriorityQueue::Search(int DataSearch)

{

ptr=head;

while(ptr->Next!=NULL)

{

if(ptr->Data==DataSearch)

return ptr->Data;

ptr=ptr->Next;

}

if(ptr->Next==NULL && ptr->Data==DataSearch)

return ptr->Data;

return(FALSE);

}

// Function Displaying elements of Priority Queue

void PriorityQueue::Display(void)

{

ptr=head;

cout<<"Priority Queue is as Follows:-"<

while(ptr!=NULL)

{

cout<Data<

ptr=ptr->Next;

}

}

// Destructor of Priority Queue

PriorityQueue::~PriorityQueue(void)

{

struct Node *temp; /* Temporary variable */

while(head->Next!=NULL)

{

temp=head->Next;

// delete head;

head=temp;

}

if(head->Next==NULL)

delete head;

}

//Main Function

void main()

{

PriorityQueue PQ;

int choice;

int DT;

while(1)

{

cout<<"Enter your choice"<

cout<<"1. Insert an element"<

cout<<"2. Display a priorty Queue"<

cout<<"3. Delete an element"<

cout<<"4. Search an element"<

cout<<"5. Exit"<

cin>>choice;

switch(choice)

{

case 1:

cout<<"Enter a Data to enter Queue"<

cin>>DT;

PQ.Insert(DT);

break;

case 2:

PQ.Display();

break;

case 3:

{

int choice;

cout<<"Enter your choice"<

cout<<"1. Maximum Priority Queue"<

cout<<"2. Minimum Priority Queue"<

cin>>choice;

switch(choice)

{

case 1:

PQ.Delete(PQ.Maximum());

break;

case 2:

PQ.Delete(PQ.Minimum());

break;

default:

cout<<"Sorry Not a correct choice"<

}

}

break;

case 4:

cout<<"Enter a Data to Search in Queue"<

cin>>DT;

if(PQ.Search(DT)!=FALSE)

cout<

else

cout<

break;

case 5:

exit(0);

default:

cout<<"Cannot process your choice"<

}

}

}

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

A priority queue is a queue in which each element is inserted or deleted on the basis of their priority. A higher priority element is added first before any lower priority element. If in case priority of two element is same then they are added to the queue on FCFS basis (first come first serve). Mainly there are two kinds of priority queue: 1) Static priority queue 2) Dynamic priority queue

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

answer my foot

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a program to implement priority queue in C?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Minimum number of queues needed to implement the priority queue?

Separated queue for every possible priority value.


What is most appropriate data structure to implement priority queue?

heap


How many minimum no of queues Rae needed to implement priority queue?

Two possible solutions: 1. Separated queue for every possible priority value. 2. One shared queue for every elements, sorted by priority.


How do you write a Java program to implement weighted queue using circular doubly linked list?

Add weights to the elements of the queue and use an algorithm to sort the queue every time an element is added.


Write an algorithm to implement any three operation of a queue?

8798797


Array implementation of priority queue example program in c plus plus?

yes


What is the difference between a priority queue and a circular queue?

A circular queue is similar to the normal queue with the difference that queue is circular queue ; that is pointer rear can point to beginning of the queue when it reaches at the end of the queue. A priority queue is a queue in which each element is inserted or deleted on the basis of their priority. A higher priority element is added first before any lower priority element. If in case priority of two element is same then they are added to the queue on FCFS basis (first come first serve).


What are the difference between ascending priority queue and descending queue?

Ascending priority queue is a collection of items which can be inserted aurbitarly and which can be removed smallest item. Descending priority queue is similar to ascending priority queue but it allows the deletion of the largest item.


Array implementation of priority queue?

the priority queue is which depends on the data stored.in which their priority is maintained by checking the forth coming values stored in the queue


How to write aC program to merge two singly linked list?

write a c program to circular queue


What are the different types of queues?

A priority queue is a queue in which each element is inserted or deleted on the basis of their priority. A higher priority element is added first before any lower priority element. If in case priority of two element is same then they are added to the queue on FCFS basis (first come first serve). Mainly there are two kinds of priority queue: 1) Static priority queue 2) Dynamic priority queue


Queue ADT Using Array?

implement the queue ADT using an array