C++ Requirements: You should name your Prority Queue class as PQ. The queue must
ID: 3862215 • Letter: C
Question
C++
Requirements: You should name your Prority Queue class as PQ. The queue must be able to hold unlimited number of integers. It has two key operations: Push and Pop, which should have the time complexity of O(logn). Files to turn in: You need to turn in four files: makefile, main.C, PQ.C, PQ.h. You should have a main.C that reads in or generates some integers, pushes them into the PQ one by one, and pops and prints them until the PQ is empty. Grading The total points is 12, you will get full credit if you correctly implement it using tree structure, 6 points if you correctly implement it using array. No late turn in is acceptable, any late turn in will be given 0 points. Your code must be compilable on Linux/Unix, any code that cannot be compiled by g++ will automatically get ZERO points.
Explanation / Answer
pq.c
-----------------------------
#Define pq.h
using namespace std;
struct node
{
int info;
struct node *link;
}*front, *rear;
/*
* Class Declaration
*/
class pq
{
public:
void push(int);
void po();
pq()
{
front = NULL;
rear = NULL;
}
};
main.c
-----------------
#include<pq.h>
#include<cstdlib>
#include<iostream>
int main()
{
int choice, i;
pq ql;
while (1)
{
cout<<" -------------"<<endl;
cout<<"Operations on Queue"<<endl;
cout<<" -------------"<<endl;
cout<<"1.Insert Element into the Queue"<<endl;
cout<<"2.Delete Element from the Queue"<<endl;
cout<<"3.Quit"<<endl;
cout<<"Enter your Choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter value to be inserted into the queue: ";
cin>>i;
ql.push(i);
break;
case 2:
ql.pop();
break;
case 3:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
}
void pq::push(int i)
{
node *tmp;
tmp = new (struct node);
tmp->info = i;
tmp->link = NULL;
if (front == NULL)
front = tmp;
else
rear->link = tmp;
rear = tmp;
}
void pq::pop()
{
node *tmp;
if (front == NULL)
cout<<"Queue Underflow"<<endl;
else
{
tmp = front;
cout<<"Element Deleted: "<<tmp->info<<endl;
front = front->link;
free(tmp);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.