Program:-
//Implementation of Stack using Array
#include<stdio.h>
#define MAX 10
int stack[10];
void push();
void pop();
void show();
int top;
void main()
{
int ch;
top=-1;
int temp;
do
{
printf("\n\t Main menu");
printf("\n 1:Insert");
printf("\n 2:Delete");
printf("\n 3:Show");
printf("\n 4:Exit");
printf("\n Enter Your Choice ");
scanf("%d",&ch);
switch(ch)
{
case 1: push(); break;
case 2: pop(); break;
case 3: show(); break;
case 4: temp=1; break;
default: printf("\n wrong choice");
}
}while(ch!=4 && temp!=1);
}
void push()
{
int item;
if(top==MAX-1)
printf("\n overflow");
else
{
printf("\n Enter the value ");
scanf("%d",&item);
top++;
stack[top]=item;
}
}
void pop()
{
int item;
if(top==-1)
printf("\n Underflow");
else
{
item=stack[top];
printf("\n Deleted element is %d",item);
top--;
}
}
void show()
{
int i;
if(top==-1)
printf("\n stack is empty");
else
{
printf("\n Elements of stack are \n");
for(i=0;i<=top;i++)
{
printf("%d\t",stack[i]);
}
}
}
Output:-
Main menu
1:Insert
2:Delete
3:Show
4:Exit
Enter Your Choice 1
Enter the value 10
Main menu
1:Insert
2:Delete
3:Show
4:Exit
Enter Your Choice 1
Enter the value 20
Main menu
1:Insert
2:Delete
3:Show
4:Exit
Enter Your Choice 1
Enter the value 30
Main menu
1:Insert
2:Delete
3:Show
4:Exit
Enter Your Choice 3
Elements of stack are
10 20 30
Main menu
1:Insert
2:Delete
3:Show
4:Exit
Enter Your Choice 2
Deleted element is 30
Main menu
1:Insert
2:Delete
3:Show
4:Exit
Enter Your Choice 3
Elements of stack are
10 20
Main menu
1:Insert
2:Delete
3:Show
4:Exit
//Implementation of Stack using Array
#include<stdio.h>
#define MAX 10
int stack[10];
void push();
void pop();
void show();
int top;
void main()
{
int ch;
top=-1;
int temp;
do
{
printf("\n\t Main menu");
printf("\n 1:Insert");
printf("\n 2:Delete");
printf("\n 3:Show");
printf("\n 4:Exit");
printf("\n Enter Your Choice ");
scanf("%d",&ch);
switch(ch)
{
case 1: push(); break;
case 2: pop(); break;
case 3: show(); break;
case 4: temp=1; break;
default: printf("\n wrong choice");
}
}while(ch!=4 && temp!=1);
}
void push()
{
int item;
if(top==MAX-1)
printf("\n overflow");
else
{
printf("\n Enter the value ");
scanf("%d",&item);
top++;
stack[top]=item;
}
}
void pop()
{
int item;
if(top==-1)
printf("\n Underflow");
else
{
item=stack[top];
printf("\n Deleted element is %d",item);
top--;
}
}
void show()
{
int i;
if(top==-1)
printf("\n stack is empty");
else
{
printf("\n Elements of stack are \n");
for(i=0;i<=top;i++)
{
printf("%d\t",stack[i]);
}
}
}
Output:-
Main menu
1:Insert
2:Delete
3:Show
4:Exit
Enter Your Choice 1
Enter the value 10
Main menu
1:Insert
2:Delete
3:Show
4:Exit
Enter Your Choice 1
Enter the value 20
Main menu
1:Insert
2:Delete
3:Show
4:Exit
Enter Your Choice 1
Enter the value 30
Main menu
1:Insert
2:Delete
3:Show
4:Exit
Enter Your Choice 3
Elements of stack are
10 20 30
Main menu
1:Insert
2:Delete
3:Show
4:Exit
Enter Your Choice 2
Deleted element is 30
Main menu
1:Insert
2:Delete
3:Show
4:Exit
Enter Your Choice 3
Elements of stack are
10 20
Main menu
1:Insert
2:Delete
3:Show
4:Exit
Enter Your Choice 4
No comments:
Post a Comment