Skip to content

Instantly share code, notes, and snippets.

@lnsp
Last active August 29, 2015 14:17
Show Gist options
  • Save lnsp/3847772569fc39568579 to your computer and use it in GitHub Desktop.
Save lnsp/3847772569fc39568579 to your computer and use it in GitHub Desktop.
A small Stack structure implementation
// A Stack structure implementation
// (c) 2015 mooxmirror
#include <stdlib.h>
#include <stdio.h>
#define STACK_TYPE int
#define STACK_TYPE_NULL 0
typedef struct stack stack;
typedef struct stack_item stack_item;
typedef STACK_TYPE stack_value;
struct stack {
stack_item * top;
};
struct stack_item {
stack_value value;
stack_item * next;
};
stack * stack_new();
void stack_delete(stack * s);
stack_value stack_peek(stack * s);
stack_value stack_pull(stack * s);
void stack_put(stack * s, stack_value v);
int stack_empty(stack * s);
/** IMPLEMENTATION **/
stack * stack_new() {
stack * s = (stack *) malloc(sizeof(stack));
(*s).top = NULL;
return s;
}
void stack_delete(stack * s) {
stack_item * current = (*s).top;
while (current != NULL) {
stack_item * next = (*current).next;
free(current);
current = next;
}
free(s);
}
stack_value stack_peek(stack * s) {
stack_item * top = (*s).top;
if (top != NULL)
return (*top).value;
return STACK_TYPE_NULL;
}
stack_value stack_pull(stack * s) {
stack_item * top = (*s).top;
if (top != NULL) {
stack_value value = (*top).value;
stack_item * tmp = (*top).next;
(*s).top = tmp;
free(top);
return value;
}
return STACK_TYPE_NULL;
}
void stack_put(stack * s, stack_value value) {
stack_item * top = (*s).top;
stack_item * next = malloc(sizeof(stack_item));
(*next).value = value;
(*next).next = top;
(*s).top = next;
}
int stack_empty(stack * s) {
return (*s).top == NULL;
}
/** RUNTIME **/
int main(void) {
stack * my_stack = stack_new();
stack_put(my_stack, 13);
printf("%d\n", stack_peek(my_stack));
stack_pull(my_stack);
stack_delete(my_stack);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment