Need help! please fill out!! C++ only //----------------------------------------
ID: 3720196 • Letter: N
Question
Need help! please fill out!! C++ only
//--------------------------------------------------------------------
//
// Laboratory 11, Programming Exercise 1 PriorityQueue.cpp
//
// ** SOLUTION: Heap implementation of the Priority Queue ADT **
//
//--------------------------------------------------------------------
#ifndef PRIORITYQUEUE_CPP
#define PRIORITYQUEUE_CPP
using namespace std;
#include "PriorityQueue.h"
//--------------------------------------------------------------------
template < typename DataType, typename KeyType, typename Comparator >
PriorityQueue<DataType, KeyType, Comparator>::PriorityQueue(int maxNumber)
// Creates an empty priority queue.
{
}
//--------------------------------------------------------------------
template < typename DataType, typename KeyType, typename Comparator >
void PriorityQueue<DataType, KeyType, Comparator>::enqueue(const DataType &newDataItem)
// Inserts newDataItem into a priority queue.
{
}
//--------------------------------------------------------------------
template < typename DataType, typename KeyType, typename Comparator >
DataType PriorityQueue<DataType, KeyType, Comparator>::dequeue()
// Removes the least recently added (front) data item from a priority
// queue and returns it.
{
}
#endif // #ifndef PRIORITYQUEUE_CPP
Explanation / Answer
Answer::
#include <stdio.h>
#include <stdlib.h>
#define MAX 30
typedef struct pqueue
{
int data[MAX];
int rear,front;
}pqueue;
void initialize(pqueue *p);
int empty(pqueue *p);
int full(pqueue *p);
void enqueue(pqueue *p, int x);
int dequeue(pqueue *p);
void print(pqueue *p);
void main()
{
int x,op,n,i;
pqueue q;
initialize(&q);
do
{
printf(" 1)Create 2)Insert 3)Delete 4)Print 5)EXIT");
printf(" Enter Choice: ");
scanf("%d",&op);
switch (op) {
case 1: printf(" Enter Number of Elements");
scanf("%d",&n );
initialize(&q);
printf("Enter the data");
for(i=0; i<n; i++)
{
scanf("%d",&x);
if(full(&q))
{
printf(" Queue is Full..");
exit(0);
}
enqueue(&q,x);
}
break;
case 2: printf(" Enter the element to be inserted");
scanf("%d ",&x);
if(full(&q))
{
printf(" Queue is Full");
exit(0);
}
enqueue(&q,x);
break;
case 3: if(empty(&q))
{
printf(" Queue is empty..");
exit(0);
}
x=dequeue(&q);
printf(" Deleted Element=%d",x);
break;
case 4: print(&q);
break;
default: break;
}
}while (op!=5);
}
void initialize(pqueue *p)
{
p->rear=-1;
p->front=-1;
}
int empty(pqueue *p)
{
if(p->rear==-1)
return(1);
return(0);
}
int full(pqueue *p)
{
if((p->rear+1)%MAX==p->front)
return(1);
return(0);
}
void enqueue(pqueue *p, int x)
{
int i;
if(full(p))
printf(" Overflow");
else
{
if(empty(p))
{
p->rear=p->front=0;
p->data[0]=x;
}
else
{
i=p->rear;
while(x>p->data[i])
{
p->data[(i+1)%MAX]=p->data[i];
i=(i-1+MAX)%MAX;
if((i+1)%MAX==p->front)
break;
}
i=(i+1)%MAX;
p->data[i]=x;
p->rear=(p->rear+1)%MAX;
}
}
}
int dequeue(pqueue *p)
{
int x;
if(empty(p))
{
printf(" Underflow..");
}
else
{
x=p->data[p->front];
if(p->rear==p->front)
initialize(p);
else
p->front=(p->front +1)%MAX;
}
return(x);
}
void print(pqueue *p)
{
int i,x;
if(empty(p))
{
printf(" Queue is empty..");
}
else
{
i=p->front;
while(i!=p->rear)
{
x=p->data[i];
printf(" %d",x);
i=(i+1)%MAX;
}
x=p->data[i];
printf(" %d",x);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.