0

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!

6
  • 1
    Agreed, sorry for those tags. It's C indeed Commented Nov 15, 2013 at 20:52
  • 2
    @Manu343726 printf, NULL and calloc all 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 of calloc.) Commented Nov 15, 2013 at 20:54
  • For starters, t = t->c; doesn't make any sense. t is of type List * and t->c is of type char. Commented Nov 15, 2013 at 20:59
  • @Manu343726: It's not worse C++ code than it is C code, considering it's not valid code in either language as it is. Commented Nov 15, 2013 at 21:01
  • Lots of things do not make any sense here... Commented Nov 15, 2013 at 21:01

1 Answer 1

1

As you want to print input string in reverse it's easiest to store string in linked list in reverse order i.e. prepend characters as they are read to the beginning ("head") of list. So in the beginning list will be empty, then it will contain "H", then "eH", "leH" and so one. Here's samblo code:

List *getInput(void)
{
    List *l = NULL; // list head, we'll prepend nodes here
    int c;          // variable for current read character

    while ((c = getchar()) != EOF) {       // read characters one by ine, until end-of-file
        List *n = calloc(1, sizeof(List)); // create new list node
        n->c = c;     // store read character in that node
        n->next = l;  // prepend newly created node to our list
        l = n;        // store newly created node as head of list
    }

    return l;
}

Here's how you can print list:

void printList (List *l)
{
    while (l != NULL) { // while we have not reached end of list
        putchar(l->c);  // print character stored in list
        l = l->next;    // and advance to next list node
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.