Skip to content

Instantly share code, notes, and snippets.

@tanayseven
Created June 14, 2012 07:57
Show Gist options
  • Save tanayseven/2928879 to your computer and use it in GitHub Desktop.
Save tanayseven/2928879 to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include<malloc.h>
struct stack
{
int info;
struct stack *next;
};
typedef struct stack *s;
void push(s sptr ,int p)
{
s s2;
s2=(s)malloc(sizeof(struct stack));
if(s2==NULL)
{
printf("no memory");
exit(1);
}
else
{
s2->info=p;
s2->next=sptr;
sptr=s2;
}
}
void pop(s sptr)
{
s s2=sptr;
int px;
if(sptr==NULL)
{
printf("stack is empty");
exit(1);
}
else
{
px=sptr->info;
sptr=sptr->next;
free(sptr);
}
}
void disp(s sptr)
{
s temp=sptr;
printf("the elements in stack are:\n");
while(sptr->next!=NULL)
{
printf("%d\n",sptr->info);
sptr=sptr->next;
}
sptr=temp;
}
void main()
{
int c,i;
s s1=NULL;
do{
printf("enter operation:-");
scanf("%d",&c);
switch(c)
{
case 1: printf("enter data:-");
scanf("%d",&i);
push(s1,i);
break;
case 2:pop(s1);break;
case 3:disp(s1); break;
}
}while(s1!=NULL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment