链表插入

来源:百度知道 编辑:UC知道 时间:2024/05/07 09:07:26
设计接口函数int ListInsert_order (Node *H , DataType item); 向递增有序的单链表H中插入新的元素item,插入后单链表仍然有序。

int ListInsert_order (Node *H , DataType item){
Node *p,*q = H;
p = (Node *)malloc(sizeof(Node));
p->data = item;
p->next = NULL;
if(NULL == q){
H->next = p;
return 1;
}
while(NULL != q){
if(q->data <= p->data){
p->next = q->next;
q->next = p;
return 1;
}
p = p->next;
}
return 0;
}