如何实现链表

how to implement linked list

#include <stdio.h>    
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
struct Node* prev;
};

void push(struct Node** head_ref, int new_data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
new_node->prev = NULL;
if ((*head_ref) != NULL)(*head_ref)->prev = new_node;
(*head_ref) = new_node;}

void append(struct Node** head_ref, int new_data){
    /* 1. allocate node */
    struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
    struct Node* last = *head_ref; /* used in step 5*/
    /* 2. put in the data */
    new_node->data = new_data;
    /* 3. This new node is going to be the last node, so
        make next of it as NULL*/
    new_node->next = NULL;
    /* 4. If the Linked List is empty, then make the new
        node as head */
    if (*head_ref == NULL) {
        new_node->prev = NULL;
        *head_ref = new_node;
        return;}
    /* 5. Else traverse till the last node */
    while (last->next != NULL)
        last = last->next;
    /* 6. Change the next of last node */
    last->next = new_node;
    /* 7. Make last node as previous of new node */
    new_node->prev = last;
    return;}

void insertAfter(struct Node* prev_node, int new_data){
/*1. check if the given prev_node is NULL */
if (prev_node == NULL) {
    printf("the given previous node cannot be NULL");
    return;}
/* 2. allocate new node */
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
/* 3. put in the data */
new_node->data = new_data;
/* 4. Make next of new node as next of prev_node */
new_node->next = prev_node->next;
/* 5. Make the next of prev_node as new_node */
prev_node->next = new_node;
/* 6. Make prev_node as previous of new_node */
new_node->prev = prev_node;
/* 7. Change previous of new_node's next node */
if (new_node->next != NULL)
    new_node->next->prev = new_node;}

void printList(struct Node* node){
    struct Node* last;
    printf("\nTraversal in forward direction \n");
    while (node != NULL) {
        printf(" %d ", node->data);
        last = node;
        node = node->next;}

    printf("\nTraversal in reverse direction \n");
    while (last != NULL) {
        printf(" %d ", last->data);
        last = last->prev;
    }}


void sortedInsert(struct Node** head, int new_data) {

    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
    new_node->data = new_data;
    new_node->next = NULL;
    struct Node* temp;

    if ((*head) == NULL || (new_node->data) > (*head)->prev->data) {
        append(head, new_data);
        return;
    }

    if ((new_node->data) < ((*head)->data)) {
        push(head, new_data);
        return;
    }

    temp = (*head)->next;
    while ((temp->data) < (new_node->data)) {
        temp = temp->next;
    }

    insertAfter(head, new_data);
}
int main() {
struct Node* head = NULL;
sortedInsert(&head, 0);
sortedInsert(&head, 9);
sortedInsert(&head, 4);
sortedInsert(&head, 3);
sortedInsert(&head, 34);
sortedInsert(&head, 15);
printf("\n Created Linked list is: ");
printList(head);
return 0;}

我正在尝试编写一个 C 程序,其中数据必须按顺序插入(从小到大) 当我 运行 代码程序由于注释而出错: 预期 'struct Node *' 但参数类型为 'struct Node **' 我该如何解决这个问题,我已经查阅了其他解决方案,例如: 但那些无法解决我的问题。 感谢任何帮助

当您使用您声明的函数时,您应该将引用(地址)传递给头部而不是头部本身,因为这是您的代码所需要的。

例如,使用 append(&head, 3) 而不是 append(head, 3)

    insertAfter(head, new_data);

how can I fix this problem

您忘记了取消引用 struct Node** head,就像您在函数 sortedInsert 的其他地方所做的那样;要获得正确的参数类型,它将是 insertAfter(*head, new_data).

但是插入逻辑还是不太对;这是更正后的版本:

void sortedInsert(struct Node** head, int new_data) {

    // new node is allocated in append(), push() or insertAfter()
    struct Node* temp;

    if ((*head) == NULL || new_data < (*head)->data) {
        push(head, new_data);
        return;
    }

    temp = *head;
    while (temp->next && temp->next->data < new_data) {
        temp = temp->next;
    }

    insertAfter(temp, new_data);
}