Created
July 4, 2019 02:32
-
-
Save surinoel/9b65ff0976d07b649527566343cf7197 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <malloc.h> | |
struct node { | |
int data; | |
struct node *link; | |
}; | |
typedef struct node stack; | |
stack *getnode() | |
{ | |
stack *tmp; | |
tmp = (stack *)malloc(sizeof(stack)); | |
tmp->link = NULL: | |
return tmp; | |
} | |
void push(stack **top, int data) | |
{ | |
stack *tmp; | |
tmp = *top; | |
*top = getnode(); | |
(*top)->data = data; | |
(*top)->link = tmp; | |
} | |
int pop(stack **top) | |
{ | |
stack *tmp; | |
int num; | |
tmp = *top; | |
if(tmp == NULL) { | |
printf("stack is empty!\n"); | |
return 0; | |
} | |
num = tmp->data; | |
*top = (*top)->link; | |
free(tmp); | |
return num; | |
} | |
int main(void) | |
{ | |
stack *top = NULL; | |
push(&top, 10); | |
push(&top, 20); | |
push(&top, 30); | |
printf("%d\n", pop(&top)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment