2011-01-05 93 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