本关任务:补全 delAt 函数,实现根据指定位置删除链表中对应的结点。
#include "linearList.h"
node * delAt(node * h, int i)
{
// 链表为空
if (h == NULL) return NULL;
// 现在链表非空
node* a = NULL;
node* b = h;
int pos_of_b = 0;
bool found = false;
for (; b;)
{
if (pos_of_b == i)
{
found = true;
break;
}
pos_of_b++;
a = b;
b = b->next;
}
if (!found) return h;
if (a == NULL)
{
node* cpy = b->next;
delete b;
return cpy;
}
else
{
a->next = b->next;
delete b;
return h;
}
}