CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Linked List

  1. #1
    Join Date
    Mar 2003
    Posts
    29

    Linked List

    Hi Gurus,

    I have created a linked list but dosent work. Can any one tell me what is worng, please.

    Thanks and Regards,
    Abbas

    struct mass_list_s
    {
    double a_v_cg[4];
    char *name;
    struct mass_list_s *link;
    }


    add_to_list(struct mass_list_s **q,double av_cg[4],char nm[64])
    {
    struct mass_list_s *temp;

    int i;
    char buff[1000];
    temp=*q;



    if(*q==NULL)
    {
    *q=malloc(sizeof(struct mass_list_s));
    temp=*q;
    }

    else
    {
    while(temp->link!=NULL)temp=temp->link;
    temp->link=malloc(sizeof(struct mass_list_s));
    temp=temp->link;
    }

    for(i=0;i<4;i++)temp->a_v_cg[i]=av_cg[i];
    temp->name=nm;
    temp->link=NULL;
    }
    display_list(struct mass_list_s *q)
    {
    while(q!=NULL)
    {
    printf("%l,%l,%l,%l",q->a_v_cg[0],q->a_v_cg[1],q->a_v_cg[2],q->a_v_cg[3]);
    q=q->link;
    }
    }

  2. #2
    Join Date
    Oct 2001
    Location
    Venezuela
    Posts
    35
    Hi. I would like to know what seems to be the problem, because it seems to work fine. I only did a change:

    temp->name=nm;

    changed to:

    int l = strlen(nm);
    temp->name = (char*)malloc(l+1);
    strcpy(temp->name, nm);

    Also, when you are printing, you can't use %l, because that's for long int, and you have double, so you must use %lf.

    The correct way to use it is like this:

    mass_list_s *p=NULL;
    double a[4]={1.,2.,3.,4.};
    double b[4]={4.,5.,6.,7.};
    double c[4]={8.,9.,10.,11.};
    add_to_list(&p,a,"test#1");
    add_to_list(&p,b,"test#2");
    add_to_list(&p,c,"test#3");

    display_list(p);

    I tried it with the following program:

    #include <stdio.h>
    #include <string.h>
    #include <malloc.h>

    struct mass_list_s
    {
    double a_v_cg[4];
    char *name;
    struct mass_list_s *link;
    };


    void add_to_list(struct mass_list_s **q,double av_cg[4],char nm[64])
    {
    struct mass_list_s *temp;

    int i;
    temp=*q;

    if(*q==NULL)
    {
    *q=(mass_list_s*)malloc(sizeof(struct mass_list_s));
    temp=*q;
    }

    else
    {
    while(temp->link!=NULL)temp=temp->link;
    temp->link=(mass_list_s*)malloc(sizeof(struct mass_list_s));
    temp=temp->link;
    }

    for(i=0;i<4;i++)temp->a_v_cg[i]=av_cg[i];
    //temp->name=nm;
    int l = strlen(nm);
    temp->name = (char*)malloc(l+1);
    strcpy(temp->name, nm);
    //done
    temp->link=NULL;
    }

    void display_list(struct mass_list_s *q)
    {
    while(q!=NULL)
    {
    printf("%lf,%lf,%lf,%lf\n",q->a_v_cg[0],q->a_v_cg[1],q->a_v_cg[2],q->a_v_cg[3]);
    q=q->link;
    }
    }

    int main()
    {
    mass_list_s *p=NULL;
    double a[4]={1.,2.,3.,4.};
    double b[4]={4.,5.,6.,7.};
    double c[4]={8.,9.,10.,11.};
    add_to_list(&p,a,"test#1");
    add_to_list(&p,b,"test#2");
    add_to_list(&p,c,"test#3");

    display_list(p);

    return 1;
    }


    Hope this helps,

    KabalProg
    Last edited by KabalProg; March 7th, 2003 at 08:55 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured