As you may have noticed, you have no means to access the list (if there was any) from outside InsertChar()
. You don't use a global variable, nor do you input or output it.
A better implementation:
item * InsertChar(item ** phead, char s) {
item * curr;
// First, allocate a new item and fill it.
curr = malloc(sizeof(item)); // no need to cast here
if (curr) { // only if malloc() succeeds
curr->value = s;
curr->next = *phead;
*phead = curr;
}
return curr;
}
// InsertChar() is only supposed to insert, not to print.
void PrintList(item * head) {
item * curr = head;
while(curr) {
printf("%c", curr->value); // omit the line break as you want abc
curr = curr->next;
}
printf("\n"); // now the line break
return;
// alternative implementation for while loop:
for(curr=head; curr; curr=curr->next) {
printf("%c\n", curr->value);
}
}
void FreeList(item * head) {
item * curr = head;
while(curr) {
item * next = curr->next; // read it out before freeing.
free(curr);
curr = next;
}
}
so that you can do now
int main() {
item * list = NULL; // empty for now, no contents.
char success = 1;
success = success && InsertChar(&list, 'a');
success = success && InsertChar(&list, 'b');
success = success && InsertChar(&list, 'c');
if (!success) {
printf("Oops?");
FreeList(list);
return 1;
}
PrintList(list);
FreeList(list); // clean up.
}
Oops? I didn't test it, but it seems to me that it prints "cba". Why does it so? Well, InsertChar() puts everything to the start.
How to get around of this?
Either we can create an AppendChar() function. But this bears the danger that we into the trap of Schlemiel the Painter's algorithm: to start searching for the right place always from the start. Thus, I'll point out another approach:
int main() {
item * list = NULL; // empty for now, no contents.
item * cursor = InsertChar(&list, 'a');
if (!cursor) goto error;
// cursor now contains our first entry.
// We put the next one to cursor->next:
cursor = InsertChar(&cursor->next, 'b');
if (!cursor) goto error;
cursor = InsertChar(&cursor->next, 'c');
if (!cursor) goto error;
PrintList(list);
FreeList(list); // clean up.
return 0;
error:
printf("Oops?");
FreeList(list);
return 1;
}
I am not sure if I am right (didn't test it), but this should be the way to go.
If you are one of these who were taught that goto is evil under all circumstances, feel free to implement the error handling in a different way.