Pages - Menu

Monday, 21 July 2014

Implementation of Stack in C.

Here is the code.. 

//AIM : TO PERFORM PUSH ,POP AND DISPLAY OPERATION IN //STACK
#include<stdio.h>

#include<stdlib.h>
#define m 10
int stack[m],x[m];
int tos=-1,j=0;
void push();
void pop();
void display();
void main()
{
int choice,y=1;


while(y==1)
{
printf("enter your choice\n");
printf("1.push,2.pop,3.display\n");
scanf("%d",&choice);
switch(choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
default : printf("wrong choice");
exit(1);
}
printf("Any more opertion(1.yes/2.no)\n");
scanf("%d",&y);
}

}
void push()
{
if(tos==(m-1))
printf("OVERFLOW CAN NOT INSERT");
else
{
tos++;
printf("Enter the element");
scanf("%d",&stack[tos]);
}
}
void pop()
{ if(tos==-1)
printf("UNDERFLOW CAN NOT INSERT");
else
{
x[j]=stack[tos];
j++;
tos--;
}
}
void display()
{
int i;
for(i=tos;i>=0;i--)
printf("%d\t",stack[i]);
}
For Turbo C users.
//AIM : TO PERFORM PUSH ,POP AND DISPLAY OPERATION IN //STACK
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define m 10
int stack[m],x[m];
int tos=-1,j=0;
void push();
void pop();
void display();
void main()
{
int choice,y=1;

clrscr();
while(y==1)
{
printf("enter your choice\n");
printf("1.push,2.pop,3.display\n");
scanf("%d",&choice);
switch(choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
default : printf("wrong choice");
exit(1);
}
printf("Any more opertion(1.yes/2.no)\n");
scanf("%d",&y);
}
getch();
}
void push()
{
if(tos==(m-1))
printf("OVERFLOW CAN NOT INSERT");
else
{
tos++;
printf("Enter the element");
scanf("%d",&stack[tos]);
}
}
void pop()
{ if(tos==-1)
printf("UNDERFLOW CAN NOT INSERT");
else
{
x[j]=stack[tos];
j++;
tos--;
}
}
void display()
{
int i;
for(i=tos;i>=0;i--)
printf("%d\t",stack[i]);
}

No comments:

Post a Comment