-
-
Save douglas-vaz/2928890 to your computer and use it in GitHub Desktop.
Stack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fixed and working