问题描述
因此,我的任务是在 Singly Linked List
中编写 C
的完整实现。
我之前写过 stack
和 dynamic vector
的实现,但是这一次,由于使用了 3 个不同的 typedef
,链表让我有点困惑。
我会像往常一样制作一个测试文件,但由于所有的 void *
类型转换,我很难编写一个。
因此我们必须遵循以下 typedefs
和给定的 prototypes
。所以它们都不能改变。
我还必须添加一个“虚拟节点”作为最后一个节点,这意味着总会有一个“虚拟节点”表示之前的一个它是列表中“真实”的最后一个节点。这是说明的一部分。
typedef struct slist slist_ty;
typedef struct slist_node slist_node_ty;
typedef slist_node_ty *slist_iter_ty;
这是我对结构的实现:
他们要求我们理论上允许在我们的节点中使用任何类型的数据,这就是我写 void *
的原因。
struct slist
{
slist_iter_ty head;
slist_iter_ty end;
};
struct slist_node
{
void *data;
slist_iter_ty next;
};
这些是函数:
/* Creates an empty single-linked list and returns pointer to the head */
/* returns NULL on failure*/
/* Complexity: O(1) */
slist_ty *SlistCreate(void)
{
slist_ty *new_list = (slist_ty *)malloc(sizeof(slist_ty));
if (NULL == new_list)
{
fprintf(stderr,"Failed to allocate memory\n");
return(NULL);
}
new_list->head = NULL;
/* create a dummy node that will represent the end of the list */
new_list->end = (slist_node *)malloc(sizeof(slist_node));
if (NULL == new_list->end)
{
fprintf(stderr,"Failed to allocate memory\n");
free(new_list);
return(NULL);
}
new_list->end->data = NULL;
new_list->end->next = NULL;
return(new_list->head);
}
/* Deletes entire List */
/* Complexity: O(n) */
void SlistDestroy(slist_ty *slist)
{
slist_iter_ty temp = NULL;
assert(slist);
while(NULL != slist->head)
{
tmp = slist->head;
slist->head = temp;
free(temp);
}
free(slist->end);
slist->end = NULL;
free(slist);
slist = NULL;
}
/* Insters the element after the iterator,returns iterator to the new node */
/* Todo Undefined behavIoUr if iter is slist_END */
/* Complexity: O(1) */
slist_iter_ty SlistInsert(slist_iter_ty iter,void *data)
{
slist_iter_ty new_node = NULL;
assert(iter);
assert(iter->next);
assert(data);
new_node->data = data;
new_node->next = iter->next;
iter->next = new_node;
return(new_node);
}
/* Returns iterator to end of the list */
/* Complexity: O(1) */
slist_iter_ty SlistIteratorEnd(const slist_ty *slist)
{
slist_iter_ty iterator = slist->head;
assert (slist);
if (NULL == slist->head)
{
return(NULL);
}
while (NULL != iterator->next->data)
{
iterator = iterator->next;
}
return(iterator);
}
我在获得反馈的请求中的问题是:
我应该free
制作任何新的slist_iter_ty
吗?
例如,我在最后一个函数中创建了一个 iterator
类型的 slist_iter_ty
,以帮助我遍历列表。但是我不能释放迭代器,因为我需要将它作为返回值返回。
我还在 new_node
函数中创建了一个 SlistInsert
。
它会作为 SlistDestroy
函数的一部分被释放吗?
谢谢。
解决方法
slist - 是列表。当你创建这个列表时你会使用 malloc,所以当你想要销毁它时你需要释放这个列表。
还有 - 每次使用插入时都使用 malloc。所以当你想销毁列表时,你需要从所有节点中清空它 - 所以你需要一个节点一个节点地释放
我可以看到您没有在 slist 插入中使用 mallloc - 如何在不使用 malloc 的情况下保留数据?
在销毁函数中
while(NULL != slist->head)
{
tmp = slist->head;
slist->head = temp;
free(temp);
}
我想你的意思是:
while(NULL != slist->head)
{
tmp = slist->head;
slist->head = slist->head->next;
free(tmp);
}
插入函数
slist_iter_ty new_node = NULL;
你应该写什么:
new_node = (slist_iter_ty) malloc(sizeof(slist_node));
在slist端函数
slist_iter_ty SlistIteratorEnd(const slist_ty *slist)
你可以直接返回(在你断言之后:)):
return (slist->end);
(否则它不会是 O(1) 它将是 O(n))