Starting with linked lists

I want help in writting the code to display this list on the screen

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 #include<iostream>
#include<cassert>

using namespace std;
struct nodeType 
{
    int info;
    nodeType *link;
};
nodeType *head, *p, *q, *newNode;

nodeType *buildListForward()
{
    nodeType *first, *newNode, *last;
    int num;
    first = NULL;

    while(num != -999)
    {
        newNode = new nodeType;
        assert(newNode != NULL);

        newNode->info = num;
        newNode->link = NULL;

        if(first == NULL)
        {
            first = newNode;
            last = newNode;
        }
        else
        {
            last->link = newNode;
            last= newNode;
        }
        cin >> num;
    }// end while
    return first;
} //end buildListForward

int main()
{
    buildListForward();
//    how can I display this list?
}
first you need a list :)

nodetype list = buildListForward(); //you currently call it in main but don't save the result.

then you need a display function, that will look roughly like this:

temp = list;
if(temp == null) cout << "empty list";
while(temp != null)
{
cout << temp.info;
temp = temp->link; //link is almost always named next by unspoken convention.
}

Last edited on
I am getting this error :
 
error: conversion from 'nodeType*' to non-scalar type 'nodeType' requested

on this line:
 
nodeType list = buildListForward(); //you currently call it in main but don't save the result. 
nodeType *list = buildListForward(); //the function returns a pointer. sorry about that above.
Your first node contains an uninitialized number. You can fix this by changing the loop to
while (cin >> num && num != -999)

You can simplify the building code by using a pointer-to-pointer. Not only does this do away the if statement in the loop, it lets you null-terminate the list just once at the end of building it, even if the list is empty.

To print the list (or any time you have to go through it) I prefer using a for loop. It separates the iteration code from the "do something for each item" code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<iostream>
#include<cassert>

using namespace std;
struct nodeType 
{
    int info;
    nodeType *link;
};
nodeType *head, *p, *q, *newNode;

nodeType *buildListForward()
{
    nodeType *first, *newNode, **pp = &first;
    int num;

    while(cin >> num && num != -999)
    {
        newNode = new nodeType;
        assert(newNode != NULL);

        newNode->info = num;
	*pp = newNode;
	pp = &newNode->link;
    }// end while
    *pp = NULL;			// null-terminate the list
    return first;
} //end buildListForward

int main()
{
    nodeType *head = buildListForward();

    if (head == NULL) {
	cout << "empty list\n";
    } else {
	for (nodeType *p = head; p; p=p->link) {
	    cout << p->info << ' ';
	}
	cout << '\n';
    }
}

The coding of dhayden runs very well!!!

What impressed me there was the use of a pointer to a pointer. I have never seen that before.
There is nothing like pointer-to-pointer in my textbook.
Any recommended lesson about pointers to pointers would be appreciated
The pointer-to-pointer trick makes deleting items really easy too.

The "big take away lessons" here are:
1. Textbooks teach to the least common denominator. I think this technique doesn't show up in textbooks because many languages don't have the "address of" operator, which is necessary to do this.

2. Pointers aren't just for dynamic memory. This is one example. Another one was on this forum a year or two ago. I can't find the post, but the questioner needed to do the same operation on variables from a wide range of places (function parameters, class members, local variables etc. He wanted to know if there was a way to avoid doing
1
2
3
4
5
func(var1);
func(var2);
func(var3);
...
func(varN);

The solution was to create an array of pointers to the variables and then loop through the array.
Topic archived. No new replies allowed.