Created
August 21, 2020 18:06
-
-
Save MNoorFawi/67499926c50026773862cedbbc20493e to your computer and use it in GitHub Desktop.
Stack data structure implementation in C
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 "stack.h" | |
| void create_stack(stack * st) { | |
| st -> indx = -1; | |
| st -> size = 0; | |
| } | |
| // check empty | |
| int is_empty(stack * st) { | |
| if (st -> indx == -1) | |
| return 1; | |
| else | |
| return 0; | |
| } | |
| // push/append | |
| void push(stack * st, int new_val) { | |
| st -> indx++; | |
| st -> val[st -> indx] = new_val; | |
| st -> size++; | |
| } | |
| // pop | |
| void pop(stack * st) { | |
| if (is_empty(st)) { | |
| puts("\n\t STACK IS EMPTY"); | |
| } else { | |
| printf("Value %i is popped from stack\n", st -> val[st -> indx]); | |
| st -> indx--; | |
| st -> size--; | |
| } | |
| } | |
| // print stack | |
| void print_stack(stack * st) { | |
| int i; | |
| for (i = 0; i < st -> size; i++) { | |
| printf("%i ", st -> val[i]); | |
| } | |
| printf("\n"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment