Program of Stack using array
This Program is displaying all the main functions of a stack like Push, Pop and display
#include<iostream.h>
#include<conio.h>
#include<process.h>
#define MAX 10
void push(int[],int);
int pop(int[]);
void disp(int[]);
int top=-1;
void main()
{
int arr[MAX];
int i;
char ans='y';
while(ans=='y')
{
clrscr();
cout<<"Operations on stacks are "<<endl;
cout<<"1. Push "<<endl;
cout<<"2. Pop "<<endl;
cout<<"3. Display "<<endl;
cout<<"Enter your choice "<<endl;
int ch;
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter an element to push"<<endl;
int x;
cin>>x;
push(arr,x);
break;
case 2:
int y;
y=pop(arr);
cout<<"The element deleted is"<<y<<endl;
break;
case 3:
disp(arr);
break;
}
cout<<"Do u wish to cont y/n"<<endl;
cin>>ans;
}
getch();
}
void push(int a[],int x)
{
if(top==(MAX-1))
{
cout<<"The stack is full"<<endl;
cout<<"Press any key to exit............"<<endl;
getch();
exit(0);
}
else
{
if(top==-1)
{
top=0;
}
else
{
top++;
}
a[top]=x;
}
}
int pop(int a[])
{
int z=a[top];
top--;
return z;
}
void disp(int a[])
{
int i;
for(i=top;i>=0;i--)
{
cout<<a[i]<<endl;
}
}
This Program is displaying all the main functions of a stack like Push, Pop and display
#include<iostream.h>
#include<conio.h>
#include<process.h>
#define MAX 10
void push(int[],int);
int pop(int[]);
void disp(int[]);
int top=-1;
void main()
{
int arr[MAX];
int i;
char ans='y';
while(ans=='y')
{
clrscr();
cout<<"Operations on stacks are "<<endl;
cout<<"1. Push "<<endl;
cout<<"2. Pop "<<endl;
cout<<"3. Display "<<endl;
cout<<"Enter your choice "<<endl;
int ch;
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter an element to push"<<endl;
int x;
cin>>x;
push(arr,x);
break;
case 2:
int y;
y=pop(arr);
cout<<"The element deleted is"<<y<<endl;
break;
case 3:
disp(arr);
break;
}
cout<<"Do u wish to cont y/n"<<endl;
cin>>ans;
}
getch();
}
void push(int a[],int x)
{
if(top==(MAX-1))
{
cout<<"The stack is full"<<endl;
cout<<"Press any key to exit............"<<endl;
getch();
exit(0);
}
else
{
if(top==-1)
{
top=0;
}
else
{
top++;
}
a[top]=x;
}
}
int pop(int a[])
{
int z=a[top];
top--;
return z;
}
void disp(int a[])
{
int i;
for(i=top;i>=0;i--)
{
cout<<a[i]<<endl;
}
}
No comments:
Post a Comment