Linked List help!

Hello everyone, I need some help understanding this code. I (shamelessly) pulled this code from this tutorial: "http://www.baumann.info/public/cpp_lernaufgabe_linked_list.pdf" and tried to piece it together myself. I understand the use of a linked list, the way it works, but not implementing one and coding one. I'm sure there are some huge mistakes in this code, but I was hoping if someone could point them out because I'm stumped. I saved over a version of the code and it actually worked but then I wanted to try and add another node and I screwed it up. I think I put void add_node_at_end() inside int main() and it compiled. So my objectives are to get this code to work and understand how to keep adding nodes. Any help is much appreciated.

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
46
47
48
49
50
51
52
53
54
  #include <iostream>
using namespace std;

struct Node{

	char name[20]; //Name of up to 20 letters
	int age; //D.O.B would be better
	float height; //in meters!
	Node *next; //pointer to next node.

};


void add_node_at_end()
{
	Node *start_ptr = NULL;
	Node *temp, *temp2; // Temporary pointers 

	// Reserve space for new node and fill it with data 
	temp = new Node;
	cout << "Please enter the name of the person: ";
	cin >> temp->name;
	cout << "Please enter the age of the person : ";
	cin >> temp->age;
	cout << "Please enter the height of the person : ";
	cin >> temp->height;
	temp->next = NULL;
	

	// Set up link to this node 
	if (start_ptr == NULL)
		start_ptr = temp;
	else
	{
		temp2 = start_ptr; // We know this is not NULL - list not empty! 
		while (temp2->next != NULL)
			temp2 = temp2->next; // Move to next link in chain 
		temp2->next = temp;
	}
}



int main(){

	Node *temp;
		
		cout << "Name : " << temp->name << endl;
		cout << "Age : " << temp->age << endl;
		cout << "Height : " << temp->height << " Meters." << endl;
		cout << endl;

	
}
I am guessing you are trying to make a one way linked list

you make a lot of mistake perhaps you should reread that paper
I can't even figure out what you are trying to code just by looking at your code I have to read that paper

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  #include <iostream>
using namespace std;

struct Node{

	char name[20]; //Name of up to 20 letters
	int age; //D.O.B would be better
	float height; //in meters!
	Node *next; //pointer to next node.

};

Node *start_ptr = NULL;


void add_node_at_end()
{
	Node *temp, *temp2; // Temporary pointers

	// Reserve space for new node and fill it with data
	temp = new Node;
	cout << "Please enter the name of the person: ";
	cin >> temp->name;
	cout << "Please enter the age of the person : ";
	cin >> temp->age;
	cout << "Please enter the height of the person : ";
	cin >> temp->height;
	temp->next = NULL;


	// Set up link to this node
	if (start_ptr == NULL)
		start_ptr = temp;
	else
	{
		temp2 = start_ptr; // We know this is not NULL - list not empty!
		while (temp2->next != NULL)
			temp2 = temp2->next; // Move to next link in chain
		temp2->next = temp;
	}
}



int main(){
    int n = 0;
    cout << "number of input : "; cin >> n;
    for( int i = 0; i < n; ++ i )
        add_node_at_end();

    Node *temp = start_ptr;

    while( temp != NULL ){
        cout << "Name : " << temp->name << endl;
        cout << "Age : " << temp->age << endl;
        cout << "Height : " << temp->height << " Meters." << endl;
        cout << endl;
        temp = temp->next;
    }

    return 0;
}


I've corrected many part of the code
Wow thanks a lot for doing this code and even reading the paper! I'm taking a class on data structures and its taught in java is proving confusing. I really appreciate this and will go back to the paper as well.
Topic archived. No new replies allowed.