gpt4 book ai didi

c - C 中的单链表(insert_pos、sort、delete_elem)

转载 作者:行者123 更新时间:2023-11-30 16:45:05 25 4
gpt4 key购买 nike

我不知道为什么函数 insert_pos 不起作用。错误是:

member reference base type 'list' (aka 'struct le*') is not a structure or union.

#include <stdio.h>
#include "stdlib.h"

struct le{
int value;
struct le *next;
};
typedef struct le listenelement;
typedef listenelement *list;

int insert_pos(int v, int pos, list *l){
listenelement * new;
new = malloc(sizeof(listenelement));
new->value = v;

for(int i = 0; i < pos - 3; i++){
*l = l->next;
}
new->next = l->next;
l->next = new;
}

int delete elem(int e, list * l){

}
void sort(int m, list * l){

}

int main() {

return 0;
}

最佳答案

为什么 insert_pos 使用pos-3

要修复语法错误,该行应为:

        l = &((*l)->next);

当且仅当新节点插入到列表的前面时,insert_pos 才应更新传递的列表指针。

作为替代方案,insert_pos 可以返回指向列表的指针:

listenelement * insert_pos(int v, int pos, listenelement *l)

关于c - C 中的单链表(insert_pos、sort、delete_elem),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44231879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com