Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 链表,带参数的操作_C_Singly Linked List - Fatal编程技术网

C 链表,带参数的操作

C 链表,带参数的操作,c,singly-linked-list,C,Singly Linked List,我正试图在中实现这个程序,我可以动态创建任意数量的单链表,并对特定的单链表(由参数定义)执行操作。我创建头指针的动态数组,以便引用由paramater(数组的索引+1)定义的特定头节点。参数仅为(1,2,3..列表数)。到目前为止,我只实现了初始化和推送功能,但编译后的程序并没有按预期工作。问题在哪里 #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define CHUNK 10 t

我正试图在中实现这个程序,我可以动态创建任意数量的单链表,并对特定的单链表(由参数定义)执行操作。我创建头指针的动态数组,以便引用由paramater(数组的索引+1)定义的特定头节点。参数仅为(1,2,3..列表数)。到目前为止,我只实现了初始化和推送功能,但编译后的程序并没有按预期工作。问题在哪里

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

#define CHUNK 10

typedef struct
{
    char *str;
    struct node *next;
} node;


node *initialise(node **array, int *amount_of_lists);
void push(node **array, int *amount_of_lists);
char *getString(void);

int main()
{
    node **heads = NULL; //initially null, pointer to the dynamic array of head pointers
    int amount_of_lists = 0;
    int *no_of_heads = &amount_of_lists;

    initialise(heads, no_of_heads);
    initialise(heads, no_of_heads);
    push(heads, no_of_heads);
    push(heads, no_of_heads);

    return 0;
}

node *initialise( node **array, int *amount_of_lists )  /*reallocate memory for another head pointer ans return the pointer to node*/
{
    ++(*amount_of_lists);
    printf("\n%d", *amount_of_lists);
    array = (node**)realloc(array, sizeof(node*)*(*amount_of_lists));
    return array[(*amount_of_lists) - 1] = malloc(sizeof(node));
}

int readParameter(int *amount_of_lists)
{
    int parameter = 0, control = 0;
    bool repeat = 0;
    do
    {
        if(repeat)
        {
            printf("\nWrong parameter, try again.");
        }
        printf("\n Enter list parameter: ");
        control = scanf("%d", &parameter);
        fflush(stdin);
        repeat = 1;
    }
    while( control != 1 || parameter < 1 || parameter > (*amount_of_lists) );

    return parameter;
}

void push(node **array, int *amount_of_lists)
{
    int parameter = readParameter(amount_of_lists) - 1;
    node *temp = array[parameter];
    array[parameter] = malloc(sizeof(node));
    array[parameter] -> next = temp;
    array[parameter] -> str = getString();
}

char *getString(void)
{
    char *line = NULL, *tmp = NULL;
    size_t size = 0, index = 0;
    int ch = EOF;

    while (ch)
    {
        ch = getc(stdin);

        /* Check if we need to stop. */
        if (ch == EOF || ch == '\n')
            ch = 0;

        /* Check if we need to expand. */
        if (size <= index)
        {
            size += CHUNK;
            tmp = realloc(line, size);
            if (!tmp)
            {

                free(line);
                line = NULL;
                break;
            }
            line = tmp;
        }

        /* Actually store the thing. */
        line[index++] = ch;
    }

    return line;
}
#包括
#包括
#包括
#定义块10
类型定义结构
{
char*str;
结构节点*下一步;
}节点;
节点*初始化(节点**数组,整数*列表的金额);
作废推送(节点**数组,整数*列表金额);
char*getString(void);
int main()
{
节点**heads=NULL;//最初为NULL,指向head指针动态数组的指针
列表的整数金额=0;
int*头的数量=&列表的数量;
初始化(头,无头);
初始化(头,无头);
推动(头,无头);
推动(头,无头);
返回0;
}
节点*初始化(节点**数组,int*列表的数量)/*为另一个头指针重新分配内存,并将指针返回到节点*/
{
++(*清单金额);
printf(“\n%d”,*清单金额);
数组=(节点**)realloc(数组,sizeof(节点*)*(*列表的数量));
返回数组[(*列表的数量)-1]=malloc(sizeof(node));
}
int readParameter(int*列表的数量)
{
int参数=0,控制=0;
布尔重复=0;
做
{
如果(重复)
{
printf(“\n错误参数,请重试。”);
}
printf(“\n输入列表参数:”);
control=scanf(“%d”和参数);
fflush(stdin);
重复=1;
}
而(控制!=1 | |参数<1 | |参数>(*数量|列表));
返回参数;
}
无效推送(节点**数组,整数*列表的金额)
{
int参数=readParameter(列表的数量)-1;
节点*temp=数组[参数];
数组[参数]=malloc(sizeof(node));
数组[参数]->next=temp;
数组[参数]->str=getString();
}
char*getString(void)
{
char*line=NULL,*tmp=NULL;
大小\u t大小=0,索引=0;
int ch=EOF;
while(ch)
{
ch=getc(标准偏差);
/*看看我们是否需要停下来*/
如果(ch==EOF | | ch=='\n')
ch=0;
/*检查是否需要扩展*/

如果(尺寸正如BLUEPIXY在他的评论1中隐晦地暗示的那样),为了修改
initialise()
main()
heads
,您必须通过引用
initialise()
传递
heads
,即更改

    initialise(heads, no_of_heads);
    initialise(heads, no_of_heads);

因此

node *initialise( node **array, int *amount_of_lists )
更改为

node *initialise(node ***array, int *amount_of_lists)
并且在
数组
中更改为
*数组
,即

    *array = realloc(*array, sizeof(node *) * *amount_of_lists);
    return (*array)[*amount_of_lists - 1] = malloc(sizeof(node));

它是如何工作的?在它接受了正确的参数后,程序就会崩溃。push函数中一定有问题,因为getString是我在某处找到的函数,它在我的其他程序中工作正常。0)
typedef struct
-->
typedef struct node1)
初始化
return array;`..
heads=初始化(头,没有头);
初始化(node**array
-->
初始化(node***array
)。还有
数组[(*amount\u of_list)-1]->next=NULL;
对不起,我什么都不懂,除了:array[(*amount\u of_list)-1]>next=NULL;你能写完整的句子并解释一下吗?谢谢。
    *array = realloc(*array, sizeof(node *) * *amount_of_lists);
    return (*array)[*amount_of_lists - 1] = malloc(sizeof(node));