Question

In: Computer Science

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:

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

#include <iostream>
#include<vector> 
using namespace std;

const int MAXSIZE=5;   //Define max size of stack vector


class Stack{

          int tp;             //top of stack
          vector<int> v;      //vector
    public:
     
     // Default Constructor

        Stack() 
           {
            tp=-1;
            v.reserve(MAXSIZE);
            }

// Push integer n onto top of stack

void push(int n) 
    {
        if(full())
          {
            cout<<"Stack Overflow"<<endl;
          }
        else {
             v[++tp]=n;              //if not full, element is inserted
             }
    }

// Pop element on top of stack

int pop() 
    {
    if(!empty())
     {
      return(v[tp--]);    // if not empty, top element is popped
     }
     else
     {
         cout<<"underflow"<<endl;
     }
    
    }

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

int top() 
    {
       if(!empty())
       return(v[tp]);     //if not empty, return top
    }

// Return whether the stack is empty or not

bool empty() 
       {
          return(tp==-1);   //if top==-1, the true is returned, else FALSE
       }
       
bool full() 
       {
          return(tp==(MAXSIZE-1)); //if full, true is returned, else FALSE
       }
       
void display()
{
    if(!empty())
          {
           cout<<endl<<"Stack elements are:";
           for(int i=tp; i>=0; i--)
               cout<<v[i]<<" ";
            cout<<endl;
           }
    else
        cout<<"Stack is empty"<<endl;
}

};


int main()
{
    int ch, val;
    Stack *ptr=new Stack();
   
   do {
       cout<<"1) Push in stack"<<endl;
       cout<<"2) Pop from stack"<<endl;
       cout<<"3) Display stack"<<endl;
       cout<<"4) check stack empty"<<endl;
       cout<<"5) check stack full"<<endl;
       cout<<"6) get stack top"<<endl;
       cout<<"7) Exit"<<endl;
       cout<<"Enter choice: "<<endl;
      cin>>ch;
      switch(ch) {
         case 1: {
            cout<<"Enter value to be pushed:"<<endl;
            cin>>val;
            ptr->push(val);
            break;
                }
         case 2: {
            if(!ptr->empty())
            {
            int pop1=ptr->pop();
            cout<<"popped element is::"<<pop1<<endl;
            }
            else
            cout<<"underflow"<<endl;
            break;
                 }
         case 3: {
            ptr->display();
            break;
                 }
         case 4: {
            if(ptr->empty())
            cout<<"stack empty"<<endl;
            else
            cout<<"stack not empty"<<endl;
            break;
                 }
         
         case 5: {
            if(ptr->full())
            cout<<"stack Full"<<endl;
            else
            cout<<"stack not Full"<<endl;
                }
            break;
            
        case 6: {
            if(!ptr->empty())
            cout<<"Stack top is"<<ptr->top()<<endl;
            else
            cout<<"stack empty"<<endl;
            break;
                }
         case 7: {
            cout<<"Exit"<<endl;
            break;
                }
         default: {
            cout<<"Invalid Choice"<<endl;
                  }
      }
   }while(ch!=7);
   return 0;
}

Related Solutions

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 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 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:...
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 =
Please use C language to code all of the problems below. Please submit a .c file...
Please use C language to code all of the problems below. Please submit a .c file for each of the solutions, that includes the required functions, tests you wrote to check your code and a main function to run the code. Q2. Implement the quick-sort algorithm.
Please code by C++ The required week2.cpp file code is below Please ask if you have...
Please code by C++ The required week2.cpp file code is below Please ask if you have any questions instruction: 1. Implement the assignment operator: operator=(Vehicle &) This should make the numWheels and numDoors equal to those of the object that are being passed in. It is similar to a copy constructor, only now you are not initializing memory. Don’t forget that the return type of the function should be Vehicle& so that you can write something like: veh1 = veh2...
C++ Please complete based on the code below. Declare another stack object to the code in...
C++ Please complete based on the code below. Declare another stack object to the code in main(). Add a stack operator called CopyStack to the Stack class which, when executed, copies the contents of the first stack into the second stack. Modify your menu so that this option is available. The menu should also allow the second stack to be printed, pushed, popped, and so forth, just like with the first stack. #include using namespace std; #define MAXSize 10 class...
USING C++ ONLY. Please study the code posted below. the goal is to rewrite the code...
USING C++ ONLY. Please study the code posted below. the goal is to rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same. /** * Queue implementation using linked list C style implementation ( no OOP). */ #include <cstdio> #include <cstdlib> #include <climits> #include <iostream> #define CAPACITY 100 // Queue max capacity using namespace std; /** Queue structure definition */ struct QueueType { int data; struct QueueType *...
C++ Fill in the blanks please You are required to fill in the blanks in this...
C++ Fill in the blanks please You are required to fill in the blanks in this program. Consider that: -In main, the variables price and quantity were given the names of p (double) and q (int), respectively. -In the getData function, the parameters associated with the main variables p and q were called pp and pq, respectively. // Prototype: Do not include the names // of the variables in the prototype void getData (FILLTHEBLANK , FILLTHEBLANK); int main () {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT