使用指向结构的指针创建链表和 C 中的内存分配

发布于 2024-10-31 12:46:50 字数 459 浏览 1 评论 0原文

我想我有一个概念问题。当我需要创建一个链接列表时,但我只是得到一个指向结构的指针(并且该结构包含一些数据类型和指针“next”)。我如何确保创建其他链接而不仅仅是根节点?我没有使用 malloc,否则这将是更明显的答案。该列表充当 malloc 本身。

提前致谢!

澄清:

将会有一个一维数组,它将充当固定大小的内存块。然后,链表将通过删除节点并将其放入数组中来“分配”,或者通过从数组中删除节点并将其添加回列表来“释放”数据。

示例

(由某些数据类型和指针定义的结构) 结构体称为节点

node array[10];  //this acts as memory
node* linked_list;  //this gives or removes data to memory (array)

I have a concept question I suppose. When I need to create a linked list, but I am just given a pointer to a struct (and the struct contains some data type and pointer "next"). How would I ensure that I am creating the other links and not just the root node? I am not using malloc or else this would be more apparent answer. This list is acting as malloc itself.

Thanks in advance!

To Clarify:

There will be single dimension array which will act as a block of memory of fixed sized. The linked list will then "allocate" by removing a node and putting it into the array or "free" data by removing it from array to add back to list.

example

(struct defined by some data type and pointer)
struct is called node

node array[10];  //this acts as memory
node* linked_list;  //this gives or removes data to memory (array)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情独悲 2024-11-07 12:46:50

这类似于我们在数据结构课上使用 FORTRAN 77 所做的事情(早在白垩纪中期);我们分配了一个固定大小的数组并将其用作内存池。

基本上,你必须维护一个“空闲”列表;这些是可供使用的节点。首次启动时,您初始化数组中的每个元素以显式指向下一个元素:

struct node {T data; struct node *next; } pool[N]; // for some type T
...
for (i = 0; i < N-1; i++)
  pool[i].next = &pool[i+1];
pool[i].next = NULL;

您的空闲列表最初指向池中的第一个元素:

struct node *free = &pool[0];

当您分配节点时,您将检索free<的元素/code> 指向,更新 free 以指向列表中的下一个可用元素(如果存在),然后根据需要初始化该元素:

if (free) // is there a free node available
{
  struct node *newNode = free;
  free = free->next;
  newNode->data = ...;
  newNode->next = NULL;
  ...
}

完成节点后,添加它回到free列表的头部:

node->next = free;
free = node;

当然,真实的代码会比这更好地组织,但这应该足以让您知道要做什么。

This is similar to how we did things in my Data Structures class using FORTRAN 77 (back in the mid-Cretaceous); we allocated a fixed-size array and used it as our memory pool.

Basically, you have to maintain a "free" list; these are the nodes that are available for use. When you first start up, you initialize each element in your array to explicitly point to the next element:

struct node {T data; struct node *next; } pool[N]; // for some type T
...
for (i = 0; i < N-1; i++)
  pool[i].next = &pool[i+1];
pool[i].next = NULL;

Your free list initially points to the first element in the pool:

struct node *free = &pool[0];

When you allocate a node, you retrieve the element that free points to, update free to point to the next available element in the list (if one exists), then initialize the element as necessary:

if (free) // is there a free node available
{
  struct node *newNode = free;
  free = free->next;
  newNode->data = ...;
  newNode->next = NULL;
  ...
}

When you're done with a node, you add it back to the head of the free list:

node->next = free;
free = node;

Naturally, real code would be better organized than this, but this should be enough to give you an idea of what to do.

失退 2024-11-07 12:46:50

我不确定这是否适用于您的特定问题,但我确实想指出有关在不使用 malloc 的情况下创建链接列表的一些信息。

如何分配链表的节点(无论是动态还是静态)并不重要。重要的是每个节点都实际存在并且指针有效。

例如,我们可以从数组构建一个链表。

#include <stdio.h>

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

int main() {

    int i;
    node linked_list[10];

    // build the linked list
    for ( i = 0; i < 10; i++ ) {
        linked_list[i].value = i * i;
        linked_list[i].next = ( i < 9 ) ? &linked_list[i+1] : 0;
    }

    // now traverse it
    node *head = &linked_list[0];
    while ( head ) {
        printf( "%d\n", head->value );
        head = head->next;
    }
}

编辑:为了使用数组作为“分配”或“释放”链表节点的位置,您可以使用一个标志来扩充节点struct指示该节点是否正在使用。

typedef struct node_t {
    char in_use;
    int value;
    struct node_t *next;
} node;

EDIT2:让我们最后一次尝试。下面是从数组中“分配”一个节点的函数(如果没有可用的节点,则返回 NULL)。假设 linked_list 是全局的。

node *get_node() {
    int i;
    for ( i = 0; i < 10; i++ )
        if ( !linked_list[i].in_use ) {
            linked_list[i].in_use = true;
            return &linked_list[i];
        }
    return 0;
}

EDIT3:如果数组要用作内存池,那么约翰·博德的答案就是要走的路。

I can't be sure if this applies to your particular question, but I do would like to point out something about creating a linked list without using malloc.

It doesn't really matter how you allocate the nodes of the linked list, be it dynamically or statically. What matters is that every node actually exists and the pointers are valid.

For example, we can build a linked list from an array.

#include <stdio.h>

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

int main() {

    int i;
    node linked_list[10];

    // build the linked list
    for ( i = 0; i < 10; i++ ) {
        linked_list[i].value = i * i;
        linked_list[i].next = ( i < 9 ) ? &linked_list[i+1] : 0;
    }

    // now traverse it
    node *head = &linked_list[0];
    while ( head ) {
        printf( "%d\n", head->value );
        head = head->next;
    }
}

EDIT: in order to use an array as the place from which to "allocate" or "free" the linked list nodes, you can augment the node struct with a flag to indicate if the node is being used or not.

typedef struct node_t {
    char in_use;
    int value;
    struct node_t *next;
} node;

EDIT2: let's give it one last try. Below is the function that "allocates" one node from the array (or returns NULL if none is available). Assume linked_list is global.

node *get_node() {
    int i;
    for ( i = 0; i < 10; i++ )
        if ( !linked_list[i].in_use ) {
            linked_list[i].in_use = true;
            return &linked_list[i];
        }
    return 0;
}

EDIT3: if the array is to be used as a memory pool the way to go is John Bode's answer.

赠佳期 2024-11-07 12:46:50

有几种可能的情况:

  • 链表正在使用 malloc。然后链表负责节点的分配和取消分配。这是标准实现。

  • 链表使用静态分配的内存块,即固定大小的静态数组。这样的链表更加复杂,因为您必须跟踪数组的哪些部分包含数据。您还需要跟踪链接列表的大小。

  • 链表是一个“带有索引查找表的节点数组”。然后,它不会分配任何数据,而是每个节点包含数组索引。链表需要跟踪列表大小。

  • 链表未执行任何分配。分配由调用者完成(静态或动态)。在这种情况下,链表仅跟踪下一个指针并且不分配任何内容。这个版本是糟糕的面向对象设计,因为它破坏了数据的私有封装。

op 更改后编辑:
看来您使用的是上面的版本 3。谷歌搜索“使用数组的链接列表”或类似内容。

There are several possible scenarios:

  • The linked list is using malloc. The linked list is then responsible for the allocation and de-allocation of nodes. This is the standard implementation.

  • The linked list is using a statically allocated chunk of memory, ie a static array of fixed size. Such a linked list is more complex, as you would have to keep track of which parts of the array that contain data. You will also need to keep track of the linked list size.

  • The linked list is an "array of nodes with index lookup table". It is then not allocating any data, but instead every node contains contains array indices. The linked list will need to keep track of the list size.

  • The linked list is not performing any allocation. Allocation is done by the caller (statically or dynamically). In this case the linked list only keeps track of the next pointers and allocates nothing. This version is poor object-oriented design, as it breaks the private encapsulation of data.

EDIT after op change:
Seems you are using version 3 above then. Google for "linked list using array" or similar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文