Skip to content

Instantly share code, notes, and snippets.

@douglas-vaz
Forked from tanayseven/gist:2928879
Created June 14, 2012 07:58
Show Gist options
  • Save douglas-vaz/2928890 to your computer and use it in GitHub Desktop.
Save douglas-vaz/2928890 to your computer and use it in GitHub Desktop.
Stack
#include<stdio.h>
#include<stdlib.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;
}
}
int pop(s *sptr)
{
s s2=*sptr; //Why???
int px;
if(*sptr == NULL)
{
printf("stack is empty");
return;
}
else
{
px = (*sptr) -> info;
*sptr = (*sptr) -> next;
//free(sptr);
return px;
}
}
void disp(s *sptr)
{
s temp=*sptr;
printf("the elements in stack are:\n");
while(temp->next!=NULL)
{
printf("%d\n",(temp)->info);
(temp)=(temp)->next;
}
printf("%d\n",temp -> info);
}
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);
}
@douglas-vaz
Copy link
Author

Fixed and working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment