删除列表中的节点

发布于 2025-01-21 22:01:23 字数 289 浏览 2 评论 0原文

*x = L->list[i];      /* Save the deleted element to parameter x */
for(j = i+1; j <= L->size-1; j++)
L->list[i] = L->list[i+1];
L->size--;            /* The number of data elements is reduced by 1*/
return 1;

我无法完全删除节点,而不是它只是替换值,但没有删除节点本身

*x = L->list[i];      /* Save the deleted element to parameter x */
for(j = i+1; j <= L->size-1; j++)
L->list[i] = L->list[i+1];
L->size--;            /* The number of data elements is reduced by 1*/
return 1;

I can't delete the node fully, instead of this, its just replacing the value, but the node itself is not deleted

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾`听者〃 2025-01-28 22:01:23

在此内,

for(j = i+1; j <= L->size-1; j++)
L->list[i] = L->list[i+1];

此语句中使用的变量i

L->list[i] = L->list[i+1];

没有更改。

看来您的意思是,

for(j = i+1; j < L->size; j++)
    L->list[j-1] = L->list[j];

如果数组list动态分配,则应该重新分配它,因为例如,

for(j = i+1; j < L->size; j++)
    L->list[j-1] = L->list[j];
L->size--;

T *tmp = realloc( L->list, L->size * sizeof( *tmp ) );
if ( tmp != NULL ) L->list = tmp;

您需要将t用数组的实际类型替换。我正在使用t,因为从您的问题中不知道该数组的元素类型是什么。

Within this for loop

for(j = i+1; j <= L->size-1; j++)
L->list[i] = L->list[i+1];

the variable i used in this statement

L->list[i] = L->list[i+1];

is not being changed.

It seems you mean

for(j = i+1; j < L->size; j++)
    L->list[j-1] = L->list[j];

If the array list is allocated dynamically then you should reallocate it as for example

for(j = i+1; j < L->size; j++)
    L->list[j-1] = L->list[j];
L->size--;

T *tmp = realloc( L->list, L->size * sizeof( *tmp ) );
if ( tmp != NULL ) L->list = tmp;

You will need to substitute T with the actual type of elements of the array. I am using T because it is unknown from your question what is the type of elements of the array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文