代码
Status InsertList(LinkList head,DataType x,int i)
{
ListNode *p;
p = head;
int j = 1;
while(p->next&&j<i){
p = p->next;
++j;
}
if(p == NULL)
{
printf("Position Error");
return ERROR;
}
s =(LinkNode*)malloc(sizeof(ListNode));
s->data = x;
s->next =p->next;
p->next =s;
return OK;
}
代码
Status DeleteList(LinkList head,int i)
{
ListNode *p,*r;
p = head;
int j =1;
while(p->next&&j<i)
{
p = p->next;
++j;
}
if(p == NULL||p->next ==NULL||j>i)
{
printf("Position Error");
return ERROR;
}
r = p->next;
p->next = r->next;
free(r);
return OK;
}
引申 :如何删除单链表的头元素?
代码
void RemoveHead(LinkList head)
{
ListNode *p;
p= head->next;
head->next = p->next;
free(p);
}