Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(psuede-code) Show how to implement a Stack class that implements the usual push

ID: 648942 • Letter: #

Question

(psuede-code) Show how to implement a Stack class that implements the usual push, pop, and isEmpty functions plus a min function that returns the minimum item in the stack. All the functions must run in constant time (the execution time should be same regardless of the number of items on the stack. In other words, the functions should run just as fast on a stack with 1000 elements as on a stack with 1 element.) The requirement that the functions run in constant time means that you cannot implement the min function by searching over the elements in stack.

Explanation / Answer

#include<iostream.h>

#include<stdlib.h>

//#include<apstring.h>

//#include"apvector.h"

//#include"apstring.cpp"

class STACK

{

private:

int top;

int stack[10];

   int size;

public:

     

STACK();

void push(int x);

   int pop();

void display();

int full();

int empty();

};

STACK::STACK()

{

   top=0;

size=10;

for(int i=0; i<size; i++)

   {

stack[i]=0;

}

}

int STACK::full()

{

if(top==(size-1))

{

return 1;

}

return 0;

}

int STACK::empty()

{

if(top==0)

{

return 1;

}

return 0;

}

void STACK::push(int x)

{

if(full()==1)

{

cout<<"Stack Is Full"<<endl;

}

else

{

stack[top] = x;

top++;

}

}

int STACK::pop ()

{

int x=0;

if(empty()==1)

{

cout<<"Stack is Empty"<<endl;

}

   else

{

stack[top]=NULL;

x = stack[top];

top--;

}

return x;

}

void STACK::display()

{

cout<<"Show your Array : ";

for(int i=0; i<10; i++)

{

cout<<stack[i]<<" ";

}

}

//Main

void main ()

{

  

STACK a;

   int n=1;

int choice =0;

int array[10]={0};

int range=0;

int data=0;

int del=0;

while(n)

{

system("cls");

cout<<endl<<endl;

cout<<"1. For Push "<<endl;

cout<<"2. For PoP " <<endl;

cout<<"3. Display "<<endl;

cout<<"4. exit "<<endl;

cout<<"Choose Option = ";

cin>>choice;

if(choice==1)

{

system("cls");

cout<<"Enter the data : "<<endl;

for(int i=0;i<10; i++)

{

cin>>array[i];

a.push(array[i]);

}

}

else

if(choice==2)

{

system("cls");

cout<<"Range of Data of DELETE = ";

cin>>del;

for(int i=0; i<del; i++)

{

a.pop();

}

}

else

if(choice==3)

{

system("cls");

int x=0;

a.display();

}

else

if(choice==4)

{

n=0;

}

else

{

cout<<"Choose Correct Option"<<endl;

}

}

}