Stack
Stack
h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
struct node
{
int info;
struct node *next;
};
typedef struct node *stack;
int isempty(stack);
stack push(stack, int);
stack pop(stack);
void display(stack);
void main()
{
stack s=NULL;
int x;
char ch='1';
clrscr();
while(ch!='4')
{
printf("\n1 - Push");
printf("\n2 - Pop");
printf("\n3 - Display");
printf("\n4 - Quit");
printf("\n\n Enter your choice");
fflush(stdin);
ch=getchar();
switch(ch)
{
case '1':
printf("Enter the element to be pushed");
scanf("%d",&x);
s=push(s,x);
break;
case '2':
s=pop(s);
break;
case '3':
display(s);
break;
case '4':
break;
default:
printf("\n Wrong choice!try again");
}
}
}
int isempty(stack s)
{
if(s==NULL)
return 1;
else
return 0;
}
stack push(stack s,int x)
{
stack temp;
temp=(stack)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\n out of memory space");
exit(0);
}
temp->info=x;
temp->next=s;
s=temp;
return s;
}
stack pop(stack s)
{
stack temp;
int x;
if(isempty(s))
{
printf("\n stack empty");
exit(0);
}
temp=s;
x=s->info;
printf("\n popped element is %d",x);
s=s->next;;
free(temp);
return s;
}
void display(stack s)
{
stack ptr;
ptr=s;
printf("\n the elements in the stack are");
if(ptr==NULL)
{
printf("\n stack is empty");
return;
}
while(ptr!=NULL)
{
printf("%d\n",ptr->info);
ptr=ptr->next;
}
}