HELP WITH LINKED LIST CODE

I'm trying to make this code work.The program compiles but I get this error:"Bus error: 10" after I select option 1. It's a linked list containing type Person structs.


#include <iostream>


int count=0;

struct Person
{
std::string name;
int age;
int id;
Person *next;
};

void PrintPerson(Person *person)
{
std::cout << person->name << " is " << person->age << " years old.\n";
}

struct List
{
//First person in the list, null if list is empty
Person *head;
//Current person in the list, where a null pointer inicate a past-the-end position
Person *current;
//Previous element, null if current is first
Person *previous;
};


void ListInitialize(List *list)
{
list->head = nullptr;
list->current = nullptr;
list->previous = nullptr;
}


void ListNext(List *list)
{
//Moves the current position in the list one element forward
//or to null if it exceeds the list
if (list->current != nullptr)
{
list->previous = list->current;
list->current = list->current->next;
}
}

void ListHead(List *list)
{
//Move the current position to the first element on the list
list->previous = nullptr;
list->current = list->head;
}

Person *ListGet(List *list)
{
return list->current;
}


//Set the current position to the person with the given name. If no person
////exists with that name, then current position is set to past-the-end.

void ListFind(List *list, int id1)
{
ListHead(list);
while (list->current != nullptr && list->current->id != id1)
ListNext(list);
}

void ListInsert(List *list, Person *person)
{
person->next = list->current;

if(list->current == list->head)
list->head=person;
else
list->previous->next = person;
list->current = person;
}

void ListRemove(List *list)
{
if(!list->current)
return;
if(list->current == list->head)
list->head = list->current->next;
else
list->previous->next = list->current->next;
Person *next = list->current->next;
delete list->current;

list->current = list->current->next;
}

void AddPerson(List *list)
{
Person *person1;
person1->id=count+1;
std::cout<< "Type the person's name: ";
std::cin>> person1->name;
std::cout<< "Enter the person's age: ";
std::cin>> person1->age;
count++;
ListInsert(list,person1);
std::cout<<"Person id"<<person1->id;
PrintPerson(ListGet(list));
}

void FindPerson(List *list)
{
std::cout<< "Enter the person's id: ";
int id1;
std::cin>>id1;
if(id1>count)
{
std::cout<< "This id is invalid...\n";
}
else
{
ListFind(list, id1);
ListGet(list);
PrintPerson(ListGet(list));


}
}

void RemovePerson(List *list)
{
std::cout<< "Enter the person's id: ";
int id1;
std::cin>>id1;
if(id1>count)
{
std::cout<< "This id is invalid...\n";
}
else
{
ListFind(list, id1);
ListRemove(list);
count --;
}
}

void PrintList(List *list)
{


int i = 0;
ListHead(list);
while(i < count){
PrintPerson(ListGet(list));
ListNext(list);
i++;
}
}

void PrintMenu ()
{
std::cout <<"Main menu \n1. Add a person\n2. Find a person\n3. Remove a person\n4. Print the list\n5. Exit\n";
}



int main()
{
List list;

ListInitialize(&list);
int x;

do
{
PrintMenu();


std::cout<<"Select an option:\n";
std::cin>>x;
switch (x)
{
case 1:
std::cout <<"You selected: Add a person\n";
AddPerson(&list);
PrintPerson(ListGet(&list));
break;

case 2:
std::cout<<"You selected: Find a person\n";
FindPerson(&list);
break;

case 3:
std::cout<<"You selected: Remove a person\n";
RemovePerson(&list);
break;

case 4:
std::cout<<"You selected: Print the list\n";
PrintList(&list);
break;

case 5:
std::cout<<"Exit";
break;

default:
std::cout<<"Option not available\n";
PrintMenu();
break;
}


}while(x!=5);
return 0;
}

I haven't a taken a detail look at the code itself, but it seems in your add person function, your pointer is uninitialized.
You left out #include <string>. Also, I made Person object global. Hope that helps.

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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <iostream>
#include <string>

int count=0;

struct Person
{
std::string name;
int age;
int id;
Person *next;
};

void PrintPerson(Person *person)
{
std::cout << person->name << " is " << person->age << " years old.\n";
}

Person person2;

struct List
{
//First person in the list, null if list is empty
Person *head;
//Current person in the list, where a null pointer inicate a past-the-end position
Person *current;
//Previous element, null if current is first
Person *previous;
};


void ListInitialize(List *list)
{
list->head = nullptr;
list->current = nullptr;
list->previous = nullptr;
}


void ListNext(List *list)
{
//Moves the current position in the list one element forward
//or to null if it exceeds the list
if (list->current != nullptr)
{
list->previous = list->current;
list->current = list->current->next;
}
}

void ListHead(List *list)
{
//Move the current position to the first element on the list
list->previous = nullptr;
list->current = list->head;
}

Person *ListGet(List *list)
{
return list->current;
}


//Set the current position to the person with the given name. If no person
////exists with that name, then current position is set to past-the-end.

void ListFind(List *list, int id1)
{
ListHead(list);
while (list->current != nullptr && list->current->id != id1)
ListNext(list);
}

void ListInsert(List *list, Person *person)
{
person->next = list->current;

if(list->current == list->head)
list->head=person;
else
list->previous->next = person;
list->current = person;
}

void ListRemove(List *list)
{
if(!list->current)
return;
if(list->current == list->head)
list->head = list->current->next;
else
list->previous->next = list->current->next;
Person *next = list->current->next;
delete list->current;

list->current = list->current->next;
}

void AddPerson(List *list)
{
//Person *person1;
person2.id=count+1;
std::cout<< "Type the person's name: ";
std::cin >> person2.name;
std::cout<< "Enter the person's age: ";
std::cin >> person2.age;
count++;
ListInsert(list,&person2);
std::cout<<"Person id"<<person2.id;
PrintPerson(ListGet(list));
}

void FindPerson(List *list)
{
std::cout<< "Enter the person's id: ";
int id1;
std::cin>>id1;
if(id1>count)
{
std::cout<< "This id is invalid...\n";
}
else
{
ListFind(list, id1);
ListGet(list);
PrintPerson(ListGet(list));


}
}

void RemovePerson(List *list)
{
std::cout<< "Enter the person's id: ";
int id1;
std::cin>>id1;
if(id1>count)
{
std::cout<< "This id is invalid...\n";
}
else
{
ListFind(list, id1);
ListRemove(list);
count --;
}
}

void PrintList(List *list)
{


int i = 0;
ListHead(list);
while(i < count){
PrintPerson(ListGet(list));
ListNext(list);
i++;
}
}

void PrintMenu ()
{
std::cout <<"Main menu \n1. Add a person\n2. Find a person\n3. Remove a person\n4. Print the list\n5. Exit\n";
}



int main()
{
List list;

ListInitialize(&list);
int x;

do
{
PrintMenu();


std::cout<<"Select an option:\n";
std::cin>>x;
switch (x)
{
case 1:
std::cout <<"You selected: Add a person\n";
AddPerson(&list);
PrintPerson(ListGet(&list));
break;

case 2:
std::cout<<"You selected: Find a person\n";
FindPerson(&list);
break;

case 3:
std::cout<<"You selected: Remove a person\n";
RemovePerson(&list);
break;

case 4:
std::cout<<"You selected: Print the list\n";
PrintList(&list);
break;

case 5:
std::cout<<"Exit";
break;

default:
std::cout<<"Option not available\n";
PrintMenu();
break;
}


}while(x!=5);
return 0;
}
Topic archived. No new replies allowed.