簡體   English   中英

帶c的雙鏈表

[英]double linked list with c

好的,現在我必須在 c 上制作雙鏈表。 有 7 個函數作用於 main。

附加。 插入。 刪除。 打印。 打印_反轉。 新節點。 新的DLL。

我只能修改 5 個函數 append、insertAt、deleteAt、print、print_reverse

最后,我可以進行 append、print、print_reverse 但是,由於索引的原因,我無法進行 insertAt、deleteAt。

1.我不明白為什么代碼

else {
    while (index-- >= 0) {
      temp = temp->next;
    }

讓記憶碰撞。 為了使用索引,我需要移動節點來收集位置並連接到新節點。 但它不起作用......

2.還有什么回報; 的作用? 我還沒有見過這種類型的回報。

3.如何使用索引進行deleteAt? 我認為 deleteAt 和 insertAt 有類似的算法。 所以我嘗試首先插入並刪除最后。 但我寫的東西不好用..

我可以在互聯網上找到很多雙鏈表的數據。 但我找不到使用索引....我只聽了兩個月的 c 語言講座,所以對 spagettii 代碼感到抱歉...

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

typedef struct Node {
    int val;
    struct Node *prev;
    struct Node *next;
} Node;

typedef struct {
    Node *head;
    int size;
} DLL;

Node *newnode(int n)
{
    Node *temp = (Node *)malloc(sizeof(Node));
    temp->val = n;
    temp->prev = NULL;
    temp->next = NULL;
    return temp;
}

DLL *newDLL() {
    DLL *temp = (DLL *)malloc(sizeof(DLL));
    temp->head = NULL;
    temp->size = 0;
    return temp;
}

void append(DLL *list, Node *newnode) {
    struct Node* temp = list->head;
    struct Node* newNode = newnode;
    if (list->head == NULL) {
        list->head = newNode;
        list->size++;
        return;
    }
    while (temp->next != NULL) {
        temp = temp->next;
    }
    list->size++;
    temp->next = newNode;
    newNode->prev = temp;
}

void insertAt(DLL *list, int index, Node *newnode) {

    struct Node* temp = (Node *)malloc(sizeof(Node));

    if (index < 0 || index >= list->size + 1) {
        printf("out of range\n");
    }
    else if (index == 0) {
        newnode->next = list->head;
        list->head->prev = newnode;
        list->head = newnode;
    }
    else {
        while (index-- >= 0) {
          temp = temp->next;
        }

    temp->val = newnode->val;
    temp->next = list->head->next;
    list->head->next = temp;
    temp->prev = list->head;

    if (temp->next != NULL)
        temp->next->prev = temp;
    }
}

void deleteAt(DLL *list, int index) {
    //save reference to first link

    struct Node* temp = (Node *)malloc(sizeof(Node));

    //if only one link
    if (list->head->next == NULL) {
        list->head->prev = NULL;
    }
    else {
        list->head->next->prev = NULL;
    }

    list->head = list->head->next;
    //return the deleted link
    return;
}

void print(DLL *list) {
    struct Node* temp = list->head;
    printf("Forward: ");
    while (temp != NULL) {
        printf("[%d] ", temp->val);
        temp = temp->next;
    }
    printf("\n");
}

void print_reverse(DLL *list) {
    struct Node* temp = list->head;
    if (temp == NULL) return; // empty list, exit
    // Going to last Node
    while (temp->next != NULL) {
        temp = temp->next;
    }
    // Traversing backward using prev pointer
    printf("Reverse: ");
    while (temp != NULL) {
        printf("[%d] ", temp->val);
        temp = temp->prev;
    }
    printf("\n");
}

int main() {
    DLL *list = newDLL();
    int i;
    for (i = 1; i < 6; i++) {
        append(list, newnode(i));
    }

    print(list);
    deleteAt(list, -1);
    deleteAt(list, 5);
    deleteAt(list, 0);
    print(list);
    deleteAt(list, 2);
    print(list);
    deleteAt(list, 2);
    print(list);
    insertAt(list, -1, newnode(6));
    insertAt(list, 3, newnode(6));
    insertAt(list, 0, newnode(7));
    print(list);
    insertAt(list, 1, newnode(8));
    print(list);
    insertAt(list, 4, newnode(9));
    print(list);
    print_reverse(list);

    return 0;
}

在索引處插入的部分有問題:

temp = malloc是錯誤的,它應該以temp = head

在插入中:

temp->val = newnode->val;
temp->next = list->head->next;
list->head->next = temp;
temp->prev = list->head;

temp->next不應該是head->next ,它應該是newnode newnode->next應該是newnode->next temp->next等。

暫無
暫無

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

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