Implement Stack using Array...
#include<iostream>
#include <conio.h>
#define MAX 10
using namespace std;
void push();
void pop();
void display();
int stack[MAX], top=-1, item;
int main()
{ int ch;
do {
cout<<"Press\n1.\tPush\n2.\tPop\n3.\tDisplay\n4.\tExit\n";
cin>>ch;
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
return 0;
default:
cout<<"\n\nInvalid entry. Please try again...\n";
}
}
while(ch!=4);
getch();
return 0;
}
void push()
{
if(top == MAX-1)
cout<<"\n\n\t\t\tStack is full.";
else
{
cout<<"\nEnter ITEM: ";
cin>>item;
top++;
stack[top] = item;
cout<<item;
}
}
void pop()
{
if(top == -1)
cout<<"\n\n\t\t\tStack is empty.";
else
{
item = stack[top];
top--;
cout<<item;
}
}
void display()
{
int i;
if(top == -1)
cout<<"\n\n\t\t\tStack is empty.";
else
{
for(i=top; i>=0; i--)
cout<<stack[i];
}
}
Output:
#include <conio.h>
#define MAX 10
using namespace std;
void push();
void pop();
void display();
int stack[MAX], top=-1, item;
int main()
{ int ch;
do {
cout<<"Press\n1.\tPush\n2.\tPop\n3.\tDisplay\n4.\tExit\n";
cin>>ch;
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
return 0;
default:
cout<<"\n\nInvalid entry. Please try again...\n";
}
}
while(ch!=4);
getch();
return 0;
}
void push()
{
if(top == MAX-1)
cout<<"\n\n\t\t\tStack is full.";
else
{
cout<<"\nEnter ITEM: ";
cin>>item;
top++;
stack[top] = item;
cout<<item;
}
}
void pop()
{
if(top == -1)
cout<<"\n\n\t\t\tStack is empty.";
else
{
item = stack[top];
top--;
cout<<item;
}
}
void display()
{
int i;
if(top == -1)
cout<<"\n\n\t\t\tStack is empty.";
else
{
for(i=top; i>=0; i--)
cout<<stack[i];
}
}
Output: