Suppose you are trapped on a desert island with nothing but a priority queue, and you need to implement a stack. Complete the following class that stores pairs (count, element) where the count is incremented with each insertion. Recall that make_pair(count, element) yields a pair object, and that p.second yields the second component of a pair p. The pair class defines an operator< that compares pairs by their first component, and uses the second component only to break ties. Code: #include #include #include #include using namespace std; class Stack { public:    Stack();    string top();    void pop();    void push(string element); private:    int count;    priority_queue> pqueue; }; Stack::Stack() {    /* Your code goes here */ } string Stack::top() {    /* Your code goes here */ } void Stack::pop() {    /* Your code goes here */ } void Stack::push(string element) {    /* Your code goes here */ } int main() {    Stack stk;    stk.push("Mary");    stk.push("had");    stk.push("a");        cout << stk.top() << endl; stk.pop();    cout << "Expected: a\n" << endl;        stk.push("little");    cout << stk.top() << endl; stk.pop();    cout << "Expected: little\n" << endl;    cout << stk.top() << endl; stk.pop();    cout << "Expected: had\n" << endl;    stk.push("lamb");    cout << stk.top() << endl; stk.pop();    cout << "Expected: lamb\n" << endl;    cout << stk.top() << endl; stk.pop();    cout << "Expected: Mary" << endl;

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16PE: The implementation of a queue in an array, as given in this chapter, uses the variable count to...
icon
Related questions
Question

Suppose you are trapped on a desert island with nothing but a priority queue, and you need to implement a stack. Complete the following class that stores pairs (count, element) where the count is incremented with each insertion.

Recall that make_pair(count, element) yields a pair object, and that p.second yields the second component of a pair p.

The pair class defines an operator< that compares pairs by their first component, and uses the second component only to break ties.

Code:

#include <iostream>
#include <queue>
#include <string>
#include <utility>

using namespace std;

class Stack
{
public:
   Stack();
   string top();
   void pop();
   void push(string element);
private:
   int count;
   priority_queue<pair<int, string>> pqueue;
};

Stack::Stack()
{
   /* Your code goes here */
}

string Stack::top()
{
   /* Your code goes here */
}

void Stack::pop()
{
   /* Your code goes here */
}

void Stack::push(string element)
{
   /* Your code goes here */
}

int main()
{
   Stack stk;
   stk.push("Mary");
   stk.push("had");
   stk.push("a");
   
   cout << stk.top() << endl; stk.pop();
   cout << "Expected: a\n" << endl;
   
   stk.push("little");

   cout << stk.top() << endl; stk.pop();
   cout << "Expected: little\n" << endl;
   cout << stk.top() << endl; stk.pop();
   cout << "Expected: had\n" << endl;

   stk.push("lamb");

   cout << stk.top() << endl; stk.pop();
   cout << "Expected: lamb\n" << endl;
   cout << stk.top() << endl; stk.pop();
   cout << "Expected: Mary" << endl;   
}



Expert Solution
steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Arrays
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning