【发布时间】:2020-06-27 03:06:36
【问题描述】:
所以我得到了以下结构:
typedef struct nodeStruct
{
int data;
struct nodeStruct *next;
}Node;
typedef struct linkedList
{
Node *head, *tail;
}List;
我必须创建一个函数,使用给定的函数原型将现有列表的内容复制到新列表中:
List *copyList(List *passedList);
我该怎么做呢?它让我感到窒息。
编辑:如果有帮助,我有这个函数来创建一个空列表:
List *createList()
{
List *newList = (List *)malloc(sizeof(List));
if (newList == NULL)
{
puts("ERROR: Could not allocate memory\n");
return NULL;
}
newList->head = newList->tail = NULL;
return newList;
}
Edit x2:我有一个 createNode 函数和一个 insertAtTail (append) 函数,如下所示:
Node *createNode(int nodeValue) // this function creates a node with the value that is passed to it
{
Node* newNode = (Node*)malloc(sizeof(Node));
newNode -> data = nodeValue;
newNode -> next = NULL;
return newNode;
}
int insertAtTail(int nodeValue, List *passedList)
{
Node *newNode = (Node *)malloc(sizeof(Node));
if (newNode == NULL)
{
puts("ERROR: Could not allocate memory.\n");
return -1;
}
newNode -> data = nodeValue;
newNode -> next = NULL;
if (passedList->tail == NULL)
{
passedList->head = passedList->tail = newNode;
}
else
{
passedList->tail->next = newNode;
passedList->tail = newNode;
}
return 1;
}
提前致谢!
【问题讨论】:
-
我假设您遍历列表以在途中复制并建立列表的副本。
-
@Thomas 我不太确定如何使用linkedList 结构来做到这一点。如果只是 nodeStruct 会简单得多,但这让我更难理解。
-
你有在列表末尾添加元素的功能吗?如果没有,写一个?然后,当您逐步浏览旧列表时,您使用该函数将旧列表中当前节点的副本添加到新列表的尾部。理想情况下,您有一个函数可以将函数“应用”到列表的每个元素,但这是为将来准备的——现在,编写代码以或多或少地复制列表。
-
"So I am given the following structs:"-- 感谢您的教授知道他在做什么。在您的函数中,只需声明一个新指针来表示您复制的列表,然后遍历当前列表,调用add节点函数将当前列表中的每个节点添加到副本中,请参见add()in ,例如Singly Linked List of Integers。然后只需返回指向副本的指针。 -
另一个想法是,由于您的
list(包装器)仅包含head/tail指针,因此您实际上不需要为列表分配。只需将您的返回类型更改为List并使用 自动存储持续时间 声明您的副本并返回结构(保存分配并释放List- 您只需要担心释放Node列表中的节点)
标签: c algorithm linked-list