簡體   English   中英

C ++指針,鏈接列表混亂

[英]C++ Pointers, Linked List Confusion

我試圖在C ++中建立一個鏈表。 我的理解是,我創建的代碼應該創建一個節點,然后逐步將4進一步鏈接到末尾。 不幸的是,雖然我希望看到的結果是“ 12 123 1234 12345”,但我看到的是“ 12 12 12 12”,而我主要無法遍歷該列表-它崩潰了。

我有以下代碼:

struct listNode {

    int val;
    listNode* next;

};


int nodeCount = 0;

listNode* addToEnd(listNode* node) {

    listNode* newNode = new listNode;
    newNode->val = ++nodeCount;
    newNode->next = NULL;

    if (node == NULL) {
        return newNode;
    }

    listNode* current = node;
    cout<<"\n\n";
    do {
        if (current->next == NULL) {
            current->next = newNode;
        }
        cout<<current->val<<"\n";
        current = current->next;
    } while (current->next != NULL);
    cout<<current->val<<endl;

}


int main()
{

    listNode* first = addToEnd(NULL);

    addToEnd(first);
    addToEnd(first);
    addToEnd(first);
    addToEnd(first);

    cout<<"Third: "<<first->next->next->val;

}

感謝您的幫助,因為我機智極了!

顯然函數addToEnd是錯誤的

listNode* addToEnd(listNode* node) {

    listNode* newNode = new listNode;
    newNode->val = ++nodeCount;
    newNode->next = NULL;

    if (node == NULL) {
        return newNode;
    }

    listNode* current = node;
    cout<<"\n\n";
    do {
        if (current->next == NULL) {
            current->next = newNode;
        }
        cout<<current->val<<"\n";
        current = current->next;
    } while (current->next != NULL);
    cout<<current->val<<endl;

}

假設列表已經包含兩個節點,並考慮函數內部的do-while循環。 首先current_next != null,因此執行以下語句

        current = current->next;

現在,當前指向第二個節點。 其數據成員next等於NULL。 所以循環的條件

    } while (current->next != NULL);

將為false,並且不會重復任何迭代。 因此,我們沒有添加任何內容。

如果node不等於NULL,該函數也不會返回任何內容。

通過以下方式重寫函數

listNode* addToEnd( listNode* node ) 
{

    listNode* newNode = new listNode { ++nodeCount, NULL };

    if ( node == NULL) return newNode;

    listNode* current = node;

    while ( current->next != NULL ) current = current->next;

    current->next = newNode;

    return newNode;
    // or
    //return node;    
}

考慮到這一說法

cout<<"Third: "<<first->next->next->val;

僅輸出第三個節點的值。 如果要輸出所有列表,則應編寫

for ( listNode *current = first; current; current = current->next ) 
{
    std::cout << current->val << ' ';
}
std::cout << std::endl;

通過使用我的函數,您可以使用以下方式編寫main :)

listNode* first;

addToEnd( addToEnd( addToEnd( addToEnd( first  = addToEnd( NULL ) ) ) ) );

使用for循環將您帶到最后一個節點(而不是一會兒),然后將新節點分配到循環的外面。 嘗試在內部執行此操作將導致無限循環(並使代碼難以閱讀):

listNode* current;
for(current = node; current->next != NULL; current = current->next) ;
current->next = newNode;

您還忘記了在函數末尾返回newNode

您正在使用非void返回類型的函數結束。 您不使用返回值的事實並不可行。

標准中的6.6.3表示:

從函數末尾流出就等於沒有值的return 這導致返回值函數中的行為不確定。

如果以if條件檢查if(node==null)失敗,則沒有return語句。

在您的問題中使用遞歸函數是否違反規則?

為什么不...

void addToEnd(listNode* node){
    if(node == NULL){
        *node = new listNode;
        node->next = NULL;
        node->val = ++nodeCount;
    }else{
        addToEnd(node->next);
    }
    return;
}

int main(){
    listNode* first = NULL;
    addToEnd(first); // 1
    addToEnd(first); // 2
    addToEnd(first); // 3
    addToEnd(first); // 4
    addToEnd(first); // Linked list is now 5 long
}

這就是我要編碼的方式,將五個節點添加到一個包含節點數的鏈表中。 如果有人有建議,歡迎您。

#include <iostream>
#include <cstdlib>

using namespace std;

struct listNode{
 int val;
 listNode* next;
};

listNode* addToEnd(listNode*, int);

int main()
{
  listNode* first = NULL;
  listNode* temp;
  int nodeCount = 1;

  for(int i = 0; i < 5; i++){
     first = addToEnd(first, nodeCount);
     nodeCount++;
  }

  temp = first;

  while(temp){
     cout << temp->val << ' ';
     temp = temp->next;
  }

  temp = first;
                               //Deallocate memory
  while(temp){                 //could do memory deallocation while displaying 
     nodeToDelete = temp;      //the value of nodeCount but wanted to illustrate  
                               //both methods individually
     temp = temp->next;

     delete nodeToDelete;   
  }  

  first = NULL;                //eliminate hanging pointer

  return 0;
}

listNode* addToEnd(listNode* node, int nodeCount) 
{

  listNode* newNode = new (nothrow) listNode;
  listNode* current = node;

  if(newNode){
     newNode->val = nodeCount;
     newNode->next = NULL;
     if (node == NULL)        
        node = newNode;       

     else{          
        while (current->next != NULL) 
           current = current->next;

           current->next = newNode;          
     }
  }
  else
     cout << "error allocationg memory" << endl;

  return node;
}

暫無
暫無

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

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