【问题标题】:how to implement linked list如何实现链表
【发布时间】:2020-06-16 09:56:41
【问题描述】:
#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 **” 我该如何解决这个问题,我已经查看了其他解决方案,例如:What does the warning - expected ‘struct node **’ but argument is of type ‘struct node **’ mean? 但那些无法解决我的问题。 任何帮助表示赞赏

【问题讨论】:

  • 在调用 insertAfter(head, new_data); 中,headstruct Node **,但根据 insertAfter 的声明和实现,第一个参数的预期类型是 struct Node *。该警告告诉您您没有传递正确类型的值,并且该警告在该评估中完全准确。

标签: c linked-list structure doubly-linked-list


【解决方案1】:
    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);
}

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 据我了解,对于将 *head 作为输入的函数,您应该使用 head(例如在主函数 printList(head) 中),对于将 **head 作为输入的函数,您应该使用 *head 如您之前提到的。但是当您在另一个函数中使用函数时它变得复杂,我尝试了您的解决方案,当我在 sortedInsert 函数中进行更改时,它会创建一个新错误:参数类型为 'struct Node * **' 在任何更改之前,只有一个错误是由于 insertAfter 而创建的,所以我认为 append 和 push 应该保持原样
    猜你喜欢
    • 1970-01-01
    • 2012-05-10
    • 1970-01-01
    • 1970-01-01
    • 2013-11-26
    • 2010-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多