【问题标题】:Passing array of linked lists into functions将链表数组传递给函数
【发布时间】:2014-10-29 09:29:29
【问题描述】:

我正在研究一个使用链表数组的等待列表。我以前使用过链表,但不知道如何将数组传递给函数。我从声明头指针和尾指针数组开始。我收到此错误:

warning: incompatible pointer types passing 'struct node *(*)[4]'
      to parameter of type 'struct node ***' [-Wincompatible-pointer-types]
                                add(&head,&tail);
                                          ^~~~~
lab6.c:10:31: note: passing argument to parameter 'tail' here
void add(NODE ***head,NODE ***tail);

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NODE struct node
struct node {
    char name[20];
    int number;
    NODE *next;
};
void add(NODE ***head,NODE ***tail);
void delete(NODE ***head,NODE ***tail);
void show(NODE ***head);

int main() {
    //Initialize
    NODE *head[4]={NULL,NULL,NULL,NULL};
    NODE *tail[4]={NULL,NULL,NULL,NULL};
    int whileLoop=0;
        while(whileLoop==0) {
                int selection;
                printf("Choose an Option: 1)Add new party 2)Remove table 3)Show list 4)Quit: ");
                scanf("%d",&selection);

                switch(selection) {
                        case 1:
                                add(&head,&tail);
                                break;
                        case 2:
                                delete(&head,&tail);
                                break;
                        case 3:
                                show(&head);
                                break;
                        case 4:
                whileLoop=1;
                                break;
                        default:
                                add(&head,&tail);
                }
        }
        return 0;
}
void add(NODE ***head,NODE ***tail) {
    char tempName[20];
    printf("enter a name for the party: ");
    scanf("%s",tempName);
    printf("enter a party size: ");
    int tempSize;
    scanf("%d",&tempSize);
    if (tempSize>0) {
        NODE *ptr=(NODE *)malloc(sizeof(NODE));
        strcpy(ptr->name,tempName);
        ptr->number=tempSize;
        ptr->next=NULL;
        int i;
        if (tempSize>=1 && tempSize<=2) {
            i=0;
        } else if (tempSize>=3 && tempSize<=4) {
            i=1;
        } else if (tempSize>=5 && tempSize<=6) {
            i=2;
        } else {
            i=3;
        }
        if (NULL==*head[i]) {
            *head[i]=*tail[i]=ptr;
        } else {
            (*tail[i])->next=ptr;
            (*tail[i])=ptr;
        }
    } else {
        printf("Valid size not entered");
    }



}
void delete(NODE ***head,NODE ***tail) {


}
void show(NODE ***head) {


}   

【问题讨论】:

  • 你可以在结构上使用#define,而不是使用typedef。提高可读性。
  • 由于您实际上没有在add 函数中分配给headtail,因此您不需要通过“通过引用”。我建议你放弃间接。
  • 至于您的问题,指向指针数组的指针与指向指针的指针相同。按照我上面的建议,您实际上也可以解决编译器错误。
  • 谢谢!你能举个例子吗?它仍然无法编译。

标签: c arrays linked-list


【解决方案1】:

您有指向节点的指针数组,当作为参数传递时,它将衰减为指向节点的指针的指针。您对数组条目(列表的头部和尾部)所做的更改将是永久性的。

所以你的函数的签名是:

void add(NODE **head, NODE **tail);

对于这个客户端代码:

NODE *head[4] = {NULL, NULL, NULL, NULL};
NODE *tail[4] = {NULL, NULL, NULL, NULL};

add(head, tail);

add 中,您将列表的负责人称为head[i]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-28
    • 2021-03-17
    • 2011-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多