【发布时间】:2020-02-08 14:53:38
【问题描述】:
这是我为单链表中的节点交换编写的函数。 还有其他功能追加,删除,长度等。 现在,当执行 nodeswap 时,左侧节点丢失了。 例如..... 链表是 1->2->3 交换后它变成 1->3
void nodeswap()
{
struct node *p,*q,*r;
int i=1,loc,l;
l=len();
printf("At what position you want to swap nodes?\n");
scanf("%d",&loc);
if(loc>l)
{
printf("Swap not possible , no nodes beyond the location\n");
}
else
{
p=root;
while(i<loc-1)
{
p=p->link;
i++;
}
//access nodes
q=p->link;
r=q->link;
}
//swap
//p,q,r
//p,r,q
q=r->link;
r->link=q;
p->link=r;
}
【问题讨论】:
标签: data-structures linked-list swap