繁体   English   中英

尝试在C中创建一个空的链表

[英]Trying to create an empty linked list in C

我正在尝试创建一个空的链接列表,该列表要求用户提供列表可以容纳的最大术语数。 (我没有将我的代码添加为简单的printf)。 然后,我必须创建一个新函数,要求用户将输入插入到先前创建的列表中。

我的问题是,如何使create_q()函数返回空列表?

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

typedef struct node_t {
    int value;
    int priority;
    struct node_t *next;
}node;

typedef struct priority_linked_list {
    struct name *head;
    int current_size;
    int max_size;
}priority_list;

typedef node *Node;
typedef priority_list *List;

void create_q(int max_terms) {

    node *head = NULL;
    node *next = NULL;
    List *current_size = 0;
    List *max_size = max_terms;

}

在C语言中,链表通常被实现为一系列存储在堆中的节点,这些节点相互指向对方。 堆是在程序的整个生命周期中运行的持久性内存区域。

当您通常在C函数中创建变量并且函数返回时,您创建的变量将不再可访问。 但是,当您在函数中的堆上创建某些东西并返回该函数时,您在堆上分配的数据仍然存在。 但是,您无法访问它-除非函数返回指针。

因此,对create_q()所做的工作是在堆上创建链接列表(使用stdlib.h中的一个名为“ malloc”的函数),然后您将返回一个指向第一个节点的指针,让主函数知道在堆上的哪里找到第一个节点。 然后,第一个节点中将有一个指针,告诉程序在堆上的哪个位置可以找到第二个节点,依此类推。

但是,您可能以错误的方式处理链表。 除非这是用于某种家庭作业项目,否则您可能不想创建一个空的链表。 链表的好处之一是它是一种动态结构,您可以在其中轻松插入新节点。 您仍然可以使用一些变量来跟踪想要列表达到的最大大小,但是您可能直到需要时才真正想要创建节点。

只要记住什么是链表。 它是一组浮在堆上的节点(在C中),每个节点都存储一些数据,并包含指向下一个浮在堆上的节点的指针。 访问链接列表所需要的只是指向第一个节点的指针。 要添加新节点,您只需在列表中“遍历”直到到达最后一个节点,然后创建一个新节点并让最旧的节点指向该节点。

Is this what you had in mind?

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

struct node_t
{
    int value;
    int priority;

    struct node_t *next;
};

static int current_size;
static int max_size;
static struct node_t* head = NULL;

struct node_t* create_q(int);

struct node_t* create_q(int max_terms)
{
    int i; // loop counter/index

    current_size = max_terms;
    max_size = max_terms;

    if( NULL == (head = malloc(sizeof(struct node_t)*max_terms)))
    { // then, malloc failed
        perror("malloc failed for struct node_t list");
        exit( EXIT_FAILURE );
    }

    // implied else, malloc successful

    // set all fields to '0,0,Null'
    memset( head, 0x00, sizeof(struct node_t)*max_terms);

    // set all next links, except last link
    for(i=0;i<(max_terms-1);i++)
    {
        head[i].next = &head[i+1];
    }

    // set last link
    head[i].next = NULL;

    return( head );
} // end function: create_q

我怀疑您正在寻找以下类似内容来创建或初始化优先级链接列表。

/*****
 * alloc_q - allocate memory for the priority linked list
 */
struct priority_linked_list *alloc_q(void)
{
    struct priority_linked_list *list;

    list = malloc(sizeof(*list));
    return list;
}

/******
 * init_q - initialize the priority linked list
 */
void init_q(struct priority_linked_list *list, int max_terms)
{
    list->head = NULL;
    list->current_size = 0;
    list->max_size = max_terms;
}

/******
 * create_q - allocate AND initialize the priority linked list
 */
struct priority_linked_list *create_q(int max_terms)
{
    struct priority_linked_list *list;

    list = alloc_q();
    if (list == NULL) {
        return NULL;
    }
    init_q(list, max_terms);
    return list;
}

节点的分配及其在列表中的添加/删除将分别处理。

上面可能有错别字(我没有测试过)。 但是,它足以使您走上想要的道路。

希望能帮助到你。

暂无
暂无

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

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