- 难点分析
一级指针与二级指针易混用;
在链表中找到第一个含有特定值的结点并删除,在链表中想要找到某个特定结点,必须遍历整个链表,代码如下;
struct linkList { int data; struct linkList *next; }; typedef struct linkList link;
void DELETE(link **head, int e) { if (head == NULL || *head == NULL) return; link *d = NULL; if ((*head)->data == e) { d = *head; *head = (*head)->next; } else { link *p = *head; while (p->next->data != e&&p->next != NULL) { p = p->next; } if (p->next != NULL&&p->next->data == e) { d = p->next; p->next = p->next->next; } } if (d != NULL) { delete d; d = NULL; } } 删除第i个元素,并将值返回; int DElete(link **head, int i, int *e)//删除第i个元素,并将返回值存为e
{ if (head == NULL || *head == NULL) return ERROR; link *q=NULL; link *p = *head; int j = 1; while (p->next&&j < i) { p = p->next; ++j; } if (!(p->next) || j>i) { return ERROR; } q = p->next; //此处是删除的p的下家; p->next = q->next; *e=q->data; free(q); return OK; }
剑指offer-删除特定元素结点
最新推荐文章于 2023-11-07 15:12:36 发布