2015-03-25 164 views
1

嗨,我想创建一个链接列表来测试一个函数,但它给了我一个错误,我找不出我的代码是什么问题。当我拳头输入数字时,我敲回车后,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; 
} 
+0

在'printf'后面加一个''fflush'阅读()' – 2015-03-25 12:17:09

+2

你说的 “它给错误” 是什么意思?这与“cmd窗口停止工作”相矛盾。 – 2015-03-25 12:18:32

+0

因为我不是母语为英语的人而对我的单词使用感到抱歉。我的意思是窗口花了时间,不能给出任何结果,并停止工作。这就像做无限循环而没有停止条件。那就是我的意思 – aukxn 2015-03-25 13:38:15

回答

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