
#include<bits/stdc++.h>
using namespace std;
#define MAX 1000 //Maximum size of stack
class Stack{
int top;// to store top of stack
public:
int elements[MAX]; //Integer array to store elements
Stack(){ //class constructor for initialization
top=-1;
}
bool push(int x) {
if(top>=(MAX-1)) //Condition for top when stack because full
{
cout<<"Stack is full\n";
return false;
}
else
{
++top; //Increasing top value
elements[top]=x; //storing it to element in to stack
return true;
}
}
int pop(){
if(top<0)// Condition for top when stack is empty
{
cout<<"stack is empty\n";
return INT_MIN;
}
else
{
int x=elements[top--];//poping out the element for stack by decreasing to element
return x;
}
}
int display(){
if(top<0)//Condition for top when stack is empty
{
cout<<"Stack is empty\n";
return INT_MIN;
}
else{
for(int i=0;i<=top;i++)//running loop from start to top and printing elements
cout<<elements[i]<<"";
cout<<endl;
return 1;
}
}
};
int main()
{
cout<<"1.push\n 2. Pop\n 3. Display\n";
char newch;
int ch;
Stack mystack;//Stack class object
while(1){//running loop till ‘n’ is not entered.
cout<<"Enter your choice:\n";
cin>>ch;
if(ch==1){
int x;
cout<<"Enter an integer element to push:\n";
cin>>x;
mystack.push(x);
}
else if(ch==2){
int x=mystack.pop();
if(x!=INT_MIN)
cout<<"Element"<<x<<"popped out\n";
}
else if(ch==3){
mystack.display();
}
else
cout<<"!!!Wrong Choice entered!!!\n";
cout<<"Do you want to continue:\n";
cin>>newch;
if(newch=='n')
break;
}
return 0;
}
Can you give me the output result screenshot for this code representing every operation and satisfying the problem statement.
![**Problem Statement:**
We can store \( k \) stacks in a single array if we use the data structure suggested in Figure 1 for the case \( k = 3 \). We push and pop from each stack as suggested in connection with Figure 2 below. However, if pushing onto stack \( i \) causes \( \text{TOP}(i) \) to equal \( \text{BOTTOM}(i - 1) \), we first move all the stacks so that there is an appropriate size gap between each adjacent pair of stacks. For example, we might make the gaps above all stacks equal, or we might make the gap above stack \( i \) proportional to the current size of stack \( i \) (on the theory that larger stacks are likely to grow sooner, and we want to postpone as long as possible the next reorganization).
**Figures Explanation:**
- **Figure 1:** Illustrates three stacks labeled "stack 1," "stack 2," and "stack 3" within a single array. The array is divided into sections with "top" and "bottom" points. The diagram shows how stack space is allocated and rearranged when needed.
- **Figure 2:** Depicts a single stack with "top" and "max length" indicators. The elements are represented vertically from "first element" to "last element."
**Instructions:**
1. On the assumption that there is a procedure to reorganize the stacks when they collide, write the code for the five stack operations.
2. Suppose there is a procedure `MakeNewTops` that computes `newtop[i]`, the "appropriate" position for the top of stack \( i \), for \( 1 \leq i \leq k \). In this case, write the procedure `reorganize`.
- *Hint:* Note that stack \( i \) could move up or down, and it is necessary to move stack \( j \) before stack \( j \) if the new position of stack \( j \) overlaps the old position of stack \( i \). Consider stacks \( 1, 2, \ldots, k \) in order, but keep a stack of "goals," each goal being to move a particular stack. If on considering stack \( i \), we can move it safely, do so, and then reconsider the stack whose number is on top of the goal stack. If we](https://content.bartleby.com/qna-images/question/eddfdae4-4eb1-4173-94bc-7dfd3da3807a/f02bb28b-b7ba-46db-bb52-7977749f0066/0fgoikv_thumbnail.png)

Trending nowThis is a popular solution!
Step by stepSolved in 2 steps with 1 images

- #include <iostream> #include <queue> //This contains the STL's efficient implementation of a priority queue using a heap using namespace std; /* In this lab you will implement a data structure that supports 2 primary operations: - insert a new item - remove and return the smallest item A data structure that supports these two operations is called a "priority queue". There are many ways to implement a priority queue, with differernt efficiencies for the two primary operations. For this lab, please do not attempt (at least at first) to implement something fancy (like a heap). Just use your mind to do something simple. Analyze the efficiency of both insertion and removal for your implementation. Afterwards, feel free to investigagte "min heaps" to see a very clever and fast priority queue implementation. Feel free to try to implement it. After implementuing your priority queue, use it to implement a sorting algorithm (priorityQueueSort). Analyze the run time of your sorting…arrow_forwardStack Implementation in C++make code for an application that uses the StackX class to create a stack.includes a brief main() code to test this class.arrow_forwardQ2: Stack stores elements in an ordered list and allows insertions and deletions at one end.The elements in this stack are stored in an array. If the array is full, the bottom item is dropped from the stack. In practice, this would be equivalent to overwriting that entry in the array. And if top method is called then it should return the element that was entered recently Please do it in C++ and use stack class(header file), not premade functionarrow_forward
- Stack: Stacks are a type of container with LIFO (Last In First Out) type of working, where a new element is added at one end and (top) an element is removed from that end only. Your Stack should not be of the fixed sized. It should be able to grow itself. bool empty() : Returns whether the Stack is empty or not. Time Complexity should be: O(1) bool full() : Returns whether the Stack is full or not. Time Complexity should be: O(1)int size() : Returns the current size of the Stack. Time Complexity should be: O(1)Type top () : Returns the last element of the Stack. Time Complexity should be: O(1) void push(Type) : Adds the element of type Type at the top of the stack. Time Complexity should be: O(1) Type pop() : Deletes the top most element of the stack and returns it. Time Complexity should be: O(1) Write non-parameterized constructor for the above class. Write Copy constructor for the above class. Write Destructor for the above class. Now write a global function show stack which…arrow_forwardData Structure Using C++ I need a function like this ::: float postfixEval(string postfix) {int a, b;stack<float> stk;string::iterator it;for(it=postfix.begin(); it!=postfix.end(); it++) {//read elements and perform postfix evaluationif(isOperator(*it) != -1) {a = stk.top();stk.pop();b = stk.top();stk.pop();stk.push(operation(a, b, *it));}else if(isOperand(*it) > 0) {stk.push(checkNumber(*it));}}return stk.top();} to evaluate postfix with more one digit in expression (multi digits) please just this function simple like the abovearrow_forwardC programming fill in the following code #include "graph.h" #include <stdio.h>#include <stdlib.h> /* initialise an empty graph *//* return pointer to initialised graph */Graph *init_graph(void){} /* release memory for graph */void free_graph(Graph *graph){} /* initialise a vertex *//* return pointer to initialised vertex */Vertex *init_vertex(int id){} /* release memory for initialised vertex */void free_vertex(Vertex *vertex){} /* initialise an edge. *//* return pointer to initialised edge. */Edge *init_edge(void){} /* release memory for initialised edge. */void free_edge(Edge *edge){} /* remove all edges from vertex with id from to vertex with id to from graph. */void remove_edge(Graph *graph, int from, int to){} /* remove all edges from vertex with specified id. */void remove_edges(Graph *graph, int id){} /* output all vertices and edges in graph. *//* each vertex in the graphs should be printed on a new line *//* each vertex should be printed in the following format:…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





