Make a simple linked list

Write your question here.
I am trying to learn about linked lists, and this function makeList() is supposed to create and return a linked list using input from a list of strings. To be honest, I'm kind of lost. Any help would be greatly appreciated.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct Node{
    string val;
    Node* next;
};

Node* makeList ()
{
    string current;
    Node* n;
    Node* head= NULL;
    Node* temp = n;

    while(cin>>current && !cin.fail())
    {
        n = new Node;
        n->val = current;
        temp ->next = n;
        temp = temp -> next;
    }
    n->next = NULL;

    return n;
}
> I'm kind of lost.
the error messages are getting more and more esoteric.


`n' is not initialized so `temp' in line 11 is pointing to garbage,
later you try to dereference `temp'
Topic archived. No new replies allowed.