数据结构已知指针P指向双向链表中的一个结点(非首结点、非尾结点),则: (1)将结点S插入在P结点的直接

2024-11-10 16:07:09
推荐回答(2个)
回答(1):

针对前插:
s->prior = p->prior;
p->prior->pnext = s;
s->pnext = p;
p->prior = s;
针对后插:
s->pnext = p->pnext;
p->pnext->prior = s;
s->prior = p;
p->pnext = s;

回答(2):

/* 插入p的前面 */
int*q;
q=p->prior;
s->next=p;
s->prior=q;
q->next=s;
p->prior=s;

/* 插入p的后面 */
int*q;
q=p->next;
s->next=q;
s->prior=p;
q->prior=s;
p->next=s;