Question

In: Computer Science

Given C++ Stack Code, Modify the code to work like a Queue.(First in First out) Stack...

Given C++ Stack Code, Modify the code to  work like a Queue.(First in First out) 

Stack
#ifndef STACK_H
#define STACK_H

#include "Node.h"


 
 template
 class Stack {
 private:
    
     Node* top;

 public:
     // Constructor
     Stack() {
      
         top = nullptr;
     }

    
    

    
     void push(Object ab) {
         
         if (top != nullptr) {
            
             Node* newNode = new Node(ab, *top);
   
             top = newNode;
         } else {
             
             Node* newNode = new Node(ab);
            
             top = newNode;
         }
     }

     
     Object pop() {
        
         if (top != nullptr) {
             Node *returnVal = top;
            
             top = top->getNext();
             Object returnObject = returnVal->getItem();
             
             delete returnVal; 
             return returnObject;
         } else {
            
             return Object();
         }
     }

    
     bool isThere(Object a) {
         Node* Copy = top;
        
         while (Copy != nullptr) {
             if (Copy->getItem() == a) {
                 
                 return true;
             }
             
             Copy = Copy->getNext();
         }
        
         return false;
     }


    
 };


#endif //STACK_H

Solutions

Expert Solution

QUEUE:
#include "Node.h" 
 template
 class Queue {
 private:    
     Node* front;
     Node* rear;
 public:
     // Constructor
     Queue() {      
         front = nullptr;
         rear = nullptr;
     }
     void push(Object ab) {
         
         if (top != nullptr) {            
             Node* newNode = new Node(ab, *rear);   
             rear = newNode;
         } else {             
             Node* newNode = new Node(ab);            
             front = newNode;
             rear = newNode;
         }
     }     
     Object pop() {        
         if (front != nullptr) {
             Node *returnVal = front;            
             front = front->getNext();
             Object returnObject = returnVal->getItem();             
             delete returnVal; 
             return returnObject;
         } else {            
             return Object();
         }
     }    
     bool isThere(Object a) {
         Node* Copy = front;        
         while (Copy != nullptr) {
             if (Copy->getItem() == a) {                 
                 return true;
             }             
             Copy = Copy->getNext();
         }        
         return false;
     }    
 };


Related Solutions

C++ Given Stack Code, Implements Queue. enqueue, dequeue. Modify to function like Queue. Stack #ifndef STACK_H...
C++ Given Stack Code, Implements Queue. enqueue, dequeue. Modify to function like Queue. Stack #ifndef STACK_H #define STACK_H #include "Node.h" template class Stack { private: Node* top; public: // Constructor Stack() { top = nullptr; } void push(Object ab) { if (top != nullptr) { Node* newNode = new Node(ab, *top); top = newNode; } else { Node* newNode = new Node(ab); top = newNode; } } Object pop() { if (top != nullptr) { Node *returnVal = top; top...
I want to know 'Linked link + queue' and 'Linked link Stack' code in C++ But...
I want to know 'Linked link + queue' and 'Linked link Stack' code in C++ But there is a condition. There shall be the following 'menu': *****Menu***** 1. Insert into Stack 2. Insert into Queue 3. Delete from Stack 4. Delete from Queue 5. View Content in Stack 6. View Content in Queue ********************************************** Let me give you a example. >> 1 30 // Insert 30 into Stack >>1 58// Insert 58 into Stack >>1 26 // Insert 26 into...
Implement a stack using a single queue. In particular, you are given a queue Q that...
Implement a stack using a single queue. In particular, you are given a queue Q that provides the method Q.size() to return its size at any point and the standard methods of queues (i.e, Q.enqueue(x) and Q.dequeue()). The requirement is to use such methods of Q to implement two methods S.push(x) and S.pop() for a stack S. What are the running times of your methods? Kindly answer using python programming
In C++ In this lab we will be creating a stack class and a queue class,...
In C++ In this lab we will be creating a stack class and a queue class, both with a hybrid method combining linked list and arrays in addition to the Stack methods(push, pop, peek, isEmpty, size, print) and Queue methods (enqueue, deque, peek, isEmpty, size, print). DO NOT USE ANY LIBRARY, implement each method from scratch. Both the Stack and Queue classes should be generic classes. Don't forget to comment your code.
Stacks & Queues C++ You are given a stack of N integers such that the first...
Stacks & Queues C++ You are given a stack of N integers such that the first element represents the top of the stack and the last element represents the bottom of the stack. You need to pop at least one element from the stack. At any one moment, you can convert stack into a queue. The bottom of the stack represents the front of the queue. You cannot convert the queue back into a stack. Your task is to remove...
(based on 8.38) A stack is a sequence container type that, like a queue, supports very...
(based on 8.38) A stack is a sequence container type that, like a queue, supports very restrictive access methods: all insertions and removals are from one end of the stack, typically referred to as the top of the stack. A stack is often referred to as a list-in first-out (LIFO) container because the last item inserted is the first removed. Implement a Stack class. It should support the following methods/functions: _init__ - Can construct either an empty stack, or initialized...
In this lab, using C++, you will create two data structures: a stack and a queue....
In this lab, using C++, you will create two data structures: a stack and a queue. You will use STL containers to demonstrate basic ADTs. Queue For the queue, you will simulate a buffer. Remember it is first-in-first-out. The user will enter a number for the number of rounds to run your simulation. You need one function that randomly generates a number. You will also have a user specified percentage, and the function uses this percentage to randomly put the...
C++ language We are given a Queue data structure that supports standard operations like Enqueue() and...
C++ language We are given a Queue data structure that supports standard operations like Enqueue() and Dequeue(): Enqueue(element): add a new element at the tail of the queue; Dequeue(): delete the element at the head of the queue. Show how to implement a stack using two queues. Analyze the running time of the stack operations: Push and Pop.
Solve in C++ program. Modify the use of queue data structure such that the array used...
Solve in C++ program. Modify the use of queue data structure such that the array used to implement the queue is dynamically allocated for a fast food autoservice
C++ Modify this to use a separate Boolean member to keep track of whether the queue...
C++ Modify this to use a separate Boolean member to keep track of whether the queue is empty rather than require that one array position remain empty. #include <stdio.h> #include <stdlib.h> #include <limits.h> // A structure to represent a queue struct Queue { int front, rear, size; unsigned capacity; int* array; }; // function to create a queue of given capacity. // It initializes size of queue as 0 struct Queue* createQueue(unsigned capacity) { struct Queue* queue = (struct Queue*)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT