将项目添加到链接列表

时间:2015-03-25 12:12:18

标签: 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;
}

1 个答案:

答案 0 :(得分:2)

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