链表,带参数的操作 [英] Linked lists, operations with parameter

查看:116
本文介绍了链表,带参数的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实施程序符合我可以动态地创建〜单链表的任意数量和特定的一个(由参数定义)进行操作。创建头指针的动态数组,这样我可以指由paramater(数组+ 1的指数)中定义的特定头节点。参数只是(名单1,2,3..number)。到目前为止,我已经成功地实现了只初始化和推动作用,但如预期complilation后程序无法正常工作。哪里有问题?

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&stdbool.h GT;的#define CHUNK 10typedef结构
{
    字符*海峡;
    结构节点*接下来的;
}节点;
节点* INITIALISE(节点**阵列,为int * amount_of_lists);
无效的push(节点**阵列,为int * amount_of_lists);
字符*的getString(无效);诠释的main()
{
    节点**头= NULL; //最初为null,指向头指针的动态数组
    INT amount_of_lists = 0;
    为int * no_of_heads =安培; amount_of_lists;    初始化(头,no_of_heads);
    初始化(头,no_of_heads);
    推(头,no_of_heads);
    推(头,no_of_heads);    返回0;
}节点初始化*(节点**阵列,为int * amount_of_lists)/ *重新分配内存为另一头指针ANS指针返回节点* /
{
    ++(* amount_of_lists);
    的printf(\\ N%D,* amount_of_lists);
    数组=(节点**)的realloc(数组的sizeof(节点*)*(* amount_of_lists));
    返回数组[(* amount_of_lists) - 1] =的malloc(sizeof运算(节点));
}INT readParameter为(int * amount_of_lists)
{
    INT参数= 0,控制= 0;
    BOOL重复= 0;
    做
    {
        如果(重复)
        {
            的printf(\\ nWrong参数,再试一次。);
        }
        的printf(\\ n输入list参数:);
        控制= scanf函数(%d个,&安培;参数);
        fflush(标准输入);
        重复= 1;
    }
    而(控制= 1 ||参数<!1 ||参数>(* amount_of_lists));    返回参数;
}无效的push(节点**阵列,为int * amount_of_lists)
{
    INT参数= readParameter(amount_of_lists) - 1;
    节点* TEMP =阵列[参数]
    数组[参数] =的malloc(sizeof运算(节点));
    数组[参数] - >接下来=温度;
    数组[参数] - >海峡=的getString();
}字符*的getString(无效)
{
    字符*线= NULL,* TMP = NULL;
    为size_t大小= 0,索引= 0;
    INT CH = EOF;    而(CH)
    {
        CH = GETC(标准输入);        / *检查,如果我们需要停止。 * /
        如果(CH == EOF || CH =='\\ n')
            CH = 0;        / *检查我们需要扩大。 * /
        如果(大小< =指数)
        {
            大小+ = CHUNK;
            TMP = realloc的(行,大小);
            如果(!TMP)
            {                免费(线);
                线= NULL;
                打破;
            }
            行= tmp目录;
        }        / *实际存储的东西。 * /
        行[指数++] = CH;
    }    回线;
}


解决方案

由于BLUEPIXY在他的评论中1)有些crypticly暗示,以修改的main()小号初始化(),你必须通过引用初始化(),我。即变化

  INITIALISE(头,no_of_heads);
    初始化(头,no_of_heads);

 初始化(安培;头,no_of_heads);
    初始化(安培;头,no_of_heads);

因此​​

 节点* INITIALISE(节点**阵列,为int * amount_of_lists)

修改

 节点* INITIALISE(节点***阵列,为int * amount_of_lists)

数组中修改 *阵列,我。即

  *数组= realloc的(*数组的sizeof(节点*)* amount_of_lists);
    回报(*数组)[* amount_of_lists - 1] =的malloc(sizeof运算(节点));

I'm trying to implement program in with i can create ~arbitrary number of singly linked lists dynamically and perform operations on particular one (defined by parameter). I create dynamic array of head pointers so that i can refer to the certain head node defined by paramater(index of an array + 1). Parameter is just (1,2,3..number of lists). So far I have managed to implement only initialise and push function but the program after complilation doesn't work as expected. Where is the problem?

#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;
}

解决方案

As BLUEPIXY somewhat crypticly hinted at in his comment 1), in order to modify main()'s heads in initialise(), you have to pass heads by reference to initialise(), i. e. change

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

to

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

consequently

node *initialise( node **array, int *amount_of_lists )

changes to

node *initialise(node ***array, int *amount_of_lists)

and inside array changes to *array, i. e.

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

这篇关于链表,带参数的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆