开发者

database creation using dynamic memory allocation in C

I am new to programming and dont know much about linked list... help me in coding a program--

Take data from users and create your database.

          a&g开发者_StackOverflow社区t;Data: Name, Age, Date of birth
          b>Memory for each entry should be dynamically created.

I have created a structure- struct database{ char name[25]; int age[5]; int dob[10]; struct database *next; }; Tell me how to proceed now...


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;

The basic idea is that whenever you create a new cel, you use the 'next' pointer in order to chain it in the linked list. For instance you can add a new cell at the end of the list:

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;                            
}

or

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;
}


I am not going to give you the code, but these link will surely help you.

http://en.wikipedia.org/wiki/Linked_list

http://richardbowles.tripod.com/cpp/linklist/linklist.htm

Also its better to go back and refer the book (as pointed by David, in comments)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜