簡體   English   中英

C++ 在雙向鏈表中插入元素邏輯錯誤

[英]C++ Inserting elements in doubly linked list logic error

我正在研究獲取未排序元素列表並將它們排序為雙向鏈表的代碼。 該代碼在大多數情況下都有效,可以將元素添加到列表的開頭和結尾,但是由於某種原因,如果添加的第一個元素將保留在頭部(按字母順序排列的最高元素)頭部的地址,這超出了我的范圍和 head->next 將是相同的。 如果順序顛倒,尾部也會發生這種情況。 這與第 28 到 50 行的邏輯有關。

下面的代碼是可編譯和可運行的。 任何關於我哪里出錯的幫助將不勝感激。

注意:不要使用 C++ 庫或類,這是您自己的練習。

#include <iostream>
#include <cstring>

using namespace std;

struct node
{
    char value[15];
    node *next;
    node *prev;
};

void insert(node *&head, node *&tail, char *value){

    if(!head){
        // empty list create new node
        node* nn = new node;
        
        strcpy(nn->value, value);
        
        nn->next = NULL;
        nn->prev = NULL;
        
        head = nn;
        tail = nn;
    }
    else if(strcmp(value, head->value) < 0){
        // smaller than head.  Update head
        node* nn = new node;
        
        strcpy(nn->value, value);
        
        nn->next = head;
        nn->prev = NULL;
        nn->next->prev = head;
        
        head = nn;
    }
    else if(strcmp(value, tail->value) > 0){
        // larger than tail.  Update tail
        node* nn = new node;
        
        strcpy(nn->value, value);
        
        nn->next = NULL;
        nn->prev = tail;
        nn->prev->next = tail;
        
        tail = nn;
    }
    else{ 
        /* TODO: insert in the middle of the list */ 
    }
}

void printlinkedList(node *ll){
    node *curr = ll;

    while(curr){
        cout << "Value: " << curr->value << "\t";
        cout << "curr: " << curr << "\t";
        cout << "curr->prev " << curr->prev << "\n";
        curr = curr->prev;
    }
}

int main(){

    // Test code
    node *head = NULL;
    node *tail = NULL;

    insert(head, tail, (char*)"aa");
    insert(head, tail, (char*)"bb");
    insert(head, tail, (char*)"cc");
    insert(head, tail, (char*)"dd");

    cout << "\nhead:\t\t" << head << "\n";
    cout << "head->prev:\t" << head->prev << "\n";
    cout << "head->next:\t" << head->next << "\n\n";

    cout << "tail:\t\t" << tail << "\n";
    cout << "tail->prev:\t" << tail->prev << "\n";
    cout << "tail->next:\t" << tail->next << "\n\n\n";

    cout << "Linked List printed in reverse order: \n";
    printlinkedList(tail);

    return 0;
}

這個:

    nn->next = head;
    nn->prev = NULL;
    nn->next->prev = head;

應該:

    nn->next = head;
    nn->prev = NULL;
    nn->next->prev = nn;

同樣是這樣:

    nn->next = NULL;
    nn->prev = tail;
    nn->prev->next = tail;

應該:

    nn->next = NULL;
    nn->prev = tail;
    nn->prev->next = nn;

您的insert()邏輯有缺陷。

當列表為空時,你很好。

但是當在head前面插入時,您將nn->next正確設置為指向舊head ,但是您將舊headprev設置為指向舊head (即指向自身)而不是指向nn

並且在tail之后插入時,您將nn->prev正確設置為指向舊tail ,但是您將舊tailnext設置為指向舊tail (即指向自身)而不是新nn .

這應該解決它:

void insert(node *&head, node *&tail, char *value) {

    node* nn = new node;
    strncpy(nn->value, value, 15);
    nn->next = NULL;
    nn->prev = NULL;

    if (!head) {
        // empty list create new node
        
        head = tail = nn;
    }
    else if (strcmp(value, head->value) < 0) {
        // smaller than head.  Update head

        nn->next = head;
        head->prev = nn;
        head = nn;
    }
    else if (strcmp(value, tail->value) > 0) {
        // larger than tail.  Update tail
        
        nn->prev = tail;
        tail->next = nn;        
        tail = nn;
    }
    else { 
        /* TODO: insert in the middle of the list */ 
    }
}

暫無
暫無

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

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