Skip to content

Instantly share code, notes, and snippets.

@kgabis
Created September 14, 2011 16:17
Show Gist options
  • Save kgabis/1216992 to your computer and use it in GitHub Desktop.
Save kgabis/1216992 to your computer and use it in GitHub Desktop.
Linked stack in C
#include "..\Header\stack.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
void stackinit(Stack * stack)
{
stack->top = NULL;
stack->top->next = NULL;
stack->isempty = true;
}
void stackpush(Stack * stack, int value)
{
if(stack->top == NULL)
stack->isempty = false;
stack->top->next = stack->top;
stack->top = malloc(sizeof(Node));
stack->top->value = value;
}
int stackpop(Stack * stack)
{
Node * temp;
int value = stack->top->value;
if(stack->top->next == NULL)
{
stack->isempty = true;
}
temp = stack->top;
stack->top = stack->top->next;
free(temp);
return value;
}
void stackdispose(Stack * stack)
{
while(! stack->isempty)
stackpop(&stack);
}
typedef struct node_t
{
int value;
struct node_t * next;
} Node;
typedef struct stack_t
{
Node * top;
int isempty;
} Stack;
void stackinit(Stack*);
void stackpush(Stack*, int);
int stackpop(Stack*);
void stackdispose(Stack*);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment