Skip to content

Instantly share code, notes, and snippets.

@ibeauregard
Last active April 3, 2021 15:28
Show Gist options
  • Save ibeauregard/43c4e209d6bec6fd5631c95cf27cfd1b to your computer and use it in GitHub Desktop.
Save ibeauregard/43c4e209d6bec6fd5631c95cf27cfd1b to your computer and use it in GitHub Desktop.
Introduce the is_empty function
/* int_stack.h */
#include <stdbool.h>
typedef struct int_node IntNode;
typedef struct int_stack {
IntNode* top;
} IntStack;
// Other function declarations...
bool is_empty_stack(IntStack* stack);
/* main.c */
#include "int_stack.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", peek_stack(stack));
while (!is_empty_stack(stack)) {
printf("`%d` was popped from the stack\n", pop_stack(stack));
}
free(stack); stack = NULL;
return 0;
}
/* int_stack.c */
#include "int_stack.h"
// ...
struct int_node {
int value;
struct int_node* next;
};
// Other definitions
bool is_empty_stack(IntStack* stack)
{
return !stack->top;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment