从指数链表中删除节点 [英] Deleting node from linked list by index
问题描述
这是我的code从链表中删除一个节点。
vec_store
持有 SEQ
和尺寸
。变量 SEQ
持有的载体和一个指针。
有关某种原因,否则如果(I< S-GT&;大小-1)。
不起作用这是最后一个条件
谁能解决这个问题?顺便说一句,这是<强> C code。
无效delete_vec(vec_store S,int i)以
{
如果(ⅰ℃,||仲&GT;尺寸-1下;ⅰ)
{
的printf(因为索引%d超出范围的\\ n无法删除矢量,我);
}
否则,如果(我== 0)
{
节点温度;
TEMP = S-&gt;序列;
S-&gt;序列= S-&GT; seq-&gt;接下来,
S-GT&; size--;
免费(TEMP);
}
否则,如果(我== S-&GT;大小-1)
{
节点temp1中,TEMP2;
// temp1中的malloc =(sizeof运算(节点));
TEMP2 =的malloc(sizeof运算(节点));
temp1中= S-&gt;序列;
如果(temp1-&gt;接下来== NULL)
{
免费(temp1目录);
仲&gt;序列= NULL;
S-GT&; size--;
输出(S-GT&;大小-1 \\ n);
}
其他
{
而(temp1-&GT;!下次= NULL)
{
TEMP2 = temp1中;
temp1中= temp1-&gt;接下来,
}
免费(temp1目录);
temp2-&gt;接着= NULL;
S-GT&; size--;
输出(S-GT&;大小1 2 \\ n);
}
}
否则,如果(I&LT; S-GT&;大小-1)
{
节点temp1中,TEMP2;
诠释J;
temp1中= S-&gt;序列;
TEMP2 =的malloc(sizeof的(结构node_record)); 为(J = 0; J&下; = I-1; J ++)
{
TEMP2 = temp1中;
temp1中= temp1-&gt;接下来,
} 免费(temp1目录);
temp2-&gt;接着= NULL;
S-GT&; size--;
}
}
这code是无法访问的:
如果(temp1-&gt;接下来== NULL){
免费(temp1目录);
仲&gt;序列= NULL;
S-GT&; size--;
输出(S-GT&;大小-1 \\ n);
}
...因为 temp1目录
是列表中的第一个元素,因此,如果第一个元素也是最后一个元素,它只会发生 - 这意味着 S-方式&gt;大小
为1,所以这将是由早期的如果(我== 0)
的情况下抓住了
该分配 TEMP2
(发生在两个地方)是假的 - TEMP2
的价值得到反正覆盖,泄漏的内存分配你:
TEMP2 =的malloc(sizeof运算(节点));
最后,什么可能是你问的谎言这里的问题(在如果(I&LT; S-&GT;大小-1)
情况下):
免费(temp1目录);
temp2-&gt;接着= NULL;
这剃掉列表中的全部结束。你想保持列表的尾部左右 - 这样的:
temp2-&gt;接下来= temp1-&gt;接下来,
免费(temp1目录);
顺便说一句, TEMP2
和 temp1目录
是pretty平淡的变量名 - 什么 previous
和电流
还是什么?此外,对于的特殊情况下,如果(我== S-&GT;大小-1)
终止的完全的不必要的 - 它应该由精处理code您对如果(I&LT; S-GT&;大小-1)。
情况
This is my code for deleting a node from a linked list.
vec_store
holds seq
and size
. Variable seq
holds the vectors and a pointer.
For some reason, the else if(i<s->size-1)
doesn't work which is the last condition.
Can anyone solve the problem? By the way this is C code.
void delete_vec(vec_store s, int i)
{
if (i<0 || s->size-1<i)
{
printf("Cannot delete vector because index %d is out of bounds\n",i);
}
else if (i==0)
{
node temp;
temp = s->seq;
s->seq = s->seq->next;
s->size--;
free(temp);
}
else if(i==s->size-1)
{
node temp1, temp2;
//temp1 = malloc(sizeof (node));
temp2 = malloc(sizeof (node));
temp1=s->seq;
if(temp1->next==NULL)
{
free(temp1);
s->seq=NULL;
s->size--;
printf("s->size-1\n");
}
else
{
while (temp1->next!=NULL)
{
temp2 = temp1;
temp1 = temp1->next;
}
free(temp1);
temp2->next=NULL;
s->size--;
printf("s->size-1 2\n");
}
}
else if(i<s->size-1)
{
node temp1,temp2;
int j;
temp1=s->seq;
temp2 = malloc(sizeof (struct node_record));
for(j=0;j<=i-1;j++)
{
temp2=temp1;
temp1 = temp1->next;
}
free(temp1);
temp2->next=NULL;
s->size--;
}
}
This code is unreachable:
if(temp1->next==NULL){
free(temp1);
s->seq=NULL;
s->size--;
printf("s->size-1\n");
}
...because temp1
is the first element in the list, and so it would only happen if the first element was also the last element - which implies s->size
is 1, so this would have been caught by the earlier if (i==0)
case.
This allocation to temp2
(which occurs in two places) is bogus - temp2
's value gets overwritten anyway, leaking the memory you allocated:
temp2 = malloc(sizeof (node));
Finally, what is probably the problem you're asking about lies here (in the if(i<s->size-1)
case):
free(temp1);
temp2->next=NULL;
This chops the entire end off the list. You want to keep the tail of the list around - like this:
temp2->next = temp1->next;
free(temp1);
By the way, temp2
and temp1
are pretty uninspiring variable names - what about previous
and current
or something? Also, the special case for if(i==s->size-1)
is completely unnecessary - it should be handled fine by the code you have for the if(i<s->size-1)
case.
这篇关于从指数链表中删除节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!