#include "LinkList.h"
/*
Args:
* head:pointer of the head node.
Return:
* True or false.
Description:
* Create an empty linked list with a head node.
* The "next" field of the head node should point to NULL.
*/
_Bool CreateEmptyList(Node** head)
{
// 开辟空间
*head = (Node*)malloc(sizeof(Node));
(*head)->next = NULL;
return true;
}
/*
Args:
* addr: pointer of an array.
* n: length of the array.
* head:pointer of the head node.
Return:
* True or false.
Description:
* Initialize a linked list with an array.
*/
_Bool CreateList(DataType* addr, unsigned int n, Node** head)
{
(*head)->next = NULL;
/* 初始化head->next
* 不断的进行插入n个元素
*/
for (int i = n - 1; i >= 0; i--) {
Node* tmp = (Node*)malloc(sizeof(Node));
tmp->data = addr[i];
tmp->next = (*head)->next;
(*head)->next = tmp;
}
return true;
}
/*
Args:
* head: pointer of the head node.
Returns:
* True or false.
Description:
* Destroy the linked list.
* Release all allocated memory.
*/
_Bool DestroyList(Node* head)
{
/*
* 遍历节点,保存下一跳的指针,然后释放当前元素,再重新赋值
*/
Node* p = head->next, * q;
while (p->next) {
q = p->next;
free(p);
p = q;
}
free(head);
free(p);
return true;
}
(linklist.h和main.c文件已知,oj上仅补全linklist.c,在vs2019上可以成功运行但oj无法通过)
这只是函数实现,外部调用函数的代码呢?
成功运行≠OJ通过
成功运行,即使能运行出给的测试样例的结果,也不一定OJ会过,这就要看实现方法还有各方面细节了。
希望对题主有所帮助,可以的话,帮忙点个采纳!