2015-03-25 168 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(); 
}