diff --git a/Exercise 8 - Dynamic Stack (Week 9)/Ex8.pdf b/Exercise 8 - Dynamic Stack (Week 9)/Ex8.pdf new file mode 100644 index 0000000..d802045 Binary files /dev/null and b/Exercise 8 - Dynamic Stack (Week 9)/Ex8.pdf differ diff --git a/Exercise 8 - Dynamic Stack (Week 9)/Ex8.txt b/Exercise 8 - Dynamic Stack (Week 9)/Ex8.txt new file mode 100644 index 0000000..7953b1e --- /dev/null +++ b/Exercise 8 - Dynamic Stack (Week 9)/Ex8.txt @@ -0,0 +1,9 @@ +2 +push 1 +push 1 +push 1 +push 1 +push 1 +pop +pop +push 1 diff --git a/Exercise 8 - Dynamic Stack (Week 9)/Ex8_Prof_Solution.pdf b/Exercise 8 - Dynamic Stack (Week 9)/Ex8_Prof_Solution.pdf new file mode 100644 index 0000000..86dd5d2 Binary files /dev/null and b/Exercise 8 - Dynamic Stack (Week 9)/Ex8_Prof_Solution.pdf differ diff --git a/Exercise 8 - Dynamic Stack (Week 9)/ex8.c b/Exercise 8 - Dynamic Stack (Week 9)/ex8.c new file mode 100644 index 0000000..6741a88 --- /dev/null +++ b/Exercise 8 - Dynamic Stack (Week 9)/ex8.c @@ -0,0 +1,116 @@ +/* License: AGPLv3 or later. https://www.gnu.org/licenses/licenses.html + * + * Exercise 8 - Dynamic Stack (Week 9) + * Name: Manish + * Student Login: ***** + * + * Compile as: + * $ gcc -std=c11 -Wall -o ex8 ex8.c + */ + +#include +#include +#include +#include + +int* stack = NULL; +int index = -1; +int size = 0; + +void push(int n); +int pop(); +int top(); +bool isEmpty(); +int max(int i, int j); + +int main(void) +{ + printf("Enter file name: "); + // Assuming filename/file path will not be longer than 256 characters + char filename[257]; + scanf("%s", filename); + + FILE* file = fopen(filename, "r"); + if (!file) + { + perror(filename); + exit(EXIT_FAILURE); + } + + if (fscanf(file, " %d ", &size) != EOF) + { + stack = malloc(sizeof(int) * size); + if (size > 0 && stack == NULL) + { + fprintf(stderr, "Failed to allocate memory\n"); + return 1; + } + } + + // Read and push integers to stack + char instruction[5]; + int n; + while (fscanf(file, " %4s ", instruction) != EOF) + { + if (strcmp("push", instruction) == 0) + { + // whitespaces around to skip them inc. newlines + if (fscanf(file, " %d ", &n) != EOF) + push(n); + } + else + n = pop(); + } + + printf("Stack contains %d entries.\n", index + 1); + + fclose(file); + free(stack); + + return 0; +} + +void push(int n) +{ + if (index == size - 1) + { + // max() in case original size is 0 + int new_size = max(size * 2, 1); + int* new_stack = malloc(sizeof(int) * new_size); + if (new_stack == NULL) + { + fprintf( + stderr, + "Failed to double stack from %d to %d. Not pushing %d.\n", + size, + new_size, + n); + return; + } + printf("Stack doubled from %d to %d.\n", size, new_size); + memcpy(new_stack, stack, size*sizeof(int)); + free(stack); + stack = new_stack; + size = new_size; + } + stack[++index] = n; +} + +int pop() +{ + if (isEmpty()) + { + return -1; + } + return stack[index--]; +} + +bool isEmpty() +{ + return index < 0; +} + +int max(int i, int j) +{ + return (i > j) ? i : j; +} diff --git a/Exercise 8 - Dynamic Stack (Week 9)/makefile b/Exercise 8 - Dynamic Stack (Week 9)/makefile new file mode 100644 index 0000000..b32b0d6 --- /dev/null +++ b/Exercise 8 - Dynamic Stack (Week 9)/makefile @@ -0,0 +1,2 @@ +ex8: ex8.c + gcc -Wall -std=c11 -o ex8 ex8.c