Skip to content

Instantly share code, notes, and snippets.

@ibeauregard
Last active April 24, 2021 11:37
Show Gist options
  • Save ibeauregard/7ec2b0949f26c46e261fd567ded95b42 to your computer and use it in GitHub Desktop.
Save ibeauregard/7ec2b0949f26c46e261fd567ded95b42 to your computer and use it in GitHub Desktop.
Step 0: Total transparency, no reuse potential
/* main.c */
#include <stdio.h>
#include <stdlib.h>
typedef struct int_node {
int value;
struct int_node* next;
} IntNode;
typedef struct int_stack {
IntNode* top;
} IntStack;
static void push_to_stack(IntStack* stack, int value)
{
IntNode* pushed = malloc(sizeof (IntNode));
pushed->value = value;
pushed->next = stack->top;
stack->top = pushed;
}
static int pop_stack(IntStack* stack)
{
IntNode* new_top = stack->top->next;
int popped = stack->top->value;
free(stack->top);
stack->top = new_top;
return popped;
}
int main()
{
IntStack* stack = malloc(sizeof (IntStack));
stack->top = NULL;
for (int i = 5; i > 0; i--) {
push_to_stack(stack, i);
}
printf("At the top of the stack is `%d`\n\n", stack->top->value);
while (stack->top) {
printf("`%d` was popped from the stack\n", pop_stack(stack));
}
free(stack); stack = NULL;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment