really confuse me. pls use c++, thank u so much Enter the full contents of your
ID: 3592739 • Letter: R
Question
really confuse me. pls use c++, thank u so much
Explanation / Answer
#include<iostream.h>
#include<conio.h>
#include<process.h>
#define MAX 5
class Stack
{
int top;
int arr[MAX];
int IsEmpty()
{
if(top == -1)
return 1;
else
return 0;
}
int IsFull()
{
if(top == MAX-1)
return 1;
else
return 0;
}
public :
Stack()
{
top = -1;
}
void Push();
void Pop();
void Peek();
void Display();
};
void Stack :: Push()
{
if(IsFull())
{
cout<<" The stack is full. Cannot push new item.";
getch();
return;
}
else
{
cout<<" Enter the number : ";
cin>>arr[++top];
}
}
void Stack :: Pop()
{
if(IsEmpty())
{
cout<<" The stack is empty. Cannot pop.";
getch();
return;
}
else
top--;
}
void Stack :: Peek()
{
if(IsEmpty())
{
cout<<" The stack is empty. Cannot peek.";
getch();
return;
}
else
cout<<" The top item is : "<<arr[top];
}
void Stack :: Display()
{
int i;
if(IsEmpty())
{
cout<<" The stack is empty. Cannot display.";
getch();
return;
}
else
{
for(i = top ; i >= 0 ; i--)
cout<<" "<<arr[i];
}
}
void main(void)
{
Stack obj;
int ch;
while(1)
{
clrscr();
cout<<" MENU";
cout<<" ____";
cout<<" 1. Push.";
cout<<" 2. Pop.";
cout<<" 3. Peek.";
cout<<" 4. Display in LIFO.";
cout<<" 5. Exit.";
cout<<" Enter your choice : ";
cin>>ch;
switch(ch)
{
case(1) : obj.Push();
break;
case(2) : obj.Pop();
break;
case(3) : obj.Peek();
break;
case(4) : obj.Display();
break;
case(5) : cout<<" Exiting...";
getch();
exit(0);
default : cout<<" Invalid input.";
getch();
}
getch();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.