Question

In: Computer Science

C++ please Fill in for the functions for the code below. The functions will implement an...

C++ please

Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below:

Stack *ptr = new Stack();

ptr->push(value);

int pop1 = ptr->pop();

int pop2 = ptr->pop();

bool isEmpty = ptr->empty();

class Stack{

    public:

// Default Constructor

Stack() {// ... }

// Push integer n onto top of stack

void push(int n) {// ... }

// Pop element on top of stack

int pop() {// ... }

// Get the top element but do not pop it (peek at top of stack)

int top() {// ... }

// Return whether the stack is empty or not

bool empty() {// ... }

};

Solutions

Expert Solution

ANS:

#pragma once
class Stack
{
private:
   int* stackArray;
   int StackSize;
   int top;
public:
   Stack(int);
   ~Stack();
   void push(int);
   void pop(int&);
   bool isFull();
   bool isEmpty();

   int tops();

   void printStack();
};

//STACK CPP AND MAIN

#include "Stack.h"
#include<iostream>
using namespace std;

Stack::Stack(int len)
{
   stackArray = new int[len];
   StackSize = len;
   top = -1;
}
Stack::~Stack()
{
   delete[] stackArray; // deleteing stack dynamic array after program completion
}
void Stack::push(int item)
{
   if (isFull())
   {
       cout << "Stack is Full" << endl;
   }

   else
   {
       top += 1;
       stackArray[top] = item;
       cout << "Element Added in stack" << endl;
   }
}
void Stack::pop(int& num)
{
   if (isEmpty())
   {
       cout << "Stack is Empty" << endl; // If empty then only cout else pop
   }

   else
   {
       num = stackArray[top];
       top--;
   }

}
bool Stack::isFull()
{
   if (top == StackSize - 1)
   {
       return true; // If top is equal to end of array then it is full so return true
   }

   else
   {
       return false;
   }
}
bool Stack::isEmpty()
{
   if (top == -1)
   {
       return true; //if top=-1 it means no element in stack so empty
   }

   else
   {
       return false;
   }
}

int Stack::tops()
{
   if (isEmpty())
   {
       cout << "Stack is Empty" << endl;
       return -1;
   }
   else
   {
       return stackArray[top];
   }

}


void Stack::printStack()
{

   cout << "Visiting Stack and printing each element" << endl;
   int element;
   for (int i = 0; i < StackSize; i++)
   {
       pop(element);
       cout << "Element#" << i << "=" << element << endl;
   }

}

int main()
{
   Stack* ptr = new Stack(3);
   ptr->push(2);
   ptr->push(3);
   ptr->push(6);

   //ptr->printStack();

   int pop1;
   ptr->pop(pop1);

   int pop2;
   ptr->pop(pop2);

   cout << "Pop1:" << pop1 << " POP2:" << pop2 << endl;

  


}

Comment down for any queries
Please give a thumbs up if you are satisfied with answer :)


Related Solutions

C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer list using dynamic array ONLY (an array that can grow and shrink as needed, uses a pointer an size of array). Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: class List { public: // Default Constructor List() {// ... } // Push integer n onto...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
C++ Please Fill in for the functions for the code below. The functions will be implemented...
C++ Please Fill in for the functions for the code below. The functions will be implemented using vectors ONLY. Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public: // Default Constructor Stack() {// ... } // Push integer n onto top of...
C++ Please Fill in for the functions for the code below. The functions will be implemented...
C++ Please Fill in for the functions for the code below. The functions will be implemented using vectors. Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: ---the code can be general (can be tested with any int main() test function)--- Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public: //...
Please write python code for the following. Implement the functions defined below. Include a triple-quoted comments...
Please write python code for the following. Implement the functions defined below. Include a triple-quoted comments string at the bottom displaying your output. Using sets (described in Deitel chapter 6) will simplify your work. Below is the starter template for the code: def overlap(user1, user2, interests): """ Return the number of interests that user1 and user2 have in common """ return 0    def most_similar(user, interests): """ Determine the name of user who is most similar to the input user...
Fill in the code for the following C functions. Function srl performs a logical right shifting...
Fill in the code for the following C functions. Function srl performs a logical right shifting using arithmetic right shift (given by value xsra), followed by other operations not including right shifts or division. Function sra performs an arithmetic right shift using a logical right shift (given by value xsrl), followed by other operations not including right shift or division. you may use the computation 8*size of (int) to determine w, the number of bits in data type int. The...
Problem: Write a C++ program that will implement and test the five functions described below that...
Problem: Write a C++ program that will implement and test the five functions described below that use pointers and dynamic memory allocation. The Functions: You will write the five functions described below. Then you will call them from the main function, to demonstrate their correctness. 1. minimum: takes an int array and the array's size as arguments. It should return the minimum value of the array elements. Do not use square brackets anywhere in the function, not even the parameter...
[Hash Tables] Given the following code in C++, implement the functions in table2.cpp. The first 2...
[Hash Tables] Given the following code in C++, implement the functions in table2.cpp. The first 2 files (table2.h, short story text file) are all fully coded and commented for convenience. *****I have given references that I've completed previously for the table2.cpp file, I just need help applying them. Will provide good rating, thanks****** -------------------------------------------------------------------------------------------------------------------------- table2.h (given file, do not edit): // FILE: table2.h // // ABSTRACT BASE CLASS: Table //    1. The number of records in the Table is...
Fill in the blanks in the MATLAB code below.
Fill in the blanks in the MATLAB code below. (Do not type unnecessary words or blank spaces in your response. The correct answers are case-sensitive.) % Consider a row vector v. % Complete the lines of code below to find the average and standard deviation of the elements of vector v such that these two values are assigned to variables M and S, respectively. E = G =
CODE IN C++ PLEASE Write a program to implement the algorithm that you designed in Exercise...
CODE IN C++ PLEASE Write a program to implement the algorithm that you designed in Exercise 19 of Chapter 1. Your program should allow the user to buy as many items as the user desires. Instructions for Exercise 19 of Chapter 1 have been posted below for your convenience. Exercise 19 Jason typically uses the Internet to buy various items. If the total cost of the items ordered, at one time, is $200 or more, then the shipping and handling...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT