Skip to content

Instantly share code, notes, and snippets.

@ibeauregard
Last active April 3, 2021 15:29
Show Gist options
  • Save ibeauregard/95cf991e96172cee92e7a384943434fb to your computer and use it in GitHub Desktop.
Save ibeauregard/95cf991e96172cee92e7a384943434fb to your computer and use it in GitHub Desktop.
Introducing header files
/* int_stack.h */
typedef struct int_node IntNode;
typedef struct int_stack {
IntNode* top;
} IntStack;
struct int_node {
int value;
struct int_node* next;
};
void push_to_stack(IntStack* stack, int value); // Forward declaration
int pop_stack(IntStack* stack); // Forward declaration
/* main.c */
#include "int_stack.h"
#include <stdio.h>
#include <stdlib.h>
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;
}
/* int_stack.c */
#include "int_stack.h"
void push_to_stack(IntStack* stack, int value)
{
// Implementation
}
int pop_stack(IntStack* stack)
{
// Implementation
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment