I have to write program which stores string entered by user into linked list and then prints it in reverse - f.e if user enters Hello. output should be .olleH.
I don't quite get the whole idea of lists however I came up with something, would really appreciate any feedback.
typedef struct L {
char c;
struct L *next;
}List;
List *getInput( void ) {
List *t = calloc(1, sizeof(List));
int i;
for (i=0; getchar() != '.'; i++) {
t->c = getchar();
t->next = NULL;
printf("%c", t->c);
t = t->c;
t->next = t->next->next;
}
return t;
}
int main ( void ) {
getInput();
return 0;
}
For now I've just tried to store it in the list t, character by character using getchar(). Then I would like to print it using another for loop counting backwards. For some reasons it's not working though and me (not fully understanding concept of lists) cannot figure out why.
Appreciate any help guys!
printf,NULLandcallocall exist in C++ whether you like them or not. (Not that I think this should be tagged as C++, but it is nearly valid C++ apart from not casting the return value ofcalloc.)t = t->c;doesn't make any sense.tis of typeList *andt->cis of typechar.