Я написал программу, чтобы связать список имен и возрастов президентов. Я успешно сделал список и распечатал его, чтобы проверить, все ли работает нормально. Я хотел сейчас пройти список и удалить данные, которые были неполными.C++ Почему эта функция печати не работает после обхода и удаления связанного списка для определенных узлов?
Основная моя забота заключается в том, что после того, как были сделаны удаления, функция печати выдаст ошибку под названием «Ошибка чтения символов строки». Это может означать, что глава списка теперь находится в конце и, следовательно, выходит за пределы списка. Мое другое беспокойство заключается в том, что, возможно, я не удалял узел и правильно привязывал список. Есть лучший способ сделать это? Может ли кто-нибудь прояснить логику псевдокода как для прохождения уже сделанного связанного списка, так и удаления узлов и связать новый список?
// void president cleaning up linked list removes incomplete entries and
void President::RemovingIncompleteEntries()
{
// make a node called delete
Presidentnode * deletenode;
Presidentnode * current = head;
Presidentnode * previous = current;
// while the current has a value
while (current->next != NULL)
{
string CurrentTotal = current->LastName + " " + current->FirstName + " " + current->age;
string CurrentNextTotal = current->next->LastName + " " + current->next->FirstName + " " + current->next->age;
if (CurrentTotal == CurrentNextTotal)
{
cout << "[[" << current->FirstName << " " << current->LastName << "] was deleted for being a duplicate]" << endl;
// sets deletenode to current (will be deleted)
deletenode = current;
// traverses the next node
current = current->next;
// sets the previous nexts pointer at the next current
previous->next = current->next;
// deletes the deletenode;
delete (deletenode);
}
else if ((current->FirstName == current->LastName) || (current->FirstName == current->age))
{
// checks to see if any parts of the name equal another part of the name(helps deal with case like 52)
cout << "[[" << current->FirstName << " " << current->LastName << "] was deleted for having two names equal to each other]" << endl;
// sets deletenode to current (will be deleted)
deletenode = current;
// traverses the next node
current = current->next;
// sets the previous nexts pointer at the next current
previous->next = current->next;
// deletes the deletenode;
delete (deletenode);
}
else if (!(current->age.find_first_not_of("1234567890")))
{
// checks to see if it finds a letter instead of a number (helps deal with Lucas Aubrey)
cout << "[[" << current->FirstName << " " << current->LastName << "] was deleted for having a missing age]" << endl;
// sets deletenode to current (will be deleted)
deletenode = current;
// traverses the next node
current = current->next;
// sets the previous nexts pointer at the next current
previous->next = current->next;
// deletes the deletenode;
delete (deletenode);
}
else{
// traverse the list
current = current->next;
}
previous = current;
}
// show we updated the list
cout << "[Updated List: Incomplete Entries and Duplicates Removed]" << endl;
}
// void printlinkedlist printes presidents in order
void President::PrintLinkedList()
{
Presidentnode *current = head;
// while the head has a value
while (current != NULL){
// prints data
cout << current->FirstName << " " << current->LastName << " " << current->age << endl;
current = current->next;
}
// show we deleted the list
cout << "[List Printed]" << endl;
}
Пожалуйста, отредактируйте ваше сообщение, чтобы включить [mcve]. После того, как эта ссылка на pastebin гниет ваш вопрос, и ответы будут бесполезны. – user4581301
Я сделал изменения. – Nathan
Чтение их. Слишком много кода и много ошибок. Трудно сузить до того, что в настоящее время вызывает у вас горе. – user4581301