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 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;
#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;
}