链表(补交)

代码:
插入操作:
Status ListInsert_L(LinkList &L, int i, ElemType e){ //第i个位置前插入节点
p = L; j = 0;
while( p && j < i-1){
p = p -> next;
++j;
}
if(!p || j> i - 1) return ERROR;
s = (LinkList )malloc(sizeof(LNode)); //生成新节点
s->data = e;
s->next = p->next;
p->next = s;
return ok;
}

删除操作:
Status ListDelete_L(LinkList &L, int i, ElemType &e){
//删除第i个元素,并有e作为返回值
P = L; j = 0;
while( p->next && j < i-1) {
p = p ->next;
j++;
}
if(!(p->next) || j > i-1) return ERROR; //删除位置不合理
q = p->next;
p->next = q->next;
e = q->data;
free(q);
return ok;
}

参考资料:
https://blog.csdn.net/iteye_9716/article/details/81900059

posted @ 2020-12-22 16:02  游浩贤  阅读(45)  评论(0编辑  收藏  举报