c - 将项目添加到链接列表

标签 c linked-list

您好,我正在尝试创建一个链接列表来测试一个函数,但它给了我一个错误,我无法找出我的代码有什么问题。当我用拳头输入数字时,在我按下回车键后 cmd 窗口停止工作。

#include <stdio.h>
#include <stdlib.h>
typedef struct llist *link;
typedef struct llist{
    int id;
    link next, pre;
} list;

list *location;
int id_input, size = 0;

int search (int id);
int add(void);
int read(void);
int delete(short id);

int main();

int read(void)
{
    printf("enter input id: ");
    scanf("%d", id_input);
    getchar();
}

int add(void)
{
    list *new_item;
    new_item = (list*)malloc(sizeof(list));
    printf("%p", new_item);
    switch(size==0){
        case 1:{
            location = new_item;
            location->pre = NULL;
            location->next = NULL;
            break;
        }
        case 0:{
            location->next=new_item;
            new_item->pre=location;
            new_item->next=NULL;
            location=new_item;
            break;
        }
    }
    location->id = id_input;
}

int main()
{
    int x,i;
    printf("start to append the list\n");
    for(i=0; i<10; i++){
        read();
        add();
    }
    return 0;
}

最佳答案

int read(void)
{
    printf("enter input id: ");
    scanf("%d", &id_input); //you are missing & here
    getchar();
}

关于c - 将项目添加到链接列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29255333/

相关文章:

c - 禁用 DejaGnu 选项

c - 什么会导致 exec 失败?接下来发生什么?

c - 链表计数到 20

java-8 - 链表 .echo() 与 .peek() 的区别

c - 有序链表插入

c++ - 使用 OpenMP 处理单链表

c++ - 如何为链表编写_find 方法?

c - 在红黑树中插入元素时出现 Malloc 错误

将十六进制的 char* 转换为普通 char*?

c - 在 c 中为 'fscanf' 使用定界符