Last active
January 8, 2017 11:24
-
-
Save chris-marsh/011585f3e50f01dd70a630e79631d598 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 TRUE 1 | |
#define FALSE 0 | |
typedef struct { | |
char *key; | |
char *value; | |
} Option; | |
typedef struct Stack { | |
Option option; | |
struct Stack *next; | |
} Stack; | |
char *strdup (const char *source_str) | |
{ | |
char *dest_str = malloc (strlen(source_str) + 1); | |
if (dest_str != NULL) strcpy (dest_str,source_str); | |
return dest_str; | |
} | |
void push_option(Stack **head, char *key, char *value) | |
{ | |
Option option = { strdup(key), strdup(value) }; | |
Stack *stack = malloc(sizeof(Stack)); | |
stack->option = option; | |
stack->next = *head; | |
*head = stack; | |
} | |
Option pop_option(Stack **head) | |
{ | |
Option option = {}; | |
if (head != NULL) { | |
Stack *prevStack; | |
prevStack = *head; | |
*head = (*head)->next; | |
option = prevStack->option; | |
free(prevStack); | |
} | |
return option; | |
} | |
void free_option(Option option) | |
{ | |
free(option.key); | |
free(option.value); | |
} | |
int main(void) | |
{ | |
Stack *stack = NULL; | |
Option option; | |
push_option(&stack, "A", "First"); | |
push_option(&stack, "B", "Second"); | |
push_option(&stack, "C", "Third"); | |
push_option(&stack, "D", "Fourth"); | |
while(stack != NULL) { | |
option = pop_option(&stack); | |
printf("Got option ... %s = %s\n", option.key, option.value); | |
free_option(option); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment