【问题标题】:Linked lists, operations with parameter链表,带参数的操作
【发布时间】:2015-08-21 13:50:48
【问题描述】:

我正在尝试实现程序,我可以动态创建〜任意数量的单链表并对特定的单链表执行操作(由参数定义)。我创建了头指针的动态数组,以便我可以引用参数定义的某个头节点(数组的索引 + 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;
}

【问题讨论】:

  • 怎么没有按预期工作?
  • 在它接受适当的参数后,程序就会崩溃。 push 函数肯定有问题,因为 getString 是我在某处找到的函数,它在我的其他程序中正常工作。
  • 0) typedef struct --> typedef struct node
  • 1) initialise 返回数组;` .. heads=initialise(heads, no_of_heads);initialise( node **array --> initialise( node ***array.还有array[(*amount_of_lists) - 1]-&gt;next = NULL;
  • 抱歉,除了:array[(*amount_of_lists) - 1]->next = NULL;你能写一些完整的句子吗?非常感谢。

标签: c singly-linked-list


【解决方案1】:

正如 BLUEPIXY 在他的评论 1) 中有些神秘地暗示的那样,为了修改 initialise() 中的 main()heads,您必须将 heads 通过引用传递给 @ 987654325@,我。 e.改变

    initialise(heads, no_of_heads);
    initialise(heads, no_of_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 内部更改为*array,即。 e.

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-14
    • 2015-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多