簡體   English   中英

最后在C ++的鏈接列表中添加節點

[英]adding node at last in Linked List in C++

我試圖實現單鏈接列表。 我的addAtLast()函數未正確執行。 執行此功能時程序崩潰。 請提出一些變化。

class LList
{
public:
    int noOfNodes;
    Node const *start;/*Header Node*/

    LList()
    {
        start=new Node;
        noOfNodes=0;start=0;
    }

    void addAtFront(Node* n)
    {
        /*
        cout<<endl<<"class"<<n;
        cout<<"start"<<start;
        cout<<"data in node";n->print();
        */
        n->next=const_cast<Node*>(start);
        start=n;
        // cout<<" next=";start->print();
        noOfNodes++;
    }

    void addAtLast(Node* n)
    {
        Node *cur=const_cast<Node*>(start);
        if (start==NULL)
        { 
            start=n;
            return;
        }
        while(cur->next!=NULL)
        {
            cur=cur->next;
        }
        cur->next=n;
        noOfNodes++;
    }

    int getPosition(Node data)
    {
        int pos=0;
        Node *cur=const_cast<Node*>(start);
        while(cur!=NULL)
        {
            pos++;
            if(*cur==data)
            {
                return pos;
            }
            cur=cur->next;
        }
        return -1;//not found
    }

    Node getNode(int pos)
    {
        if(pos<1)
            return -1;// not a valid position
        else if(pos>noOfNodes)
            return -1; // not a valid position

        Node *cur=const_cast<Node*>(start);
        int curPos=0;
        while(cur!=NULL)
        {
            if(++curPos==pos)
                return *cur;
            cur=cur->next;
        }
    }

    void traverse()
    {
        Node *cur=const_cast<Node*>(start);
        while(cur!=NULL)
        {
            //   cout<<"start"<<start;        
            cur->print();
            cur=cur->next;
        }
    }  

    ~LList()
    {
        delete start;
    }
};
void addAtLast(Node* n) {
    Node *cur=const_cast<Node*>(start);
    if(start==NULL) {
        start=n;
        n->next = NULL;
        noOfNodes++;
        return;
    }
    while(cur->next!=NULL) {
        cur=cur->next;
    }
    cur->next=n;
    n->next = NULL;  // Added
    noOfNodes++;
}

從一開始就..

start=new Node;
noOfNodes=0;start=0;

應該這樣嗎?

start=new Node;
noOfNodes=0;start->next=NULL;

在2行中,您創建了內存泄漏。 我不知道您為什么要設置start=0 不要那樣做,您已經為其分配了內存!

至於崩潰,可以通過@liulinhuai的答案解決。 您將取消引用未初始化的指針,嘗試獲取它的next成員。

在您的ctor中,將start設置為0。在崩潰函數中,您首先檢查它是否為NULL

if (start==NULL)
{ 
   start=n;
   //n->next is now points to nowhere
   return;
}

下次調用addAtLast進行迭代,直到找到NULL但是上一個addAtLast沒有將next指針設置為NULL因此第二次迭代將導致access violation

while(cur->next!=NULL) {
   //first time it's ok but second call on cur will crash
   cur=cur->next;
}

解決方案是-所有新Node必須將next指針設置為NULL

我提到了此評論,但將在此處作為答案解決。 此函數的調用者必須確保兩件事:

  1. 必須將傳入的節點列表(即使只有一個元素長, 也是一個列表)也必須通過將end-next-pointer設置為NULL來正確終止。 調用者必須確保這一點,因為此代碼無法假定它,並且盲目將node->next = NULL;設置node->next = NULL;

  2. 絕對確保調用者知道,一旦這個執行, 這個名單現在擁有傳入的節點和任何列表它可能開始與主叫方,因此,決不能釋放它,或任何其指向,對發送方。

除了節點數管理問題之外, addAtLast()函數沒有任何問題,盡管我會以不同的方式實現它:

void addAtLast(Node* n)
{
    // no nulls allowed
    if (n == NULL)
        return;

    // set start if first in list
    if (start == NULL)
    {
        start = n;
        noOfNodes = 1;
    }

    // else walk list to find end
    else
    {
        Node *cur = const_cast<Node*>(start);
        while(cur->next != NULL)
            cur = cur->next;
        cur->next = n;
        ++noOfNodes;
    }

    // adjust count to contain any nodes from 'n'
    while (n->next != NULL)
    {
        ++noOfnodes;
        n = n->next;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM