Linked List problem in displaying the result

closed account (oj1C54Gy)
I encountered a problem every time I try to run this code random numbers will always appear

#include<iostream>

using namespace std;

struct dataType{
string name;
int age;

dataType *next;
};

dataType *head, *pnew, *pc;

int main(){


//initialize
head = new dataType;
head -> next = NULL;
pc = head;

//add
for(char ans= 'y' ;ans != 'n';){

pnew = new dataType;
pc-> next = pnew;

cout<<"Name: ";
cin>>pnew -> name;
cout<<"Age: ";
cin>>pnew -> age;

pnew -> next = NULL;
pc = pnew;

cout<<"\n Do you want to add record? (y/n)";
cin>>ans;
}

//display
pc = head;
while (pc != NULL){

cout << pc->name << "\t" << pc->age << endl;
pc = pc->next;

}
system("pause");
return 0;

}

result:

Name: jay
Age: 21

Do you want to add record? (y/n)y
Name: rick
Age: 56

Do you want to add record? (y/n)y
Name: jade
Age: 20

Do you want to add record? (y/n)n
-842150451 //This is what I mean
jay 21
rick 56
jade 20
Last edited on
When you come to display, you are setting pc to point to the head object and you want to point it to the object that head points to...
1
2
3
    //display
    pc = head->next;
    while (pc != NULL) {
A more 'normal' implementation is to have head point to the first node - not being a distinct node with no data. Perhaps:

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
#include <iostream>
#include <string>

struct dataType {
	std::string name;
	int age {};

	dataType* next {};
};

int main() {
	dataType* head {};

	for (char ans = 'y'; ans != 'n'; ) {
		const auto pnew {new dataType};

		std::cout << "Name: ";
		std::cin >> pnew->name;

		std::cout << "Age: ";
		std::cin >> pnew->age;

		pnew->next = head;
		head = pnew;

		std::cout << "\nDo you want to add record? (y/n): ";
		std::cin >> ans;
	}

	for (auto pc {head}; pc != nullptr; pc = pc->next)
		std::cout << pc->name << "\t" << pc->age << '\n';
}

closed account (oj1C54Gy)
I also need to display the highest number so I added
1
2
3
4
int max = INT_MAX;

	if (max > pc->age)
	   max = pc->age;

but its not working
Try initialising max to zero not INT_MAX. int max = 0; or int max{}; etc.
Last edited on
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
#include<iostream>

using namespace std;

struct DataNode
{
    string name;
    int age{-99};
    
    DataNode *next;
};

int main()
{
    DataNode* head{nullptr};
    DataNode* pnew{nullptr};
    DataNode* pc{nullptr};
    
    //initialize
    head = new DataNode;
    head -> next = nullptr;
    pc = head;
    
    //add
    for(char ans= 'y' ;ans != 'n';)
    {
        pnew = new DataNode;
        pc-> next = pnew;
        
        cout<<"Name: ";
        cin>>pnew -> name;
        cout<<"Age: ";
        cin>>pnew -> age;
        
        pnew -> next = nullptr;
        pc = pnew;
        
        cout<<"\n Do you want to add record? (y/n)";
        cin>>ans;
    }
    
    //display
    int max_age{0};
    
    pc = head -> next;
    while (pc != nullptr)
    {
        if(pc->age > max_age)
            max_age = pc->age;
           
           cout << pc->name << "\t" << pc->age << endl;
        pc = pc->next;
    }
    
    cout << "Maximum age: " << max_age << '\n';
    
    return 0;
}



Name: aa
Age: 11

 Do you want to add record? (y/n)y
Name: bb
Age: 7

 Do you want to add record? (y/n)y
Name: cc
Age: 14

 Do you want to add record? (y/n)n
aa	11
bb	7
cc	14
Maximum age: 14
Program ended with exit code: 0
Topic archived. No new replies allowed.