2011-01-05 92 views
0

我是新來的編程和不知道很多關於鏈表...幫助我在從用戶編碼program--數據庫創建使用動態內存分配使用C

取數據,並創建你的數據庫。

  a>Data: Name, Age, Date of birth 
      b>Memory for each entry should be dynamically created. 

我已經創建了一個結構 - 結構數據庫{ 炭名稱[25]; int age [5]; int dob [10]; struct database * next; }; 告訴我現在該怎麼繼續......

+0

對於'C'動態分配內存,尋找'malloc'和'免費'。 – 2011-01-05 07:24:44

+0

是的,肯定我必須使用malloc和免費的,但我dnt knw如何創建鏈接列表..內鏈表只有我會使用malloc和免費,,,對嗎? – kamakshi 2011-01-05 07:29:18

+0

在您正在使用的入門書中查看它。請不要告訴我你正計劃通過在stackoverflow上提問來學習編程。這將是一個糟糕的進行方式。 – 2011-01-05 08:08:21

回答

0
struct database { 
    char name[25]; 
    int age[5]; 
    // in my opinion you should only keep dob, since age would have to be constantly updated 
    int dob[10]; 
    struct database *next; 
    } TCel, *TList, **Alist; 

的基本思路是,當你創建一個新的CEL,使用「下一個」指針,以IT連鎖鏈表。例如,您可以在列表的末尾添加一個新的小區:

AList InsEnd(AList aL, Info e) 
{ 
    TLista aux; 
    // allocate cel and set the information inside it 
    aux = AlocCel(e);      
    if (!aux) return aL;    
    while (*aL != NULL) 
     aL = &(*aL)->next; 
    // linking the node 
    *aL = aux;        
    return aL;        
} 

TList InsEnd2(TList aL, Info e) 
{ 
    TLista aux; 
    aux = AlocCel(e); 
    if(!aux) return aL; 
    while(aL->next != NULL) 
     aL = aL->next; 
    // linking the node 
    aL->next = aux; 
    return aL; 
} 
+0

如何檢查輸入數據的錯誤?意味着如果有人輸入12345作爲名字,如何檢查它並打印錯誤信息,同樣年齡也是我想問的。 – kamakshi 2011-01-06 10:01:28

+0

對於給定示例,所有應該在AlocCel函數中完成的可能是什麼?無論如何,你所問的問題都是非常基本的。您可以使用ASCII值或函數,如ctype.h庫中的isdigit(char)或isalpha(char)。你可能想看看這個鏈接:http://en.wikipedia.org/wiki/Ctype.h – Chris 2011-01-06 12:53:46