Question

In: Computer Science

*In C++ Please! Give the full implementation of a constant member function that returns the second...

*In C++ Please!

Give the full implementation of a constant member function that returns the second element from the top of the stack without actually changing the stack. Write separate solutions for the two different stack versions. (C++ program for array implementation of stack with insert function and display of the second element from top)

Solutions

Expert Solution

#include <iostream>
using namespace std;
int stack[100], n=100, top=-1;
void push(int val) {
   if(top>=n-1)
   cout<<"Stack Overflow"<<endl;
   else {
      top++;
      stack[top]=val;
   }
}
void pop() {
   if(top<=-1)
   cout<<"Stack Underflow"<<endl;
   else {
      cout<<"The popped element is "<< stack[top] <<endl;
      top--;
   }
}
void display() {
   if(top>=1) {
      cout<<"The second element from the top is";
      cout<<stack[top-1]<<" ";
      cout<<endl;
   } else
   cout<<"Stack has only 1 element";
}
int main() {
   int ch, val;
   cout<<"1) Push in stack"<<endl;
   cout<<"2) Pop from stack"<<endl;
   cout<<"3) Display the second element from the top of stack"<<endl;
   cout<<"4) Exit"<<endl;
   do {
      cout<<"Enter choice: "<<endl;
      cin>>ch;
      switch(ch) {
         case 1: {
            cout<<"Enter value to be pushed:"<<endl;
            cin>>val;
            push(val);
            break;
         }
         case 2: {
            pop();
            break;
         }
         case 3: {
            display();
            break;
         }
         case 4: {
            cout<<"Exit"<<endl;
            break;
         }
         default: {
            cout<<"Invalid Choice"<<endl;
         }
      }
   }while(ch!=4);
   return 0;
}

Related Solutions

(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the...
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the nodes in a binary tree that have only one child. Convert it to an iterative version. in C++
Write a member function that deletes all repetitions from the bag. In your implementation, assume that...
Write a member function that deletes all repetitions from the bag. In your implementation, assume that items can be compared for equality using ==. Use the following header for the function: void remove_repetitions() Here is a brief outline of an algorithm: - A node pointer p steps through the bag - For each Item, define a new pointer q equal to p - While the q is not the last Item in the bag ◼ If the next Item has...
4.3 Lab: Queues 1 Write the c++ implementation of the following four member functions of the...
4.3 Lab: Queues 1 Write the c++ implementation of the following four member functions of the Queue class: getLength(): returns the number of elements in the queue isEmpty(): returns true if the queue is empty, false otherwise peek(): returns the value at the front of the queue without removing it. Assumes queue is not empty (no need to check) peekRear(): returns the value at the end of the queue without removing it. Assumes queue is not empty (no need to...
Which production function illustrates the case of constant returns to scale?
 A xY = F (zK, zL) where x <z  B  zY = F (zK, zL)  C  yY =F (zK, zL) where y>z  Which production function illustrates the case of constant returns to scale?  Which production function illustrates the case of decreasing returns to scale?  Which production function illustrates the case of increasing returns to scale?    The costs of expected inflation include (choose one or more)  A  shoeleather cost  B  menu costs  C  variability in relative prices leading to microeconomic inefficiencies in the...
C++ Write the implementation of the function concatenateIntArrays. This function receives 4 parameters in the following...
C++ Write the implementation of the function concatenateIntArrays. This function receives 4 parameters in the following order: An array of integer values (array1). An integer representing the size of array1 (size1). An array of integer values (array2). An integer representing the size of array2 (size). The function creates a dynamic array of integers of size size1+size2 to store all the values in array1, followed by all the values in array2. The function returns the pointer used to create the dynamic...
for directed un-weighted graph : Implement the following member function: int MyGraphAss3::findTopV(). This function returns a...
for directed un-weighted graph : Implement the following member function: int MyGraphAss3::findTopV(). This function returns a vertex that can reach all other vertices in the graph by a path starting from this vertex. There can be more than one top vertex in a graph. Just output one of them. (in c++)
c++ Write the implementation (.cpp file) of the Counter class of the previous exercise. The full...
c++ Write the implementation (.cpp file) of the Counter class of the previous exercise. The full specification of the class is: A data member counter of type int. An data member named counterID of type int. A static int data member named nCounters which is initialized to 0. A constructor that takes an int argument and assigns its value to counter. It also adds one to the static variable nCounters and assigns the (new) value of nCounters to counterID. A...
On the second tab build the full amortization table for a 15 year Constant Amortizing Mortgage...
On the second tab build the full amortization table for a 15 year Constant Amortizing Mortgage (CAM) Loan with a 6% interest rate compounded monthly. The initial loan amount should be $7,500,000. in excel
write a c++ member function that removes the first instance of a specific element in a...
write a c++ member function that removes the first instance of a specific element in a linked list and then return the size of the list after the removal whether it was successful or not.
write a c++ member function that removes the FIRST OCCURENCE of a SPECIFIC ELEMENT in a...
write a c++ member function that removes the FIRST OCCURENCE of a SPECIFIC ELEMENT in a linked list. After attemtped removal return the SIZE of the linked lost whether or not the removal was successful.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT