Skip to content

Instantly share code, notes, and snippets.

@r00takaspin
Created January 30, 2016 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r00takaspin/dd30d09614f1f377cb0a to your computer and use it in GitHub Desktop.
Save r00takaspin/dd30d09614f1f377cb0a to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_DATA 100
struct StackElement {
struct StackElement *head;
char data[MAX_DATA];
};
struct Stack {
struct StackElement *head;
};
struct Stack *stack = NULL;
void Stack_push(const char *data)
{
struct StackElement *el = malloc(sizeof(struct StackElement));
char *res = strncpy(el->data, data, MAX_DATA);
el->head = stack->head;
stack->head = el;
}
struct StackElement *Stack_pop()
{
struct StackElement *el = stack->head;
if (stack->head != NULL)
{
free(stack->head);
stack->head = el->head;
return el;
}
else
{
return NULL;
}
}
int main()
{
stack = malloc(sizeof(struct Stack));
stack->head = NULL;
int word_num = 5;
char *words[] = {"one", "two", "three", "four", "five"};
for(int i=0; i < word_num; i++)
{
Stack_push(words[i]);
}
//exit(1);
struct StackElement *el = malloc(sizeof(struct StackElement));
while((el = Stack_pop())!=NULL)
{
printf("%s\n",el->data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment