Skip to content
Snippets Groups Projects
Commit 8a391167 authored by Florian Burgener's avatar Florian Burgener
Browse files

Refactoring

parent 0513733e
Branches
No related tags found
No related merge requests found
......@@ -14,59 +14,65 @@
#include "stack.h"
void sort_array(int *array, int array_length) {
stack s_a;
stack_init(&s_a, array_length);
stack s_b;
stack_init(&s_b, array_length);
for (int i = 0; i < array_length; i += 1) {
// Pop values from left stack to right stack.
while (!stack_is_empty(s_a)) {
int tmp;
stack_peek(s_a, &tmp);
if (array[i] < tmp) {
break;
}
void pop_left_push_right(stack *left_stack, stack *right_stack, int current_value) {
while (!stack_is_empty(*left_stack)) {
int tmp;
stack_peek(*left_stack, &tmp);
stack_pop(&s_a, &tmp);
stack_push(&s_b, tmp);
if (current_value < tmp) {
break;
}
// Pop values from right stack to left stack.
while (!stack_is_empty(s_b)) {
int tmp;
stack_peek(s_b, &tmp);
stack_pop(left_stack, &tmp);
stack_push(right_stack, tmp);
}
}
if (array[i] > tmp) {
break;
}
void pop_right_push_left(stack *right_stack, stack *left_stack, int current_value) {
while (!stack_is_empty(*right_stack)) {
int tmp;
stack_peek(*right_stack, &tmp);
stack_pop(&s_b, &tmp);
stack_push(&s_a, tmp);
if (current_value > tmp) {
break;
}
stack_pop(right_stack, &tmp);
stack_push(left_stack, tmp);
}
}
void sort_array(int *array, int array_length) {
stack left_stack;
stack_init(&left_stack, array_length);
stack right_stack;
stack_init(&right_stack, array_length);
for (int i = 0; i < array_length; i += 1) {
// Pop values from left stack to right stack.
pop_left_push_right(&left_stack, &right_stack, array[i]);
// Pop values from right stack to left stack.
pop_right_push_left(&right_stack, &left_stack, array[i]);
// Push to left stack.
stack_push(&s_a, array[i]);
stack_push(&left_stack, array[i]);
}
// Until right stack is empty pop value from right stack and push it to left stack.
while (!stack_is_empty(s_b)) {
while (!stack_is_empty(right_stack)) {
int tmp;
stack_pop(&s_b, &tmp);
stack_push(&s_a, tmp);
stack_pop(&right_stack, &tmp);
stack_push(&left_stack, tmp);
}
// Until the left stack is empty, pop the value and place it in the array.
for (int i = 0; i < array_length; i += 1) {
int tmp;
stack_pop(&s_a, &tmp);
stack_pop(&left_stack, &tmp);
array[i] = tmp;
}
stack_destroy(&s_a);
stack_destroy(&s_b);
stack_destroy(&left_stack);
stack_destroy(&right_stack);
}
void print_array(int *array, int array_length) {
......@@ -77,9 +83,14 @@ void print_array(int *array, int array_length) {
}
int main(int argc, char **argv) {
if (argc < 2) {
printf("Too few arguments.\n");
return EXIT_FAILURE;
}
int array_length = atoi(argv[1]);
if (array_length != argc - 2) {
if (array_length != argc - 2 || array_length == 0) {
printf("The length of the array does not correspond to the number of elements.\n");
return EXIT_FAILURE;
}
......
......@@ -34,11 +34,19 @@ void stack_push(stack *s, int val) {
}
void stack_pop(stack *s, int *val) {
if (stack_is_empty(*s)) {
return;
}
stack_peek(*s, val);
s->top -= 1;
}
void stack_peek(stack s, int *val) {
if (stack_is_empty(s)) {
return;
}
*val = s.data[s.top];
}
......
......@@ -9,12 +9,11 @@
*
*/
#include "stack.h"
#include <assert.h>
#include <stdlib.h>
#include "stack.h"
int main() {
const int STACK_LENGTH = 10;
......@@ -45,6 +44,17 @@ int main() {
assert(s2.top == 2);
stack_destroy(&s2);
// Test : stack_push (size overflow)
stack s2b;
stack_init(&s2b, 2);
stack_push(&s2b, 1);
stack_push(&s2b, 2);
// Overflow from here.
stack_push(&s2b, 3);
stack_push(&s2b, 4);
assert(s2b.top == 1);
stack_destroy(&s2b);
// Test : stack_is_empty
stack s3;
stack_init(&s3, STACK_LENGTH);
......@@ -53,7 +63,7 @@ int main() {
assert(!stack_is_empty(s3));
stack_destroy(&s3);
// Test : stack_push
// Test : stack_pop
stack s4;
stack_init(&s4, STACK_LENGTH);
stack_push(&s4, 1);
......@@ -67,6 +77,14 @@ int main() {
assert(s4.top == -1);
stack_destroy(&s4);
// Test : stack_pop (stack is empty)
stack s4b;
stack_init(&s4b, STACK_LENGTH);
int val1b;
stack_pop(&s4b, &val1b);
assert(s4b.top == -1);
stack_destroy(&s4b);
// Test : stack_peek
stack s5;
stack_init(&s5, STACK_LENGTH);
......@@ -76,4 +94,12 @@ int main() {
assert(val2 == 1);
assert(s5.top == 0);
stack_destroy(&s5);
// Test : stack_peek (stack is empty)
stack s5b;
stack_init(&s5b, STACK_LENGTH);
int val2b;
stack_peek(s5b, &val2b);
assert(s5b.top == -1);
stack_destroy(&s5b);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment