Printing linked lists with struct

I think that my information is loading into the list, but I can't verify it because I can't get the list to print. This is for school.

[#include <iostream>
#include <string>
using namespace std;


struct node
{
int trackNo;
string animalName;
double boardCharge;
node *next;
};

//***********************
//Function prototypes
//***********************
void insert(node *curr);
void showList(node *head);



int main()
{
node *curr = new node;
node *head = new node;


//**********************************************
//Variable to deteermine whether to repeat or not
//***********************************************
char ContinuationFlag = 'Y';
char Nextchar = '0';


node *temp = new node;

while (toupper(ContinuationFlag) == 'Y')
{
cout<<"Input the tracking number of the animal"<<endl;
cin>>temp->trackNo;
cout<<"Input the name of the animal"<<endl;
cin>>temp->animalName;
cout<<"Input the charge for boarding the animal"<<endl;
cin>>temp->boardCharge;

insert(temp);

cout<<endl<<"Do you wish to enter another item? "<<endl;
cout<<"Enter 'Y' or 'N' "<<endl;

cin>>ContinuationFlag;
}
showList(temp);

return 0;
}

void insert(node *curr)
{
node *temp = new node;
node *head = new node;

curr->next = NULL;
curr->next = head;
head=temp;

head->next = temp;
head=head->next;

}
void showList(node *head)
{

node *curr = new node;
curr=head;
while(curr != NULL)
{
cout<<curr->trackNo<<endl;
cout<<curr->animalName<<endl;
cout<<curr->boardCharge<<endl;
curr=curr->next;
}
}
Please use code tags.
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <string>
using namespace std;


struct node
{
int trackNo;
string animalName;
double boardCharge;
node *next;
};

//***********************
//Function prototypes
//***********************
void insert(node *curr);
void showList(node *head);



int main()
{
node *curr = new node;
node *head = new node;


//**********************************************
//Variable to deteermine whether to repeat or not
//***********************************************
char ContinuationFlag = 'Y';
char Nextchar = '0';


node *temp = new node;

while (toupper(ContinuationFlag) == 'Y') 
{
cout<<"Input the tracking number of the animal"<<endl;
cin>>temp->trackNo;
cout<<"Input the name of the animal"<<endl;
cin>>temp->animalName;
cout<<"Input the charge for boarding the animal"<<endl;
cin>>temp->boardCharge;

insert(temp);

cout<<endl<<"Do you wish to enter another item? "<<endl;
cout<<"Enter 'Y' or 'N' "<<endl;

cin>>ContinuationFlag;
}
showList(temp);

return 0;
}

void insert(node *curr)
{
node *temp = new node;
node *head = new node;

curr->next = NULL;
curr->next = head;
head=temp;

head->next = temp;
head=head->next;

}
void showList(node *head)
{

node *curr = new node;
curr=head;
while(curr != NULL)
{
cout<<curr->trackNo<<endl;
cout<<curr->animalName<<endl;
cout<<curr->boardCharge<<endl;
curr=curr->next;
}
}
I don't know what a "code tag" is.
When you reply to a topic or create a topic there is a format section to the right of the text area... the symbol <> will produce a code tag which when you paste your code in between the tag will output the code like this
Thank you
changes that should work...

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
line 17: void insert(node *head, node *curr);
line 24: remove that line, you dont need curr
line 26: head = NULL; // initialize head
line 51: temp = temp->next = NULL; //add this before you close the loop

//you have to create a new node for every iteration.
node *temp;
while (toupper(ContinuationFlag) == 'Y')
{
    temp = new node;
    //...
}

line 46: insert(head,temp);  //new insert function
line 53: showList(head); //print from first.

//complete makover of insert
void insert(node *head,node *curr)
{
    if(head == NULL)   //if there is no head
    {
           head = curr;
    }
    else
    {
        //look for last node.
        // I assume you are adding to the end of the list
        node *temp = new node;
        node *previous = new node;
        for(temp = head; temp != NULL; temp = temp->next)
        {
           previous = temp;
        }
        previous = curr;
    }
} 
Last edited on
We are putting nodes in the front. Nothing is getting to the show function. I have traced, but i don't see where.

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <string>
using namespace std;


struct node
{
	int trackNo;
	string animalName;
	double boardCharge;
	node *next;
};

//***********************
//Function prototypes
//***********************
void insert(node *head, node *curr);
void showList(node *head);



int main()
{
	   node *head = new node;

		head = NULL;
 //**********************************************
//Variable to deteermine whether to repeat or not
//***********************************************
char ContinuationFlag = 'Y';


			node*temp;	 
				
				//********************
				//input of information
				//********************
    while (toupper(ContinuationFlag) == 'Y')  
		

			{
			temp = new node;
				cout<<"Input the tracking number of the animal"<<endl;
				cin>>temp->trackNo;
				cout<<"Input the name of the animal"<<endl;
				cin>>temp->animalName;
				cout<<"Input the charge for boarding the animal"<<endl;
				cin>>temp->boardCharge;
				
                //*******************************
				//send to function that captures list
				//**********************************
                insert(head,temp);
				

				//*******************
				//ask user if they want to continue
				//******************************
                cout<<endl<<"Do you wish to enter another item? "<<endl;
				cout<<"Enter 'Y' or 'N' "<<endl;
				
	            cin>>ContinuationFlag;
			}		

		
	//*********************************
	//when user is finish, displays list
	//*********************************
    showList(head);
	
    return 0;
}

void insert(node *head, node *curr)
{
	
		if(head == NULL)
		{
			head = curr;
			
		}
		else
		{
			node *temp = new node;
			temp =NULL;
			temp=head;
			temp=temp->next;
			curr = temp->next;
			head=curr;
			
		}
				
	      return; 
}
void showList(node *head)
{
	node *curr = new node;

    curr=head;
    while(curr != NULL)
	{
        cout<<curr->trackNo<<endl;
        cout<<curr->animalName<<endl;
        cout<<curr->boardCharge<<endl;
		curr=curr->next;
		
	}
}
Topic archived. No new replies allowed.