Question: Code in C++, Make the code copyable, include screenshots Project 4: Stack-based Implementation of Que You are given the code skeleton for implementing a queue
Code in C++, Make the code copyable, include screenshots
Project 4: Stack-based Implementation of Que You are given the code skeleton for implementing a queue using Stack (that is in turn implemented using a Doubly Linked List). Complete the code for the enqueue(), dequeue() and isEmpty() functions of the Queue class. The code for the main function is also given to you. After you implement the above three functions, you can run your main function and capture screenshot. The queue size can range from 5 to 10 and the maximum value for an element in the queue can be 50. As you can notice, there are two Stacks (stack1 and stack2) declared as private member variables in the Queue class. You need to use these two Stacks for implementing the functionalities of a queue. I suggest the following design (you are free to choose your own design; provide a detailed explanation in your project report if your design is different from mine). Use stack1 to store the elements of the queue (with the invariant that the topmost element of stack1 is the element at the front of the queue and the bottommost element of stack1 is the element at the end of the queue) and use stack2 as an auxiliary data structure to implement the enqueue function. Since the topmost element of stack1 is the element in the front of the queue, the dequeue function can be simply implemented as the result of a pop operation on stack1. It is the enqueue function that needs to be thought out in greater detail and implemented. I suggest the following idea for enqueue of an integer 'data': First check if stack1 is empty or not. If it is empty, simply push the 'data' to it and return from the enqueue function. If stack1 is not empty to start with, then pop out all the elements of stack1 and push each of them to stack2. After stack1 gets empty, push the 'data' to it. Now, pop out all the elements of stack2 and push them back to stack1. As a result of this, the newly enqueued 'data' will be in the bottom of stack1. Submission (through Canvas): (1) Write a pseudo code of the enqueue function implemented for this problem. Explain how the invariant that "the topmost element of stack1 is the element at the front of the queue and the bottommost element of stack1 is the element at the end of the queue is maintained with each enqueue and dequeue operation." (2) Discuss the time complexity of the enqueue and dequeue implementations. (3) Include the complete C++ or Java code of the (Doubly Linked List-based) Stack-based Queue implementation, including the main function. (4) Show a screenshot of the execution of your implementation for a queue of size anywhere chosen from 5 to 10, with the maximum value of an element in the queue being 50
#include
// implementing a queue using Stack
class Node{ private: int data; Node* nextNodePtr; Node* prevNodePtr; public: Node(){} void setData(int d){ data = d; } int getData(){ return data; } void setNextNodePtr(Node* nodePtr){ nextNodePtr = nodePtr; } Node* getNextNodePtr(){ return nextNodePtr; } void setPrevNodePtr(Node* nodePtr){ prevNodePtr = nodePtr; } Node* getPrevNodePtr(){ return prevNodePtr; } };
class Stack{
private: Node* headPtr; Node* tailPtr;
public: Stack(){ headPtr = new Node(); tailPtr = new Node(); headPtr->setNextNodePtr(0); tailPtr->setPrevNodePtr(0); } Node* getHeadPtr(){ return headPtr; } Node* getTailPtr(){ return tailPtr; } bool isEmpty(){ if (headPtr->getNextNodePtr() == 0) return true; return false; } void push(int data){ Node* newNodePtr = new Node(); newNodePtr->setData(data); newNodePtr->setNextNodePtr(0); Node* lastNodePtr = tailPtr->getPrevNodePtr(); if (lastNodePtr == 0){ headPtr->setNextNodePtr(newNodePtr); newNodePtr->setPrevNodePtr(0); } else{ lastNodePtr->setNextNodePtr(newNodePtr); newNodePtr->setPrevNodePtr(lastNodePtr); } tailPtr->setPrevNodePtr(newNodePtr); }
int pop(){ Node* lastNodePtr = tailPtr->getPrevNodePtr(); Node* prevNodePtr = 0; int poppedData = -100000; //empty stack if (lastNodePtr != 0){ prevNodePtr = lastNodePtr->getPrevNodePtr(); poppedData = lastNodePtr->getData(); } else return poppedData; if (prevNodePtr != 0){ prevNodePtr->setNextNodePtr(0); tailPtr->setPrevNodePtr(prevNodePtr); } else{ headPtr->setNextNodePtr(0); tailPtr->setPrevNodePtr(0); }
return poppedData; } int peek(){ Node* lastNodePtr = tailPtr->getPrevNodePtr(); if (lastNodePtr != 0) return lastNodePtr->getData(); else return -100000; // empty stack } void IterativePrint(){ Node* currentNodePtr = headPtr->getNextNodePtr(); while (currentNodePtr != 0){ cout << currentNodePtr->getData() << " "; currentNodePtr = currentNodePtr->getNextNodePtr(); } cout << endl; } void ReversePrint(){ Node* currentNodePtr = tailPtr->getPrevNodePtr(); while (currentNodePtr != 0){ cout << currentNodePtr->getData() << " "; currentNodePtr = currentNodePtr->getPrevNodePtr(); } cout << endl; } };
class Queue{ private: Stack stack1; Stack stack2; public: Queue(){} void enqueue(int data){ // complete the code for the enqueue function } int dequeue(){ // complete the code for the dequeue function } bool isEmpty(){ // complete the code for the isEmpty function } };
int main(){
int queueSize; cout << "Enter the number of elements you want to insert: "; cin >> queueSize; Queue queue; // Create an empty queue srand(time(NULL)); int maxValue; cout << "Enter the maximum value for an element: "; cin >> maxValue;
for (int i = 0; i < queueSize; i++){ int value = rand() % maxValue; queue.enqueue(value); cout << value << " "; } cout << endl; while (!queue.isEmpty()){ cout << queue.dequeue() << " "; } cout << endl; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
