如何执行链表

问题描述 投票:-1回答:2
#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程序,其中必须以有序的方式(从小到大)插入数据当我运行代码程序时,由于注释而导致错误:预期的“结构节点*”,但参数的类型为“结构节点**”我如何解决此问题,我已经查看了其他解决方案,例如:What does the warning - expected ‘struct node **’ but argument is of type ‘struct node **’ mean?但是那些不能解决我的问题。任何帮助表示赞赏

c linked-list structure doubly-linked-list
2个回答
0
投票

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

作为示例,使用append(&head, 3)代替append(head, 3)


0
投票
    insertAfter(head, new_data);

我该如何解决此问题

您忘记了在功能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);
}
© www.soinside.com 2019 - 2024. All rights reserved.