diff options
| author | mo khan <mo.khan@gmail.com> | 2020-07-05 11:55:53 -0600 |
|---|---|---|
| committer | mo khan <mo.khan@gmail.com> | 2020-07-05 11:55:53 -0600 |
| commit | 915320dd77fe495fe38f805eb29241eb49a49024 (patch) | |
| tree | a9fe23753d103cb2f8dd106911e3ad39f78afd2e /src | |
| parent | 080c0e2eaec96b2627b37c6eb9e5e34e6e65ee04 (diff) | |
Create program to showcase a min stack
Diffstat (limited to 'src')
| -rw-r--r-- | src/01/06/README.md | 10 | ||||
| -rw-r--r-- | src/01/06/main.c | 29 | ||||
| -rw-r--r-- | src/01/06/min_stack.c | 2 | ||||
| -rw-r--r-- | src/01/06/min_stack.h | 2 |
4 files changed, 35 insertions, 8 deletions
diff --git a/src/01/06/README.md b/src/01/06/README.md index f288e0f..c02bf45 100644 --- a/src/01/06/README.md +++ b/src/01/06/README.md @@ -3,14 +3,14 @@ Name: Mo Khan Student ID: 3431709 -1. Problem Statement: +## Problem Statement Design and implement a MinStack data structure that can store comparable elements and supports the stack operations `push(x)`, `pop()`, and `size()`, as well as the `min()` operation, which returns the minimum value currently stored in the data structure. All operations should run in constant time. -2. Description of the Code: -3. Errors and Warnings: -4. Sample Input and Output: -5. Discussion: +## Description of the Code +## Errors and Warnings +## Sample Input and Output +## Discussion diff --git a/src/01/06/main.c b/src/01/06/main.c index f90e37f..104a000 100644 --- a/src/01/06/main.c +++ b/src/01/06/main.c @@ -1,8 +1,35 @@ +#include "min_stack.h" #include <stdio.h> #include <stdlib.h> +void inspect(Stack *stack) +{ + Node *head = stack->head; + + while(head) { + printf("\t [%7d]\n", head->data); + head = head->next; + } +} + int main(int argc, char *argv[]) { - printf("=== COMP-272 - Assignment 1 - Question 1b ===\n"); + printf("=== COMP-272 - Assignment 1 - Question 6 ===\n"); + + Stack *stack = initialize(); + + for (int i = 0; i < 10; i++) { + int data = rand() % 100; + printf("Push: %d\n", data); + push(stack, data); + inspect(stack); + } + + while(size(stack) > 0) { + printf("Pop: %d\n", pop(stack)); + inspect(stack); + } + + free(stack); return 0; } diff --git a/src/01/06/min_stack.c b/src/01/06/min_stack.c index adb96c8..6f8511b 100644 --- a/src/01/06/min_stack.c +++ b/src/01/06/min_stack.c @@ -2,7 +2,7 @@ #include <stdlib.h> #include "min_stack.h" -Stack *initialize() { +Stack *initialize(void) { Stack *self = malloc(sizeof(Stack)); self->head = NULL; return self; diff --git a/src/01/06/min_stack.h b/src/01/06/min_stack.h index 567b0a4..442a63b 100644 --- a/src/01/06/min_stack.h +++ b/src/01/06/min_stack.h @@ -9,7 +9,7 @@ typedef struct { Node *head; } Stack; -Stack *initialize(); +Stack *initialize(void); void push(Stack *self, int data); int pop(Stack *self); int size(Stack *self); |
