Commit c1eba56
Changed files (3)
src
01
src/01/06/main.c
@@ -23,7 +23,7 @@ int main(int argc, char *argv[])
inspect(stack);
}
- printf("Bye");
+ printf("Bye\n");
free(stack);
return 0;
}
src/01/06/min_stack.c
@@ -22,14 +22,12 @@ int size(Stack *self) {
}
void push(Stack *self, int data) {
- if (self->min) {
- if (data < self->min->data) {
- Node *tmp = new(data);
- tmp->next = self->min;
- self->min = tmp;
- }
- } else {
+ if (!self->min)
self->min = new(data);
+ else if (data < self->min->data) {
+ Node *tmp = new(data);
+ tmp->next = self->min;
+ self->min = tmp;
}
Node *node = new(data);
@@ -72,10 +70,8 @@ int pop(Stack *self) {
Node *current = self->head;
int data = current->data;
- if (data == self->min->data) {
+ if (data == self->min->data)
self->min = self->min->next;
- }
-
self->head = current->next;
self->size--;
current->next = NULL;
src/01/06/README.md
@@ -12,5 +12,143 @@ All operations should run in constant time.
## Description of the Code
## Errors and Warnings
+
+```bash
+モ make run_test
+clang build/min_stack.o build/min_stack_test.o -lcgreen -o build/test
+cgreen-runner -c -v build/test
+Discovered MinStack:when_a_single_item_is_on_the_stack_it_has_a_size_of_one (CgreenSpec__MinStack__when_a_single_item_is_on_the_stack_it_has_a_size_of_one__)
+Discovered MinStack:when_a_single_item_is_on_the_stack_it_is_the_min (CgreenSpec__MinStack__when_a_single_item_is_on_the_stack_it_is_the_min__)
+Discovered MinStack:when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_a_min_of_null (CgreenSpec__MinStack__when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_a_min_of_null__)
+Discovered MinStack:when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_a_size_of_zero (CgreenSpec__MinStack__when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_a_size_of_zero__)
+Discovered MinStack:when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_the_item (CgreenSpec__MinStack__when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_the_item__)
+Discovered MinStack:when_empty (CgreenSpec__MinStack__when_empty__)
+Discovered MinStack:when_empty_it_has_a_min_of_null (CgreenSpec__MinStack__when_empty_it_has_a_min_of_null__)
+Discovered MinStack:when_empty_it_has_a_size_of_zero (CgreenSpec__MinStack__when_empty_it_has_a_size_of_zero__)
+Discovered MinStack:when_pushing_a_single_integer (CgreenSpec__MinStack__when_pushing_a_single_integer__)
+Discovered MinStack:when_pushing_duplicate_values_on_to_the_stack (CgreenSpec__MinStack__when_pushing_duplicate_values_on_to_the_stack__)
+Discovered MinStack:when_pushing_multiple_integers_out_of_order (CgreenSpec__MinStack__when_pushing_multiple_integers_out_of_order__)
+Discovered 11 test(s)
+Opening [build/test] to run all 11 discovered tests ...
+Running "test" (11 tests)...
+ "MinStack": 39 passes in 7ms.
+Completed "test": 39 passes in 7ms.
+```
+
## Sample Input and Output
+
+```bash
+モ make run
+clang -c -o build/main.o main.c
+clang build/min_stack.o build/main.o -o build/program
+./build/program
+=== COMP-272 - Assignment 1 - Question 6 ===
+Pushing:
+==========
+Push: 7, Min: 7
+ [7]
+Push: 24, Min: 7
+ [24][7]
+Push: 23, Min: 7
+ [23][24][7]
+Push: 8, Min: 7
+ [8][23][24][7]
+Push: 5, Min: 5
+ [5][8][23][24][7]
+Push: 22, Min: 5
+ [22][5][8][23][24][7]
+Push: 19, Min: 5
+ [19][22][5][8][23][24][7]
+Push: 3, Min: 3
+ [3][19][22][5][8][23][24][7]
+Push: 23, Min: 3
+ [23][3][19][22][5][8][23][24][7]
+Push: 9, Min: 3
+ [9][23][3][19][22][5][8][23][24][7]
+Push: 15, Min: 3
+ [15][9][23][3][19][22][5][8][23][24][7]
+Push: 15, Min: 3
+ [15][15][9][23][3][19][22][5][8][23][24][7]
+Push: 17, Min: 3
+ [17][15][15][9][23][3][19][22][5][8][23][24][7]
+Push: 17, Min: 3
+ [17][17][15][15][9][23][3][19][22][5][8][23][24][7]
+Push: 12, Min: 3
+ [12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
+Push: 3, Min: 3
+ [3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
+Push: 2, Min: 2
+ [2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
+Push: 4, Min: 2
+ [4][2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
+Push: 15, Min: 2
+ [15][4][2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
+Push: 12, Min: 2
+ [12][15][4][2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
+Push: 3, Min: 2
+ [3][12][15][4][2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
+Push: 19, Min: 2
+ [19][3][12][15][4][2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
+Push: 9, Min: 2
+ [9][19][3][12][15][4][2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
+Push: 7, Min: 2
+ [7][9][19][3][12][15][4][2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
+Push: 10, Min: 2
+ [10][7][9][19][3][12][15][4][2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
+Popping:
+==========
+ [10][7][9][19][3][12][15][4][2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
+Pop: 10, Min: 2
+ [7][9][19][3][12][15][4][2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
+Pop: 7, Min: 2
+ [9][19][3][12][15][4][2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
+Pop: 9, Min: 2
+ [19][3][12][15][4][2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
+Pop: 19, Min: 2
+ [3][12][15][4][2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
+Pop: 3, Min: 2
+ [12][15][4][2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
+Pop: 12, Min: 2
+ [15][4][2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
+Pop: 15, Min: 2
+ [4][2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
+Pop: 4, Min: 2
+ [2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
+Pop: 2, Min: 3
+ [3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
+Pop: 3, Min: 5
+ [12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
+Pop: 12, Min: 5
+ [17][17][15][15][9][23][3][19][22][5][8][23][24][7]
+Pop: 17, Min: 5
+ [17][15][15][9][23][3][19][22][5][8][23][24][7]
+Pop: 17, Min: 5
+ [15][15][9][23][3][19][22][5][8][23][24][7]
+Pop: 15, Min: 5
+ [15][9][23][3][19][22][5][8][23][24][7]
+Pop: 15, Min: 5
+ [9][23][3][19][22][5][8][23][24][7]
+Pop: 9, Min: 5
+ [23][3][19][22][5][8][23][24][7]
+Pop: 23, Min: 5
+ [3][19][22][5][8][23][24][7]
+Pop: 3, Min: 5
+ [19][22][5][8][23][24][7]
+Pop: 19, Min: 5
+ [22][5][8][23][24][7]
+Pop: 22, Min: 5
+ [5][8][23][24][7]
+Pop: 5, Min: 7
+ [8][23][24][7]
+Pop: 8, Min: 7
+ [23][24][7]
+Pop: 23, Min: 7
+ [24][7]
+Pop: 24, Min: 7
+ [7]
+Pop: 7, Min: 0
+
+Bye
+```
+
## Discussion