Linked List

closed account (zbX9z8AR)
I'm trying to set multiple islands that will each have a different population and for each person on an island, I need to be able to see their status.
For example, the user can say they want 5 islands with 30, 40, 50, 60, and 70 people for these islands. I must be able to go into each island and change a person's status. If anyone can help, that would be wonderful. I understand that the population number should not be in the person class.

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
#include <iostream>
#include <string>
using namespace std;

class Person
{
friend class Island;

private:
	char status;
	int population;
public:
	Person *next;
	Person();
	Person(char&, int&);
};

Person::Person()
{
}

Person::Person(char& s, int& p)
{
	status = s;
	population = p;
	next = NULL;
}

class Island
{
private:
	Person *root;
public:
	Island();
	~Island();
	void addPerson(char&, int&);
	void traverseList();
};

Island::Island()
{
	root = new Person();
	root = NULL;
}

Island::~Island()
{
	delete root;
}

void Island::addPerson(char& healthstatus, int& population)
{
	Person *new_civilian = new Person(healthstatus, population);

	if(root == NULL)
	{
		root = new_civilian;
		return;
	}
	else
	{
		Person *temp_person = root;
		while(temp_person->next != NULL)
		{
			temp_person = temp_person->next;
		}
		temp_person->next = new_civilian;
	}
}

void Island::traverseList()
{
	Person *temp_person = root;
	while(temp_person != NULL)
	{
		cout << temp_person->status<<endl;
		cout << temp_person->population<<endl;
		temp_person = temp_person->next;
	}
}

int main()
{
	int num;
	Island island_catalog;
	char healthstatus = 'h';
	cout << "How many islands?\n";
	cin >> num;
	for(int i=0; i<num; i++)
	{
		int pop;
		cout << "Island "<< i+1 << " Population: ";
		cin >> pop;
		island_catalog.addPerson(healthstatus, pop);
		pop = 0;
	}
	island_catalog.traverseList();
	return 0;
}
Similar to how you have the cin statement for prompting the user "How many islands", you could ask the user "Do you want to modify this island" when you traverse the list. The user can answer yes, and if the user answers yes, have them type the new population number, and save it into the population variable for that Island.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void modify(Person* person)
{
    cout << "<<Ask for Population and Modify person here>>" << endl;
}

void Island::traverseList()
{
	Person *temp_person = root;
	while(temp_person != NULL)
	{
		cout << temp_person->status<<endl;
		cout << temp_person->population<<endl;
        
 		char modify_response = 'n';
		cout << "Do you want to modify this island? (y/n)";
		cin >> modify_response;
		if (modify_response == 'y')
		{
			modify(temp_person);
		}
        
		temp_person = temp_person->next;
	}
}
Last edited on
Topic archived. No new replies allowed.