-
-
Save ibeauregard/0b766868479d449266a5e630f4177564 to your computer and use it in GitHub Desktop.
Introduce the peek function
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
/* int_stack.h */ | |
// ... | |
struct int_node { | |
int value; | |
struct int_node* next; | |
}; | |
// Other function declarations... | |
int peek_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 (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" | |
// Other definitions | |
int peek_stack(IntStack* stack) | |
{ | |
return stack->top->value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment