gpt4 book ai didi

c - 链表和结构

转载 作者:行者123 更新时间:2023-11-30 19:23:28 25 4
gpt4 key购买 nike

我想知道我的程序设计是否正确,以及了解我的评论区域是否正在做它应该做的事情。我收到这些编译错误,这些错误可能与我的代码的注释段相关,并且我会撒谎以获得一些帮助。谢谢!

  part1.c:15:6: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'insert'
part1.c: In function 'main':
part1.c:43:14: error: incompatible types when assigning to type 'struct point' from type 'int'
part1.c:49:44: error: invalid type argument of '->' (have 'struct point')
part1.c:49:59: error: invalid type argument of '->' (have 'struct point')
part1.c:55:5: error: incompatible type for argument 1 of 'free'
/usr/include/stdlib.h:488:13: note: expected 'void *' but argument is of type 'struct point'






char *chars[3]= {"a","b","c"};

int nums[3]= {5,8,9};

struct point {char *letter;
int number;
struct point *next;};

struct point* insert(struct point list[],char *rqdLetters, int rqdNums)
{
struct point *new;

new = (struct point*)malloc(sizeof(struct point));
if(new == NULL)
fprintf(stderr,"error!");

new->letter = rqdLetters;
new->number = rqdNums;

new->next = head;
head = new;

//not sure if i'm returning the a pointer to the start of new list
return head;
}

int main(int argc, char **argv)
{
//not sure if i need to declare these here or in the insert
struct point list[3];
struct point *head = NULL;
struct point *next;
struct point *new;

int i;
for (i = 0; i < 3; i++)
{
//return result put back into the pointer to the start of the list
head[i] = insert(list[i], chars[i], nums[i]);
}

int j;
for(j = 0; j < 3; j++)
{
printf("letter %s and number %d\n", list[j]->letter, list[j]->number);
}

int z;
for(z = 0; z < 3; z++)
{
free(list[z]);
}

return 0;
}

最佳答案

乍一看,您的代码存在几个问题。首先,您没有正确声明变量。

new = list;

应该是:

struct point* new;

你的函数签名看起来也有点可疑。如果您要返回指向数据结构的指针,它应该类似于:

struct point* insert(...) { ... }

在更一般的层面上,我确实觉得你对链表的想法可能有点不对劲。要表示一个列表,您只需保留列表的 headtail,而不是保留点数组。

如果您创建一个数据结构来保存这些指针,通常会有所帮助。然后,您可以将此结构传递给对列表进行操作的函数,例如insert() 函数。

作为一个简单的例子(未经测试):

struct node {
struct node *next;
char letter;
int number;
}

struct list {
struct node *head;
struct node *tail;
}

/* create a new list */
struct list* list_new(void) {
struct list *L = malloc(sizeof(struct list));
L->head = NULL;
L->tail = NULL;
}

/* add a new node to the list */
void list_insert(struct list *list, char in_letter, int in_number) {
struct node *node = malloc(sizeof(struct node));
node->letter = in_letter;
node->number = in_number;
node->next = NULL;

if (list->head == NULL) { /* empty list */
list->head = node;
list->tail = node;
} else { /* append to list */
list->tail->next = node;
list->tail = node;
}
}

然后您可以这样使用它:

int i;
char chars[3]= {"a","b","c"};
int nums[3]= {5,8,9};

struct list *mylist = list_new();

for (i = 0; i < 3; i++)
{
list_insert(mylist, chars[i], nums[i]);
}
<小时/>

回应:

... and i am not sure if i am supposed to declare it inside insert or main, i did in the main however

这取决于您打算在何处使用变量以及这些变量的预期生命周期。正如上面的评论所述,您可能需要加深对范围界定规则的理解。

关于c - 链表和结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11763310/

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