求数据结构函数

来源:百度知道 编辑:UC知道 时间:2024/06/21 18:22:10
在不带头结点的单链表中插入结点
void insertList (Node *head,Node *p,Elemtype x)
{
/* head为链表的表头指针,在单链表的p结点之后插入结点x */
Node *q;
q=(Node*)malloc(sizeof(Node)); /*给新结点分配存储单元*/
q->data=x;
if(head==NULL)
{
head=q;
q->next=NULL;
} /*head是空表的情况*/
else
{
q->next=p->next;
p->next=q;
} /*修改指针使新结点插在p结点的后面*/
}
在单链表的p结点之前插入结点x的函数!
我相信只要懂你就会!

void insertList (Node *head,Node *p,Elemtype x)
{
/* head为链表的表头指针,在单链表的p结点之前插入结点x */
Node *q,*r; //r为辅助指针
q=(Node*)malloc(sizeof(Node)); /*给新结点分配存储单元*/
q->data=x;
if(head==NULL)
{
head=q;
q->next=NULL;
} /*head是空表的情况*/
r=head;
while(r->next!=p)
{
r=r->next;
}
q->next=r->next;
r->next=q;
}