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) {...
Please comments this C++ code and show screenshots of the outputs main.cpp #include<iostream> #include<vector> #include<string> #include"BST.h"...
Please comments this C++ code and show screenshots of the outputs main.cpp #include<iostream> #include<vector> #include<string> #include"BST.h" #include"BST.cpp" using namespace std; std::vector<std::string> tokenize(char line[]) {    std::vector<std::string> tok;        std::string word = "";        for (int i = 0; i < strlen(line); i++)        {            if (i == strlen(line) - 1)            {                word += line[i];                tok.push_back(word);                return tok;       ...
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); }
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to...
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to store user input bool cont = true; //implement a loop so that it will continue asking until the user provides a positive integer // the following provides ONLY part of the loop body, which you should complete { cout <<"How many words are in your message? \n"; cout <<"Enter value: "; // get user input integer here    cout <<"\nInvalid value. Please Re-enter a...
--- 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...
Read Ch. 3. Using the C++ editor, type the following program: #include <iostream> #include <string> using...
Read Ch. 3. Using the C++ editor, type the following program: #include <iostream> #include <string> using namespace std; int main () {        //declarations string name;        //input cout <<"Enter your first name" << endl;        cin >> name;        //output        cout << "Hello " << name << "! " << endl;        return 0; } Compile and run. What does the cin statement do? What does the cout statement do? Is name a variable or a constant? What...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT