我试图插入一个节点到一个链表后,它在列表中找到一个名称。我的问题是,当我打印出链表时,它打印出包含包含名称的节点的节点,但随后无限输出我插入的节点。任何帮助都将非常感激。谢谢!
(一些额外的信息),(指针学生已经指向一个动态创建的节点)。:)
bool StudentLinkedList::insertStudentAfter(StudentNode* student, string _name)
{
StudentNode* curr = headP;
StudentNode* prev = headP;
while (curr != NULL)
{
if (curr->getName() == _name)
{
StudentNode* dummy = curr -> getnext();
curr->setNext(student);
prev = prev->getnext();
curr=curr ->getnext();
curr->setNext(dummy);
prev = curr;
curr = curr->getnext();
length++;
return true;
}
prev = curr;
curr = curr->getnext();
}
return false;
}
只需将新节点插入到当前&上一页
如果你真的想欣赏,请接受并为答案投票
bool StudentLinkedList::insertStudentAfter(StudentNode* student, string _name)
{
StudentNode* curr = headP;
StudentNode* prev = headP;
while (curr != NULL)
{
if (curr->getName() == _name)
{
student->setNext(curr->getnext());
curr->setNext(student);
length++;
return true;
}
prev = curr;
curr = curr->getnext();
}
return false;
}
你的函数太复杂了函数越复杂,包含的bug越多,读起来也越困难:)
函数可以用下面的方式编写。我假设定义了以下函数
getNext, setNext, getPrev, setPrev
^^^^
给你。
bool StudentLinkedList::insertStudentAfter( StudentNode *student,
const std::string &name )
{
StudentNode *current = headP;
while ( current != nullptr && current->getName() != name )
{
current = current->getNext();
}
bool success = current != nullptr;
if ( success )
{
student->setPrev( current );
student->setNext( current->getNext() );
if ( current->getNext() != nullptr )
{
current->getNext()->setPrev( student );
}
current->setNext( student );
}
return success;
}
考虑到如果列表也有一个类似tail的数据成员,那么如果current->getNext
等于nullptr
,函数也必须改变tail变量。
if ( current->getNext() != nullptr )
{
current->getNext()->setPrev( student );
}
else
{
tailP = student;
//^^^^
}
下面是一个示范程序,展示了如何使用您的方法定义类
#include <iostream>
#include <string>
class StudentLinkedList
{
private:
class StudentNode
{
private:
StudentNode *next;
StudentNode *prev;
std::string name;
public:
StudentNode( const std::string &name )
: next( nullptr ), prev( nullptr ), name( name ) {}
StudentNode * getNext() const { return next; }
StudentNode * getPrev() const { return prev; }
const std::string & getName() const { return name; }
void setNext( StudentNode *student ) { next = student; }
void setPrev( StudentNode *student ) { prev = student; }
} *head = nullptr, *tail = nullptr;
public:
StudentLinkedList() = default;
StudentLinkedList( const StudentLinkedList & ) = delete;
StudentLinkedList & operator =( const StudentLinkedList & ) = delete;
~StudentLinkedList()
{
while ( head != nullptr)
{
StudentNode *tmp = head;
head = head->getNext();
delete tmp;
}
tail = head;
}
void appendStudent( const std::string &name )
{
StudentNode *student = new StudentNode( name );
appendStudent( student );
}
void insertStudentAfter( const std::string ¤t_name,
const std::string &new_name )
{
StudentNode *student = new StudentNode( new_name );
insertStudentAfter( student, current_name );
}
friend std::ostream & operator <<( std::ostream &os, const StudentLinkedList &lsdt );
private:
void appendStudent( StudentNode *student )
{
if ( tail == nullptr )
{
head = tail = student;
}
else
{
tail->setNext( student );
tail = tail->getNext();
}
}
bool insertStudentAfter( StudentNode *student, const std::string &name )
{
StudentNode *current = head;
while ( current != nullptr && current->getName() != name )
{
current = current->getNext();
}
bool success = current != nullptr;
if ( success )
{
student->setPrev( current );
student->setNext( current->getNext() );
if ( current->getNext() != nullptr )
{
current->getNext()->setPrev( student );
}
else
{
tail = student;
}
current->setNext( student );
}
return success;
}
};
std::ostream & operator <<( std::ostream &os, const StudentLinkedList &lst )
{
for ( StudentLinkedList::StudentNode *current = lst.head;
current != nullptr;
current = current->getNext() )
{
os << current->getName() << ' ';
}
return os;
}
int main()
{
const size_t N = ( 'Z' - 'A' + 1 ) / 2;
StudentLinkedList students;
for ( size_t i = 0; i < N; i++ )
{
char name[2] = { char( 'A' + 2 * i ) };
students.appendStudent( name );
}
std::cout << students << std::endl;
for ( size_t i = 0; i < N; i++ )
{
char new_name[2] = { char( 'A' + 2 * i + 1 ) };
char current_name[2] = { char( 'A' + 2 * i ) };
students.insertStudentAfter( current_name, new_name );
}
std::cout << students << std::endl;
return 0;
}
程序输出为
A C E G I K M O Q S U W Y
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z