(1)查找元素并删除
int findAndDelete(LNode *list, int x){
LNode *p;
p = list;
while(p->next != NULL){
if(p->next == x){
break;
}
p = p->next;
}
if(p->next == NULL){
return 0;
}
else{
LNode *del;
del = p->next;
p->next = p->next->next;
free(del);
return 1;
}
}
(2)删除单链表中的最小值
void deleteMin(LNode *list){
LNode *p = list->next, *pre = list; *premin = pre;
while(p != MULL){
if(p->data < minp->data){
minp = p;
premin = pre;
}
pre = p;
p = p->next;
}
premin->next = minp->next;
free(minp);
}
(3)查找链表中倒数第k个结点
int findkLast(LNode *list,int k){
LNode *p, *q;
p = list->next;
q = list;
int i = 1;
while(p != NULL){
p = p->next;
i++;
if(i > k)
q = q->next;
else{
printf("%d",p->data);
return 1;
}
}
}
|