簡體   English   中英

在C中實現雙向鏈表

[英]Implement a doubly linked list in C

每當調用insertAtBeginning方法時,我都想在鏈表的開頭插入一個節點。 我的代碼構建良好,但是我沒有得到想要的輸出。

我得到以下輸出:

0------>NULL

所需的輸出是:

9------>8------>7------>6------>5------>4------>3------>2------>1------>0------>NULL

以下是我的代碼:

#include<stdio.h>
#include<stdlib.h>

struct dll{
    int data;
    struct dll* previous;
    struct dll* next;
};


struct dll* insertAtBeginning(int a, struct dll* head){

    if(head == NULL){
        head->data = a;
        head->previous = NULL;
        head->next = NULL;
        return head;
    }
    else{
        struct dll *first;
        first = (struct dll*) malloc( sizeof(struct dll));
        first->data = a;
        first->next = head;
        head->previous = first;
        first->previous = NULL;
        head = first;
        free(first);
        return head;
    }
}


void display_from_first(struct dll* head){
    struct dll *temp;
    temp = head;

    printf("\nThe linked list contains: ");
    while(temp != NULL) {
        printf("%d------>",temp->data);
        temp = temp->next;
    }
    printf("NULL\n");
    free(temp);
    }


int main(){
    int i = 0;
    struct dll *head1, *tail1;
    head1 = (struct dll*) malloc( sizeof(struct dll));
    head1->next = NULL;
    head1->previous = NULL;

    for(i=0; i<10; i++){
        insertAtBeginning(i, head1);
    }

    display_from_first(head1);

    return 0;
}

如果從兩個節點的空列表開始,雙向鏈接列表的代碼將更加簡潔,如下所示。

在此處輸入圖片說明

這樣,您就不必處理諸如if(head==NULL)類的特殊情況。 在要插入(或刪除)的節點之前和之后始終都有一個節點,因此您只需將事情掛起來就可以完成。

#include <stdio.h>
#include <stdlib.h>

typedef struct s_node Node;
struct s_node
{
    Node *prev;
    Node *next;
    int  data;
};

Node *insertAtBeginning( Node *head, int value )
{
    // allocate memory for the new node
    Node *node = malloc( sizeof(Node) );
    if ( node == NULL )
        return NULL;

    // insert the node at the beginning of the list
    Node *temp = head->next;
    head->next = node;
    temp->prev = node;

    // fill in the fields of the node
    node->prev = head;
    node->next = temp;
    node->data = value;

    return node;
}

void showList( Node *head )
{
    Node *node;

    printf( "The list contains: " );
    for ( node = head->next; node->next != NULL; node = node->next )
        printf( "%d--->", node->data );
    printf( "NULL\n" );
}

int main( void )
{
    // create an empty list with two nodes
    Node head = { NULL , NULL, 0 };
    Node tail = { &head, NULL, 0 };
    head.next = &tail;

    // insert more nodes
    for ( int i = 0; i < 10; i++ )
        insertAtBeginning( &head, i );

    // display the list
    showList( &head );
}

你不能free(first); insertAtBeginning()

代碼在這里。

順便說一句,當您有空列表時, display_from_first()打印The linked list contains: 0------>NULL因為

head1 = (struct dll*) malloc( sizeof(struct dll));
head1->next = NULL;
head1->previous = NULL;

main() 從主電源上將其刪除以具有正確的輸出

您在這里有幾個錯誤。

1)您的函數insertAtBeginning返回指向已更改列表的指針,但是您不更新主函數中指向列表開頭的指針。

2)您正在釋放剛剛分配的指向插入函數中新節點的指針。 您以為您正在釋放指針,但實際上您說的是內存中不再需要該位置,因此您的節點不能在那里。

這里主要有兩個問題:

  1. free(first) :這不是必需的,因為您希望保存剛分配的內存,而不是刪除它。

  2. 您的insertAtBeginning()函數返回一個指向head的指針,因此在main() ,您在其中調用此函數將其更改為head1=insertAtBeginning(i, head1); 這樣,您的頭也被保存。

這是帶有兩個編輯的代碼:

http://ideone.com/nXwc8z

暫無
暫無

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

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