Created
November 28, 2012 17:42
-
-
Save RenatoUtsch/4162787 to your computer and use it in GitHub Desktop.
C Stack implementation
This file contains 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 <stdlib.h> | |
#include <string.h> | |
#include "stack.h" | |
typedef struct StackNode { | |
StackItem item; /** The data of this node. **/ | |
struct StackNode *next; /** The next node (the one below the top). **/ | |
} StackNode; | |
struct Stack { | |
size_t count; /** The number of items in the stack. **/ | |
StackNode *top; /** The top item of the stack. **/ | |
}; | |
Stack *stackCreate() | |
{ | |
/* Create a stack and set everything to the default values. */ | |
Stack *stack = (Stack *) malloc(sizeof *stack); | |
if(stack == NULL) | |
return NULL; | |
stack->count = 0; | |
stack->top = NULL; | |
return stack; | |
} | |
void stackDestroy(Stack *stack) | |
{ | |
stackClean(stack); | |
free(stack); | |
} | |
void stackClean(Stack *stack) | |
{ | |
while(!stackIsEmpty(stack)) | |
stackPop(stack); | |
} | |
bool stackIsEmpty(Stack *stack) | |
{ | |
return stack->top == NULL ? true : false; | |
} | |
size_t stackSize(Stack *stack) | |
{ | |
return stack->count; | |
} | |
StackItem stackTop(Stack *stack) | |
{ | |
return stack->top->item; | |
} | |
bool stackPush(Stack *stack, StackItem item) | |
{ | |
StackNode *newNode = (StackNode *) malloc(sizeof *newNode); | |
if(newNode == NULL) | |
return false; | |
newNode->item = item; | |
newNode->next = stack->top; | |
stack->top = newNode; | |
stack->count += 1; | |
return true; | |
} | |
StackItem stackPop(Stack *stack) | |
{ | |
StackNode *oldTop; | |
StackItem item; | |
if(stack->top == NULL) | |
return 0; /** @todo Make a better way to return this error. **/ | |
oldTop = stack->top; | |
item = oldTop->item; | |
stack->top = oldTop->next; | |
free(oldTop); | |
oldTop = NULL; | |
stack->count -= 1; | |
return item; | |
} |
This file contains 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
#ifndef STACK_H_INCLUDED | |
#define STACK_H_INCLUDED | |
#include <stdlib.h> | |
#if !defined(__bool_true_false_are_defined) && !defined(__cplusplus) | |
typedef int bool; | |
#define true 1 | |
#define false 0 | |
#define __bool_true_false_are_defined | |
#endif | |
#define StackItem char | |
typedef struct Stack Stack; | |
Stack *stackCreate(); | |
void stackDestroy(Stack *stack); | |
void stackClean(Stack *stack); | |
bool stackIsEmpty(Stack *stack); | |
size_t stackSize(Stack *stack); | |
StackItem stackTop(Stack *stack); | |
bool stackPush(Stack *stack, StackItem item); | |
StackItem stackPop(Stack *stack); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment