C 链表仅存储最后一个元素

C 链表仅存储最后一个元素,c,linked-list,dynamic-memory-allocation,singly-linked-list,C,Linked List,Dynamic Memory Allocation,Singly Linked List,我的链表似乎只存储我输入的最后一个元素。换句话说,如果我添加3辆车并尝试打印list->head->Manufacturer,那么我将得到我输入的最后一辆车。如果我尝试打印list->head->next->manufacturer,我将得到我输入的最后一个元素 另外,更有趣的是,print语句 printf("the new node in begining func is %s\n", list->head->data->manufacturer);

我的链表似乎只存储我输入的最后一个元素。换句话说,如果我添加3辆车并尝试打印list->head->Manufacturer,那么我将得到我输入的最后一辆车。如果我尝试打印list->head->next->manufacturer,我将得到我输入的最后一个元素

另外,更有趣的是,print语句

  printf("the new node in begining func is %s\n", list->head->data->manufacturer);
list->head->next = newNode;
printf("the new nodein func using next as a newNOde is %s\n",list->head->next->data->manufacturer);
在我将下一个元素插入链表之前,它似乎已经存在

这是我的结构

  typedef struct Node_car{
    Car *data;
    struct Node_car *next;
}Node;

struct LinkedCarList_s{
    Node *head;
    Node *tail;
};
这是我的add函数

LinkedCarList carListCreate(){
    LinkedCarList new;
    new=(malloc(sizeof(LinkedCarList)));
    if(new==NULL) return 0;
    new->head = NULL;
    new->tail = NULL;
    return new;
}

int addNewNode(LinkedCarList list, Car *c){
if(list->head==NULL && list->tail==NULL){
    Node *newNode;
    newNode = (malloc(sizeof(Node)));
    newNode->data = c;
    newNode->next = NULL;
    list->head = newNode;
    list->tail = newNode;
    
    printf("in  if part add new func i added%s\n",newNode->data->manufacturer);
    return 1;
    
}
else{
    printf("the new node in begining func is %s\n", list->head->data->manufacturer);
    Node *newNode;
    newNode = (Node*)(malloc(sizeof(Node)));
    if(!newNode) return 0;
    newNode->data = c;
    newNode->next =list->head;
    
    list->head->next = newNode;
    printf("the new node in func is %s\n", list->head->data->manufacturer);
    printf("the new node as a newNOde is %s\n",newNode->data->manufacturer);
    printf("the new nodein func using next as a newNOde is %s\n",list->head->next->data->manufacturer);
    
}
return 1;
}

我这样调用函数:

LinkedCarList lcarList;
lcarList =carListCreate();


addNewCar(lcarList);

函数中的内存分配
carListCreate

new=(malloc(sizeof(LinkedCarList)));
这是不正确的。您正在为指针分配内存

你需要写作

new = malloc( sizeof( *new ) );
list->head = newNode;
并在函数
addNewNode
中替换此语句

  printf("the new node in begining func is %s\n", list->head->data->manufacturer);
list->head->next = newNode;
printf("the new nodein func using next as a newNOde is %s\n",list->head->next->data->manufacturer);
你需要写作

new = malloc( sizeof( *new ) );
list->head = newNode;
这些声明

printf("the new node in func is %s\n", list->head->data->manufacturer);
printf("the new node as a newNOde is %s\n",newNode->data->manufacturer);
将输出相同的数据,因为在上述分配点之后,
head
newNode
将输出到列表的同一节点

这句话呢

  printf("the new node in begining func is %s\n", list->head->data->manufacturer);
list->head->next = newNode;
printf("the new nodein func using next as a newNOde is %s\n",list->head->next->data->manufacturer);
如果列表仅包含一个节点,则可以调用未定义的行为。所以把它拿走

还要检查您是否总是将指向Car类型的同一对象的指针传递给函数。如果是这样,那么您需要复制指针c指向的对象。比如说

newNode->data = malloc( sizeof( Car ) );
*newNode->data = *c;
这个函数调用
addNewCar

addNewCar(lcarList);
不正确,因为函数需要两个参数

int addNewNode(LinkedCarList list, Car *c){

但事实并非如此work@qua你错了。这很有效。不起作用的是您没有显示的代码。@qua此内存分配新=(malloc(sizeof(LinkedCarList));这是不正确的。您需要编写new=malloc(sizeof(*new))@qua LinkedCarList不是一个结构。否则就不能编写new=(malloc(sizeof(LinkedCarList));因为new必须是指针。因此,LinkedCarList似乎是指针的别名。@qua然后再读一次我的答案。不要强制转换
malloc()
返回的值。