Question

In: Computer Science

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 = 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

View comments (1)

Solutions

Expert Solution

Queue
#ifndef STACK_H
#define STACK_H

#include "Node.h"


 
 template
 class Stack {
 private:
    
     Node* top;

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

    
    

    // enqueue() operation will be same as push(), changes sholud be made only in pop() method to implement //dequeue()
     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() {
        Node * previous; // to previous node address
         Node * front; // to hold the address of front node
         front=top; //initialize front with top address
         if (front== nullptr) {    // zero node case
               return Object();
         }

          if(front.getNext() == nullptr) // single node case
           {
            top=nullptr;
             Object returnObject = front.getItem();
              delete front;
               return returnObject;  
            }

          while(front.getNext()!=nullptr)// more than one node case
           {
               previous=front;
                front=front.getNext();
            }
                 
             previous.Next=nullptr; /*include this line only if know what attribute is defined inside the Node struct/class to hold the link of next node in current node to set the second 
bottom-most node's next field to point nullptr. so replace the attribute "Next" by actual attribute that is defined in Node struct/class.*/
               Object returnObject = front.getItem();
              delete front;
               return returnObject;
     }

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


    
 };


#endif //STACK_H

Related Solutions

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 =...
Using Python list tools, create the standard stack (push, pop) and queue (enqueue, dequeue) operations Counting...
Using Python list tools, create the standard stack (push, pop) and queue (enqueue, dequeue) operations Counting Letter Challenge: Create a function that... Takes in a string as parameter Counts how often each letter appears in the string Returns a dictionary with the counts BONUS: make it so lowercase and uppercase letter count for the same letter
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.
Discuss the relative efficiency of the enqueue and dequeue operations for an array-based queue implemented with...
Discuss the relative efficiency of the enqueue and dequeue operations for an array-based queue implemented with a fixed-front approach as opposed to a floating-front approach.
Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method...
Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method * Complete the peek() method * No other methods/variables should be added/modified */ public class A3Queue {    /*    * Grading:    * Correctly adds an item to the queue - 1pt    */    public void enqueue(E val) {        /*        * Add a node to the list        */    }    /*    * Grading:   ...
Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method...
Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method * Complete the peek() method * No other methods/variables should be added/modified */ public class A3Queue {    /*    * Grading:    * Correctly adds an item to the queue - 1pt    */    public void enqueue(E val) {        /*        * Add a node to the list        */    }    /*    * Grading:   ...
In Java: Initiate empty queue of strings and recreate .isempty, .size, .dequeue, .enqueue methods. //You may...
In Java: Initiate empty queue of strings and recreate .isempty, .size, .dequeue, .enqueue methods. //You may not use the original methods of the stack api to answer. You may not add any more fields to the class. import java.util.NoSuchElementException; import edu.princeton.cs.algs4.Stack; public class StringQueue {    //You may NOT add any more fields to this class.    private Stack stack1;    private Stack stack2;    /**    * Initialize an empty queue.    */    public StringQueue() { //TODO   ...
True False The enqueue and dequeue operations in a priority queue take O(lg n) time, while...
True False The enqueue and dequeue operations in a priority queue take O(lg n) time, while linked list and array implementations take O(1) time. A binary heap is a (nearly) complete binary tree. Heaps usually use a linked node structure for implementation. When implementing heaps as arrays, you can leave a blank space at the front. If you do, the parent of a node at index i can be found at index floor(i/2). When implementing heaps as arrays, if you...
Give the algorithms in pseudo code of the enqueue and dequeue procedures. Constraint: the structure implementing...
Give the algorithms in pseudo code of the enqueue and dequeue procedures. Constraint: the structure implementing the queue must only be pointed to by a single field (example: head, tail or number of elements, etc.)
Task 3: Implement a queue through circular linked list. Develop enqueue, dequeue, status, isempty and isfull...
Task 3: Implement a queue through circular linked list. Develop enqueue, dequeue, status, isempty and isfull functions. Test your code in main function with 10 elements Note: for each task, you are supposed to create three files as specified in task1, i.e., implementation file, interface file and file containing point of entry. in C++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT