【发布时间】:2015-10-24 07:36:52
【问题描述】:
void insert()
{
struct node *temp;
struct node*pre=start,*count=start;
int ps,i,c=0;
printf("Enter the position where u want to insert\n");
scanf("%d",&ps);
if(ps==1)
{insertbeg();}
else
{
while(count!=0)
{c++;
count=count->link;}
if(ps>c)
{insertend(ps);}
else
{
temp=(struct node *)malloc(sizeof(struct node));
if (temp==NULL)
{printf("Memory is full\n");}
else
{
for(i=1;i<ps;i++) //place1
{pre=pre->link;}
temp->link=pre;//place2
pre=temp;//place3
printf("Enter the element to inserted\n");
scanf("%d",&temp->data);
}
}
}
}
代码运行,但每当我在中间位置插入任何元素时,它都不会使用显示功能显示,而在开头插入 (insertbeg()) 并在结尾插入 (insertend() ) 正常工作和显示。 当我在 1,2&3 的地方替换下面的行时,元素会正常显示。
for(i=1;i<ps-1;i++)
temp->link=pre->link;
pre->link=temp;
我想知道为什么会发生这种情况,因为这两组语句看起来是相同的。
【问题讨论】:
-
无明显调试,DCV。
标签: c insert linked-list