Created
January 30, 2016 12:11
-
-
Save r00takaspin/dd30d09614f1f377cb0a to your computer and use it in GitHub Desktop.
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 <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