Question

I am to implement a simple simulation that supports a stack and a queue of items being either enqueued and dequeued (ont...

I am to implement a simple simulation that supports a stack and a queue of items being either enqueued and dequeued (onto the queue) or pushed and popped (onto the stack). I are required to use STL data structures to implement and create the stack and queue for my program.

----- testfile1.tst --------
enqueue 5
enqueue 7
push blooper
push rookie
dequeue
push demerits
pop
enqueue 3
enqueue 8
push showplace
enqueue 9
dequeue
pop
dequeue
push palmetto
push zebra
pop
push collapse
dequeue
dequeue
dequeue
pop
pop
pop
pop
pop
enqueue 2
enqueue 4
push penguins
push sleeping
exit

The output should look as shown below:

Enque : 5
Stack :
Queue : 5
Enque : 7
Stack :
Queue : 5 7
Push : blooper
Stack : blooper
Queue : 5 7
Push : rookie
Stack : rookie blooper
Queue : 5 7
Dequeue: 5
Stack : rookie blooper
Queue : 7
Push : demerits
Stack : demerits rookie blooper
Queue : 7
Popped : demerits
Stack : rookie blooper
Queue : 7
Enque : 3
Stack : rookie blooper
Queue : 7 3
Enque : 8
Stack : rookie blooper
Queue : 7 3 8
Push : showplace
Stack : showplace rookie blooper
Queue : 7 3 8
Enque : 9
Stack : showplace rookie blooper
Queue : 7 3 8 9
Dequeue: 7
Stack : showplace rookie blooper
Queue : 3 8 9
Popped : showplace
Stack : rookie blooper
Queue : 3 8 9
Dequeue: 3
Stack : rookie blooper
Queue : 8 9
Push : palmetto
Stack : palmetto rookie blooper
Queue : 8 9
Push : zebra
Stack : zebra palmetto rookie blooper
Queue : 8 9
Popped : zebra
Stack : palmetto rookie blooper
Queue : 8 9
Push : collapse
Stack : collapse palmetto rookie blooper
Queue : 8 9
Dequeue: 8
Stack : collapse palmetto rookie blooper
Queue : 9
Dequeue: 9
Stack : collapse palmetto rookie blooper
Queue :
Dequeue request from empty queue
Stack : collapse palmetto rookie blooper
Queue :
Popped : collapse
Stack : palmetto rookie blooper
Queue :
Popped : palmetto
Stack : rookie blooper
Queue :
Popped : rookie
Stack : blooper
Queue :
Popped : blooper
Stack :
Queue :
Pop request from empty stack
Stack :
Queue :
Enque : 2
Stack :
Queue : 2
Enque : 4
Stack :
Queue : 2 4
Push : penguins
Stack : penguins
Queue : 2 4
Push : sleeping
Stack : sleeping penguins
Queue : 2 4
Simulation ends

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <stack>
#include <queue>
#include <stack>
#include <sstream>
#include <algorithm>
using namespace std;

stack <string> s;
queue <int> q;
void enqueue(int n) {
   q.push(n);
}
void dequeue() {
   if (q.empty()) {
       cout << "QUEUE IS EMPTY" << endl;
   }
   else {
       q.pop();
   }
}
void push(string str) {
   s.push(str);
}
void pop() {
   if (s.empty()) {
       cout << "STACK IS EMPTY" << endl;
   }
   else {
       s.pop();
   }
}
void displayQueue() {
   cout << "QUEUE: ";
   queue <int> qu;
   cout << endl;
   if (q.empty()) {
       cout << "empty queue" << endl;
   }
   else {
       while (!q.empty())
       {
           cout << " ";
           qu.push(q.front());
           cout << q.front();
           q.pop();
       }
       if (qu.empty()) {
           cout << "empty queue" << endl;
       }
       while (!qu.empty()) {
           q.push(qu.front());
           qu.pop();
       }
   }
   cout << endl;
}
void displayStack() {
   cout << "STACK: ";
   stack <string> sr;
   cout << endl;
   if (s.empty()) {
       cout << "stack is empty" << endl;
       return;
   }
   else {
      
       while (!s.empty())
       {
           cout << " ";
           sr.push(s.top());
           cout << s.top();
           s.pop();
       }
       if (sr.empty()) {
           cout << "stack is empty" << endl;
       }
       while (!sr.empty()) {
           cout << " ";
           s.push(sr.top());
           sr.pop();
       }
       cout << endl;

   }
}
int main()
{
   int val = 5;
   cout << "Enqueue:" << val<<endl;
  
   enqueue(5);
  
   displayStack();
   displayQueue();

   val = 7;
   cout << "Enqueue:" << val << endl;
   enqueue(7);

   displayQueue();
   displayStack();
  
   string s="blooper";
   cout << "PUSH: " << s << endl;
   push("blooper");
   s = "rookie";
   cout << "PUSH: " << s << endl;
   push("rookie");
   displayStack();
   displayQueue();


   dequeue();
   displayStack();
   displayQueue();

   s = "demerits";
   cout << "PUSH: " << s << endl;
   push("demerits");
   displayStack();
   displayQueue();

   pop();
   displayStack();
   displayQueue();

   val = 3;
   cout << "Enqueue:" << val << endl;
   enqueue(3);
   displayStack();
   displayQueue();

   val = 8;
   cout << "Enqueue:" << val << endl;
   enqueue(8);
   displayStack();
   displayQueue();

   s = "showplace";
   cout << "PUSH: " << s << endl;
   push("showplace");
   displayStack();
   displayQueue();


   val = 9;
   cout << "Enqueue:" << val << endl;
   enqueue(9);
   displayStack();
   displayQueue();

   dequeue();
   displayStack();
   displayQueue();
  
   pop();
   displayStack();
   displayQueue();

   dequeue();
   displayStack();
   displayQueue();
  
   s = "palmetto";
   cout << "PUSH: " << s << endl;
   push("palmetto");
   displayStack();
   displayQueue();

   s = "zebra";
   cout << "PUSH: " << s << endl;
   push("zebra");
   displayStack();
   displayQueue();

   pop();
   displayStack();
   displayQueue();

   s = "collapse";
   cout << "PUSH: " << s << endl;
   push("collapse");
   displayStack();
   displayQueue();

   displayStack();
   displayStack();
   displayQueue();

   dequeue();
   displayStack();
   displayQueue();

   dequeue();
   displayStack();
   displayQueue();

   dequeue();
   displayStack();
   displayQueue();

   pop();
   displayStack();
   displayQueue();

   pop();
   displayStack();
   displayQueue();

   pop();
   displayStack();
   displayQueue();

   pop();
   displayStack();
   displayQueue();

   pop();
   displayStack();
   displayQueue();


   val = 2;
   cout << "Enqueue:" << val << endl;
   enqueue(2);
   displayStack();
   displayQueue();


   val = 4;
   cout << "Enqueue:" << val << endl;
   enqueue(4);
   displayStack();
   displayQueue();

   s = "penguins";
   cout << "PUSH: " << s << endl;
   push("penguins");
   displayStack();
   displayQueue();

   s = "sleeping";
   cout << "PUSH: " << s << endl;
   push("sleeping");
   displayQueue();
   displayStack();
   return 0;
}

C\WINDOWS System32\cmd.exe collapse palmetto rookie blooper QUEUE empty queue STACK: palmetto rookie blooper QUEUE empty queu

IF THERE'S ANY QUERY PLEASE COMMENT DOWN BELOW

PLEASE GIVE A THUMBS UP

Add a comment
Know the answer?
Add Answer to:
I am to implement a simple simulation that supports a stack and a queue of items being either enqueued and dequeued (ont...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (ii) [6 marks] Assume that we have an empty stack S and an empty queue Q....

    (ii) [6 marks] Assume that we have an empty stack S and an empty queue Q. Given a series of stack operations on S as below: Push(S,10), Push(S, 7), Push(S, 23), Pop(S), Push(S, 9), Pop(S), Pop(S) Output the element returned by each Pop operation. Given a series of queue operations on Q as below: Enqueue(0,10),Enqueue(Q,20), Enqueue(0,33), Dequeue(Q), Enqueue(Q,55), Dequeue(Q), Dequeue(Q) Output the element returned by each Dequeue operation. (iii) [8 marks] Given an empty binary search tree T, draw the...

  • A. Starting with an initially empty stack, after 6 push operations, 3 pop operations, and 2...

    A. Starting with an initially empty stack, after 6 push operations, 3 pop operations, and 2 push operations, the number of elements in the stack would be: B. Starting with an initially empty queue, after 5 enqueue operations, 4 dequeue operations, and 6 enqueue operations, the number of elements in the queue would be:

  • help finish Queue, don't think I have the right thing. # 1. After studying the Stack...

    help finish Queue, don't think I have the right thing. # 1. After studying the Stack class and testStack() functions in stack.py # complete the Queue class below (and test it with the testQueue function) # # 2. Afer studying and testing the Circle class in circle.py, # complete the Rectangle class below (and test it with the testRectangle function) # # # 3. SUBMIT THIS ONE FILE, with your updates, TO ICON. # # # NOTE: you may certainly...

  • The class pictured below is designed to implement an integer queue using two stacks. Assume the...

    The class pictured below is designed to implement an integer queue using two stacks. Assume the stack methods all work as desired (though their implementations are not shown). .(a) Trace what happens in the following situation, showing intermediate steps (values of variables, what is in the stacks, and what is in the queue at various points in the methods, not just the results of the methods). • Start with an empty queue. Enqueue 5, then 16, then 7. Dequeue twice....

  • In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and...

    In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and dequeue functions. You could use either the array or linked list implementation for stacks and queues. Source for stack array: --------------------------------------------------- #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the...

  • e. public class Queue { // Uses the correct Stack class from ME2 Ex 19 private Stack mStack; public Queue() { setStack(new Stack()); } public Queue enqueue(E pData) { getStack().push(pData); return t...

    e. public class Queue { // Uses the correct Stack class from ME2 Ex 19 private Stack mStack; public Queue() { setStack(new Stack()); } public Queue enqueue(E pData) { getStack().push(pData); return this; } public E dequeue() { return getStack().pop(); } public E peek() { return getStack.peek(); } private Stack getStack() { return mStack; } private void setStack(Stack pStack) { mStack = pStack; } } f. public class Queue extends Stack { // Uses the correct Stack class from ME2 Ex...

  • I need a method that takes two parameters, one is Queue and other is int n,...

    I need a method that takes two parameters, one is Queue and other is int n, then I want to use stack and/or queue to reverse the order of n elments in Queue instance. I don't want to use recursion. I want the reverse class with method taking queue and n as parameters and a main method. I want stack class with array implementation with push and pop methods and Queue class with linkedlist implementation with methods deQueue and enQueue...

  • You are going to create a Queue. (alternately you can create a list and simply implement...

    You are going to create a Queue. (alternately you can create a list and simply implement enqueue and dequeue functions in the List – that will technically make it a queue). You will fill the first list with numbers consecutively numbered from 2 to n where n is entered by the user (we will call this Q1). When creating your Queue object use the correct function names for enqueue and dequeue functions. Again – sorry, cannot use an Javascript array...

  • JAVA LANG PLEASE: I have follwed these below guidelines but when i run my queue test...

    JAVA LANG PLEASE: I have follwed these below guidelines but when i run my queue test it is not executing but my stack is working fine, can you fix it please! MyQueue.java Implement a queue using the MyStack.java implementation as your data structure.  In other words, your instance variable to hold the queue items will be a MyStack class. enqueue(String item): inserts item into the queue dequeue(): returns and deletes the first element in the queue isEmpty(): returns true or false...

  • Please Answer this question using the language of C++. I provide you with the picture of figure 1...

    Please Answer this question using the language of C++. I provide you with the picture of figure 18_02. Thank you. I 7/ Fig. 18.2: Stack.h 2 // Stack class template. #ifndef #de fine 3 STACK-H STACK-H 5 #include 7 template 8 class Stack ( 9 public: 10 I const T& top) 12 13 l/ return the top element of the Stack return stack.frontO; // push an element onto the Stack void push(const T& pushValue) 15 stack push front (pushValue); 17...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT