Refactored 'length(stack)' and added test.

This commit is contained in:
Murilo Pereira
2011-02-16 22:42:49 -02:00
parent b1000cfb55
commit 3420045a35
2 changed files with 29 additions and 7 deletions

View File

@@ -45,15 +45,13 @@ bool empty(struct stack *stack) {
}
int length(struct stack *stack) {
struct stack *iterator = stack;
int length = 0;
int length;
if (!empty(stack)) {
length = 1;
while (iterator->next != NULL) {
length++;
iterator = iterator->next;
}
for (length = 1; stack->next; stack = stack->next, length++)
;
} else {
length = 0;
}
return(length);