Skip to content

Instantly share code, notes, and snippets.

@surinoel
Created July 4, 2019 02:32
Show Gist options
  • Save surinoel/9b65ff0976d07b649527566343cf7197 to your computer and use it in GitHub Desktop.
Save surinoel/9b65ff0976d07b649527566343cf7197 to your computer and use it in GitHub Desktop.
#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