链表中的全局指针

时间:2014-01-05 10:45:01

标签: c linked-list dynamic-data singly-linked-list

所以我有这个问题,我希望你可以帮助我。我有这个链表,并要求用户在其中输入数据。它适用于我只使用一组但我希望这个链表可以使用多于1套,但我已经将头节点声明为全局,我不知道如何安排它。问题是,当我在一个集合中输入数据时,它也会保留第二个集合的输入。

这是链表的结构:

struct node//initializing a linked list
{
    int info;
    struct node*link;
}*start;

这就是我创建链表的方式:

list* createList()
{
    struct node*set;
    set=(struct node*)malloc(sizeof(struct node));
    if(start==NULL)
    {
        set->link=NULL;
        start=set;
    }
    return set;
} 

最后这是添加功能:

list* Add(list* set,int x)
{
    struct node *tempnode;
    if(start==NULL)
    {
        printf("Memory Allocation failed. Goodbye!");
    exit(EXIT_FAILURE);
    }
    set=start;
    printf("Please enter an input: \n");
    scanf("%d",&x);
    while(1)
    {
        while(x==set->info)
        {
            printf("Error! Please enter another integer: \n");
            scanf("%d",&x);
            set=start;
        }
        if(set->link!=NULL)
        set=set->link;
        else
            break;
    }
    tempnode=(struct node*)malloc(sizeof(struct node));
    tempnode->info=x;
    tempnode->link=NULL;
    set->link=tempnode;
    printf("%d was created successfully!\n",x);
    return set;
}

1 个答案:

答案 0 :(得分:1)

正如Oli Charlesworth已经评论过的那样,你需要将你想要操作的列表作为参数传递给处理列表的每个函数。我将在一瞬间向您展示,但让我先对您的代码中的一些问题进行评论。

  • 您的代码看起来不整洁,因为您没有将逻辑分开 从中添加(添加数字,检查数字是否已在集合中) 输入(与scanf有关的一切)。相反,你Add起作用 实现了大块的一切。

  • 另外,为什么将x作为参数传递给Add?永远不会读 (至少在被scanf覆盖之后)。这应该是真的 是一个局部变量。同样适用于set,您将其用作本地 目前是变量。

  • 你有{dev'fed struct nodelist,这是合法的, 但有点混乱。 (节点不是列表。)

好的,继续你的问题:你应该为每个新列表创建一个start = NULL。然后将start传递给所有列表函数。但是你必须要小心:大多数功能 - 比如打印或检查数字是否在列表中 - 都不会改变start的值。你的附加功能必须改变它,所以你应该将&start,即struct node **传递给这些功能。

因为这有点令人困惑,你可以使用另一种解决方案:创建一个表示列表的结构,为该列表编写一个创建函数,然后在该结构的指针上运行。

以下是一个示例实现:

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

typedef struct Set Set;
typedef struct Node Node;

struct Node {
    int info;
    Node *next;
};

struct Set {
    Node *head;
};

Set *set_create()
{
    Set *set = malloc(sizeof(*set));

    // enforce set != NULL
    set->head = NULL;
    return set;
}

void set_delete(Set *set)
{
    Node *node = set->head;

    while (node) {
        Node *next = node->next;
        free(node);
        node = next;
    }
}

int set_contains(const Set *set, int x)
{
    const Node *node = set->head;

    while (node) {
        if (node->info == x) return 1;
        node = node->next;
    }

    return 0;
}

void set_print(const Set *set)
{
    const Node *node = set->head;

    printf("{");
    while (node) {
        if (node != set->head) printf(", ");
        printf("%d", node->info);
        node = node->next;
    }
    printf("}\n");
}

Node *set_add(Set *set, int x)
{
    Node *node;

    if (set_contains(set, x)) return NULL;

    node = malloc(sizeof(*node));
    // enforce node != NULL

    node->info = x;
    node->next = NULL;

    if (set->head == NULL) {
        set->head = node;
    } else {
        Node *iter = set->head;

        while (iter->next) iter = iter->next;
        iter->next = node;
    }

    return node;
}

int main()
{
    Set *one = set_create();
    Set *two = set_create();

    set_add(one, 1);
    set_add(one, 1);
    set_add(one, 2);
    set_add(one, 3);
    set_add(one, 5);
    set_add(one, 1);
    set_add(one, 5);

    set_add(two, 1);
    set_add(two, 2);
    set_add(two, 4);

    set_print(one);
    set_print(two);

    set_delete(one);
    set_delete(two);

    return 0;
}

有几点需要注意:

  • 代码被拆分为执行特定任务的小功能。他们可能会互相打电话,但总是清楚做了什么。

  • 构造函数set_create创建一个指向空列表的指针。该指针是您应该作为第一个参数传递给所有列表函数的句柄

  • 在代码中,添加整数的函数会检查列表是否已包含整数。但是,因为set_add函数不处理来自用户的输入,所以它表示是否在其返回值中确实添加了一个节点:如果整数已经在列表中,则为NULL ,否则指向新节点的指针。

  • 创建列表并向其添加整数时,可以使用malloc分配内存。这意味着,您还需要一个使用free再次释放内存的函数,以便您可以避免内存泄漏。

  • 示例代码将一些硬连线整数添加到列表中;它不处理用户输入。您可以轻松编写循环以在main函数中添加元素,然后添加它们。

  • 因为列表的句柄是结构,所以您可以通过向该结构添加新字段来轻松扩展列表界面。例如,您可能希望保留节点数或指示节点是否已排序的标志。

相关问题