Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef STACK_H
- #define STACK_H
- #include <stdlib.h>
- typedef struct node {
- void *value;
- struct node *next;
- } node;
- typedef struct stack {
- node *head;
- int size;
- } stack;
- void push(stack *stk, void *val) {
- node *n = malloc(sizeof(node));
- n->value = val;
- n->next = stk->head == NULL ? NULL : stk->head; //either point to old head or become head
- stk->head = n; //become current head
- stk->size++;
- }
- void *pop(stack *stk) {
- if (stk->head == NULL) { //empty
- return NULL;
- }
- node *temp = stk->head; //temp pointer to old head
- void *val = temp->value; //get value
- stk->head = temp->next; //set new head
- free(temp); //free old head
- stk->size--;
- return val;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement