繁体   English   中英

链接列表中的全局指针

[英]Global Pointer in Linked List

因此,我遇到了这个问题,希望您能帮助我解决这个问题。 我有此链接列表,要求用户在其中输入数据。 当我仅使用一个集合但希望此链接列表与多个集合一起使用时,它适用,但是我已声明头节点为全局集合,但我不知道如何安排它。 问题是,当我在一组中输入数据时,它也将输入保留在第二组中。

这是链表的结构:

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

最后是Add函数:

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

正如Oli Charlesworth所说,您需要将要操作的列表作为参数传递给处理列表的每个函数。 一会儿,我将向您展示如何做,但首先让我对您的代码中的一些问题发表评论。

  • 您的代码看起来不整洁,因为您没有从输入中分离集合的逻辑(添加数字,检查数字是否已在集合中)(与scanf一切)。 相反,您可以使用Add函数以很大的一部分来实现所有功能。

  • 另外,为什么将x作为参数传递给Add 永远不会读取它(至少不会被scanf覆盖之后)。 这实际上应该是一个局部变量。 set ,您现在可以将其用作局部变量。

  • 您已经将typedef'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函数中添加元素,然后添加它们。

  • 由于列表的句柄是一个结构,因此可以通过向该结构添加新字段来轻松扩展列表接口。 例如,您可能想保留节点数或一个指示节点是否已排序的标志。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM