Question

In: Computer Science

Complete the following TODO: parts of the code in C++ #include <iostream> #include <string> #include <limits>...

Complete the following TODO: parts of the code in C++

#include <iostream>

#include <string>

#include <limits>

#include <vector>

using namespace std;


//

// CLASS: NODE

//

class Node{

public:

int value = 0; // our node holds an integer

Node *next = nullptr; // our node has a pointer to the next Node

Node(int i){ // contructor for our Node class

value = i; // store a copy of argument "i" in "value"

next = nullptr; // be sure next Node is null

}

}; // end class definition for Node


//

// CLASS: MAX PRIORITY QUEUE

//


class MaxPriorityQueueLinkedList{

private:

Node *head = nullptr;

int currentSize = 0; // # of items in the heap, also next avaialble stack position

public:

MaxPriorityQueueLinkedList(){ // constructor

//cout << "Created new Max Priority Queue." << endl;

currentSize = 0;

head = nullptr;

}


//

// PUSH - push a new Node instance into the Priority Queue

//

void push(int i){

Node *temp = new Node(i); // create a node with i as the value

// TODO: write your implementation of push here

void push(int i){

// don't forget to manage the "currentSize" value

} // end push()

//

// POP - remove an existing Node instance with the largest value from the Priority Queue

//

int pop(){ // remove the Node with the largest value from the PQ

// TODO:

// Write your implementation of pop here

// You will need to search the queue rooted at "head" to find the largest value

// Don't forget to manage the "currentSize" value when you pop the Node

// Don't forget to return -1 if your queue is empty

// Otherwise you must return the value of the item you just popped (don't return the Node)

// Don't forget to delete the Node you pop before returning, like:

// Node *temp; // declaration of a pointer to the node that you be deleting

// delete temp; // deletes the Node pointed to by "temp"

Solutions

Expert Solution

Program:

#include <iostream>
#include <string>
#include <limits>
#include <vector>

using namespace std;

//
// CLASS: NODE
//
class Node{
public:
int value = 0; // our node holds an integer
Node *next = nullptr; // our node has a pointer to the next Node
Node(int i){ // contructor for our Node class
value = i; // store a copy of argument "i" in "value"
next = nullptr; // be sure next Node is null
}
}; // end class definition for Node

//
// CLASS: MAX PRIORITY QUEUE
//
class MaxPriorityQueueLinkedList{
private:
Node *head = nullptr;
int currentSize = 0; // # of items in the heap, also next available stack position
public:
MaxPriorityQueueLinkedList(){ // constructor
cout << "Created new Max Priority Queue." << endl;
currentSize = 0;
head = nullptr;
}
//
// PUSH - push a new Node instance into the Priority Queue
//
void push(int i){
Node *temp = new Node(i); // create a node with i as the value
// check if your queue is empty
if(currentSize==0)
head = temp;
else{
temp->next=head;
head = temp;
}
//increase currentSize
currentSize++;
} // end push()

//
// POP - remove an existing Node instance with the largest value from the Priority Queue
//
int pop(){ // remove the Node with the largest value from the PQ
// check if your queue is empty
if(currentSize==0)
return -1;

// declaration of a pointer to the node
Node *temp = head;

//search the queue to find the largest value
int maxm = temp->value;
while(temp!=nullptr){
if(temp->value > maxm)
maxm = temp->value;
temp = temp->next;
}

temp = head;
//if the largest value found at head
if(head->value==maxm)
{
head = head->next;
}
//if the largest value found other than head
else
{
Node *p=nullptr;
while(temp->value!=maxm){
p = temp;
temp = temp->next;
}
p->next = temp->next;
}
//decrease currentSize
currentSize--;
// deletes the Node pointed to by "temp"
delete temp;
//return the largest value
return maxm;
} // end pop()
}; // end class definition for MaxPriorityQueueLinkedList


//main function
int main()
{
//create object of MaxPriorityQueueLinkedList
MaxPriorityQueueLinkedList pq;

//insert to the PQ
pq.push(7);
pq.push(4);
pq.push(2);
pq.push(3);
pq.push(8);

//remove from PQ
cout<<"Remove largest value from the PQ : " << pq.pop() << endl;
cout<<"Remove largest value from the PQ : " << pq.pop() << endl;
cout<<"Remove largest value from the PQ : " << pq.pop() << endl;
cout<<"Remove largest value from the PQ : " << pq.pop() << endl;
cout<<"Remove largest value from the PQ : " << pq.pop() << endl;
cout<<"Remove largest value from the PQ : " << pq.pop() << endl;

return 0;
}

Output:

Solving your question and helping you to well understand it is my focus. So if you face any difficulties regarding this please let me know through the comments. I will try my best to assist you. However if you are satisfied with the answer please don't forget to give your feedback. Your feedback is very precious to us, so don't give negative feedback without showing proper reason.
Always avoid copying from existing answers to avoid plagiarism.
Thank you.


Related Solutions

C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using...
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using namespace std; // M x N matrix #define M 5 #define N 5 // Naive recursive function to find the minimum cost to reach // cell (m, n) from cell (0, 0) int findMinCost(int cost[M][N], int m, int n) {    // base case    if (n == 0 || m == 0)        return INT_MAX;    // if we're at first cell...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using namespace std; const int CWIDTH = 26; int main() {    int choice;    double convertFoC, converCtoF;    double starting, endvalue, incrementvalue;    const int CWIDTH = 13;    do {       cin >> choice;    switch (choice)    {        cin >> starting;    if (starting == 28){       cout << "Invalid range. Try again.";    }    while (!(cin >> starting)){       string  garbage;       cin.clear();       getline(cin, garbage);       cout << "Invalid data Type, must be a number....
For C++ Consider the following code defining classes for assets and office equipment: #include<string> #include<iostream> using...
For C++ Consider the following code defining classes for assets and office equipment: #include<string> #include<iostream> using namespace std; // class definition for asset class Asset{ protected: int value; // value of asset in cents public: Asset(int value); // constructor int get_value(); // get the value }; Asset::Asset(int val){ // implementation of constructor value=val; } int Asset::get_value(){ // implementation of get_value return value; } // abstract class for office equipment class OfficeEquipment:public Asset{ public: OfficeEquipment(int val); // constructor virtual void use_item()...
Add Bubble Sorting & Binary Search Functions to the following code (C++) #include<iostream> #include<fstream> #include<string> #include<iomanip>...
Add Bubble Sorting & Binary Search Functions to the following code (C++) #include<iostream> #include<fstream> #include<string> #include<iomanip> #include<algorithm> using namespace std; const int row = 10; const int col = 7;   ifstream name; ifstream grade; ofstream out; void readData(string stname[row], int stgrade[row][col]); void stsum(int stgrade[row][col], int total[row]); void staverage(int total[row], double average[row]); void stlettergrade(double average[row],char ltrgrade[row]); void displayData(string stname[row], int stgrade[row][col], int total[row], double staverage[row], char ltrgrade[row]); void swapltrgrade(char* xp, char* yp); int main() {    int STG[row][col] = { {0},{0}...
C++ finish the AVL Tree code: #include "AVLNode.h" #include "AVLTree.h" #include <iostream> #include <string> using namespace...
C++ finish the AVL Tree code: #include "AVLNode.h" #include "AVLTree.h" #include <iostream> #include <string> using namespace std; AVLTree::AVLTree() { root = NULL; } AVLTree::~AVLTree() { delete root; root = NULL; } // insert finds a position for x in the tree and places it there, rebalancing // as necessary. void AVLTree::insert(const string& x) { // YOUR IMPLEMENTATION GOES HERE } // remove finds x's position in the tree and removes it, rebalancing as // necessary. void AVLTree::remove(const string& x) {...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell {...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell { int val; Cell *next; }; int main() { int MAX = 10; Cell *c = NULL; Cell *HEAD = NULL; srand (time(NULL)); for (int i=0; i<MAX; i++) { // Use dynamic memory allocation to create a new Cell then initialize the // cell value (val) to rand(). Set the next pointer to the HEAD and // then update HEAD. } print_cells(HEAD); }
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; //...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; // constants const int FINAL_POSITION = 43; const int INITIAL_POSITION = -1; const int NUM_PLAYERS = 2; const string BLUE = "BLUE"; const string GREEN = "GREEN"; const string ORANGE = "ORANGE"; const string PURPLE = "PURPLE"; const string RED = "RED"; const string YELLOW = "YELLOW"; const string COLORS [] = {BLUE, GREEN, ORANGE, PURPLE, RED, YELLOW}; const int NUM_COLORS = 6; // names...
this is cahce memory related c program....and not working or running properly??????? #include <iostream> #include <string>...
this is cahce memory related c program....and not working or running properly??????? #include <iostream> #include <string> #include <vector> #include <iomanip> #include <cmath> #include <cstdlib> #include <ctime> using namespace std; int cash_type, block_size, cash_size,number_of_blocks=0;; int compulsry_misses=0, capcity_misses=0, conflict_misses=0; #define DRAM_SIZE (64*1024*1024) unsigned int m_w = 0xABABAB55; /* must not be zero, nor 0x464fffff */ unsigned int m_z = 0x05080902; /* must not be zero, nor 0x9068ffff */ unsigned int rand_() { m_z = 36969 * (m_z & 65535) + (m_z >>...
What is the flowchart for this code. Thank You! #include<iostream> #include<iomanip> #include<string> #include<cmath> using namespace std;...
What is the flowchart for this code. Thank You! #include<iostream> #include<iomanip> #include<string> #include<cmath> using namespace std; float series(float r[], int n) {    float sum = 0;    int i;    for (i = 0; i < n; i++)        sum += r[i];    return sum; } float parallel(float r[], int n) {    float sum = 0;    int i;    for (i = 0; i < n; i++)        sum = sum + (1 / r[i]);...
A C++ question: I want to indent the code of this C++ program: #include<iostream> #include<cstring> using...
A C++ question: I want to indent the code of this C++ program: #include<iostream> #include<cstring> using namespace std; int lastIndexOf(char *s, char target) { int n=strlen(s); for(int i=n-1;i>=0;i--) { if(s[i]==target) { return i; } } return -1; } void reverse(char *s) { int n=strlen(s); int i=0,j=n-1; while(i<=j) { char temp=s[i]; s[i]=s[j]; s[j]=temp; i++; j--; } return; } int replace(char *s, char target, char replacementChar) { int len=strlen(s); int total=0; for(int i=0;i<len;i++) { if(s[i]==target) { s[i]=replacementChar; total+=1; } } return total;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT