Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 为以结构为值的节点分配内存_C_Memory Management_Struct_Linked List_Malloc - Fatal编程技术网

C 为以结构为值的节点分配内存

C 为以结构为值的节点分配内存,c,memory-management,struct,linked-list,malloc,C,Memory Management,Struct,Linked List,Malloc,我已经想到了几种方法来做到这一点,但我想知道哪一种是正确的 任务要求我们遍历二维数组(迷宫),并从头到尾打印出最终解路径的每个坐标 坐标将存储在一个链表中,链表上有堆栈操作(推送、弹出等)。因此,一次只查看一个节点(头部将指向空节点,然后将值“推”到初始节点前面,然后根据需要弹出) 我的问题是为节点正确分配内存 我提出的解决方案可以概括为: struct coordinates{ int x; int y; }; struct linkedStruct { int* elem

我已经想到了几种方法来做到这一点,但我想知道哪一种是正确的

任务要求我们遍历二维数组(迷宫),并从头到尾打印出最终解路径的每个坐标

坐标将存储在一个链表中,链表上有堆栈操作(推送、弹出等)。因此,一次只查看一个节点(头部将指向空节点,然后将值“推”到初始节点前面,然后根据需要弹出)

我的问题是为节点正确分配内存

我提出的解决方案可以概括为:

struct coordinates{
    int x;
    int y;
};

struct linkedStruct
{
 int* elem;
 struct linkedStruct*  next;
};

typedef struct linkedStruct linked;
typedef linked* linkedPtr; 
所以大体上我想我会这么做

linkedPtr ptr = malloc(sizeof(linked));
ptr->elem = coordinates;

这是正确的吗?我第一次做链表。

更改
linkedStruct
以在其中包含
坐标的对象:

struct linkedStruct
{
   coordinates elem;
   struct linkedStruct*  next;
};
然后,在
malloc
之后,可以设置新
linkedStruct
的成员

linkedPtr ptr = malloc(sizeof(linked));
ptr->elem.x = 1;
ptr->elem.y = 2;
ptr->next = NULL;

我不会详细介绍链表算法,因为这似乎是你的家庭作业,可能应该阅读这本书和/或课程材料

因为元素值是一个
结构坐标
,所以在链表节点中存储一个指向它的指针。或者为了避免太多关于释放内容的担心,可以在节点中使用结构坐标而不是指针

struct linkedStruct
{
    struct coordinates elem;
    struct linkedStruct*  next;
};

typedef struct linkedStruct linked;
typedef linked* linkedPtr;
添加节点时,需要像在代码中一样设置指向新分配节点的
下一个
指针。然后设置指向结构的指针

linkedPtr ptr = malloc(sizeof(linked));
ptr->elem.x = 3;
ptr->elem.y = 4;
// assuming you are appending to the end of linked list and last_node->next is NULL
ptr->next = NULL
last_node->next = ptr;
您还需要将新创建的节点
ptr
next
指针设置为
NULL
,如果它位于末尾(后面没有下一个节点)。 完成后,需要使用
free
清理节点。 另一个建议是有两个结构,一个是链表,它存储指向列表开始和结束的指针(便于附加),另一个是表示节点的结构(带有元素和下一个指针)。使用该建议,上述代码可以在函数中:

typedef struct {
    linkedPtr *head;
    linkedPtr *last;
} linked_list; // Linked list with pointers to first and last node.

int *append(linked_list *list, int x, int y)
{
    linkedPtr ptr = malloc(sizeof(linked)); // This might fail to allocate
    if (ptr == NULL) {
        return -1; // This will be the error code for failure.
    }
    ptr->elem.x = x;
    ptr->elem.y = y;
    ptr->next = NULL;
    if (list->first == NULL) { // If list is empty.
        list->first = ptr;
        list->last = ptr;
    } else {
        list->last->next = ptr; // Point the last node to new node
        list->last = ptr; // Set the last to new last node
    }
    return 0; // Code success
}
要使用append,您需要创建
链接列表

linked_list *list = malloc(sizeof(linked_list)); // This might also fail.
list->head = NULL;
list->last = NULL;
if (append(list, 3, 4) == -1) {
  printf("Unable to allocate memory for appending to list\n");
}

当你完成后,不要忘记释放所有这些。

请。应该
int*elem;
be
struct coordinates*elem;
?蒂姆,malloc语句是我教授指导的直接副本,但在阅读之后,我不会再投了。谢谢你乔纳森,这是我的另一个想法,但我不确定我能做到t、 然而,它使事情变得更简单。你能告诉我在这种情况下如何分配内存吗?如果给
struct linkedStruct
一个
struct coordinates
类型的成员,而不是指向这样一个对象的指针,会更干净。这样可以在两端节省一些不必要的内存管理,并消除一个潜在的错误错误的根本原因。说到错误,你真的应该检查
malloc()
是否成功,而不是假设它会成功。