Struct linked list with while statement

Hi, everyone i'm having some problems with an assignment. we are supposed to create a program using structured linked list, that uses one global variable to represent pointer to head, two functions input and print, both are to use "while" loop.

i'm having trouble kicking it out of the while loop. at this point I'm pretty much lost any help is 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

struct node
{
	int tracking;
	char animal[35];
	float charge;
	node *Next;
};

	node *head;

void inputfn ();
void printList(node *);

int main()
{
	inputfn();
	
	printList(head);

	return 0;

}

    void inputfn()
	{
	int num;
	node *nodePtr;
	nodePtr = new node;
	
	while(nodePtr != NULL)
	{
	
	cout << "Please enter the tracking number: " <<endl;
	cin >> nodePtr->tracking;
		
	cout << "Please enter the Type of Animal: " <<endl;
	cin >> nodePtr->animal;

	cout << "Please enter the Daily Boarding Charge: "<<endl;
	cin >> nodePtr->charge;

	nodePtr->Next = new node;
	nodePtr=nodePtr->Next;
	cout<<"To continue inputting data enter '1' or '0' to print list"<<endl;
	cin >> num;
	if(num = 0){
		nodePtr = NULL;
		
	}
	
	}}

	void printList( node *head)
{
	//nodeptr = head;

	cout<<left<<setw(17)<<setfill(' ')<< " Tracking Number "<<setw(40)<<setfill(' ')
		<<" Type of Animal "<<setw(22)<<setfill(' ')<<" Daily Boarding Charge "
		<<endl;

	while (head != NULL)
	{
		cout<<left<<setw(17)<<setfill(' ')<< head->tracking <<setw(40)<<setfill(' ')
		<< head->animal<< setw(22)<<setfill(' ')<< head->charge<< endl;
		head = head->Next;
	}
}
Last edited on
lines 48-49: If the user enters 1, you're fine since you continue to the top of the loop and the newly allocated node is modified. However, if the user enters 0, you're left with an empty extraneous node.

You never modify head to point to the new node as you create nodes.

Line 15: head is an uninitialized null pointer.

Line 24: You pass an uninitialized null pointer to your print function resulting in undefined behavior (probably a crash) printList() not printing the list.

Edit: Corrected uninitialized pointer for null pointer.





Last edited on
Ok still not understanding what I need to get out of loop line. Thanks for your help
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void inputfn()
{   int num;
	node *nodePtr;
	nodePtr = new node;
	
	while(nodePtr != NULL)
	{   cout << "Please enter the tracking number: " <<endl;
	    cin >> nodePtr->tracking;		
	    cout << "Please enter the Type of Animal: " <<endl;
	    cin >> nodePtr->animal;
    	cout << "Please enter the Daily Boarding Charge: "<<endl;
    	cin >> nodePtr->charge;
    	nodePtr->Next = head;   //  chain existing list (if any) to current node
	    head = nodePtr;         //  Set head to current node
	    cout<<"To continue inputting data enter '1' or '0' to print list"<<endl;
	    cin >> num;
	    if (num == 0)          // You were using assignment operator (=), not the equality operator (==)
	    {   nodePtr = NULL;
		    continue;           // return to top of loop
	    }
	    nodePtr = new node;     //  Now we can allocate new node for next iteration
	}
}

Line 15: head is an uninitialized pointer.

No, global and static POD are zero-initialized.

A slightly different way based on AbstractAnon's code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void
inputfn()
{
    int num=1;
    while (num == 1) {
	node *nodePtr = new node;
	cout << "Please enter the tracking number: " << endl;
	cin >> nodePtr->tracking;
	cout << "Please enter the Type of Animal: " << endl;
	cin >> nodePtr->animal;
	cout << "Please enter the Daily Boarding Charge: " << endl;
	cin >> nodePtr->charge;

	nodePtr->Next = head; //  chain existing list (if any) to current node
	head = nodePtr;	      //  Set head to current node

	cout << "To continue inputting data enter '1' or '0' to print list" << endl;
	cin >> num;
    }
}


It would be better to use a do-while loop but the OP specifically says while loops.
global and static POD are zero-initialized.

Oops. I knew that. Thanks.

The point still stands that the OP never modifies head, therefore, printList() will print only the header and will not print the list since printList() is passed a null pointer.



Topic archived. No new replies allowed.