尝试插入节点会导致无限链表

时间:2016-11-09 17:19:48

标签: c++ pointers memory linked-list insert

我尝试在列表中找到名称后将节点插入到链表中。 我的问题是,当我打印出链表时,它会打印节点,包括包含该名称的节点,然后无限输出我插入的节点。任何帮助将不胜感激。谢谢!

(一些额外的信息),(指针学生已经指向动态创建的节点)。 :)

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;

}

2 个答案:

答案 0 :(得分:0)

你的功能太复杂了。函数越复杂,它包含的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,该函数还必须更改尾部变量。

例如

        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 &current_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 

答案 1 :(得分:0)

您只需在curr&amp; amp;一个先前

如果你真的想欣赏接受并提出答案

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