Thread: linked list issue

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    9

    linked list issue

    I have problem with printing a linked list , it print what i want but it report an error and exit directly .

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Check the error before allowing it to exit directly and do something else instead.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Do what qny said..But also it would be helpful to provide us some more information ...or some code or both

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    9
    here is the part that cause the error
    Code:
    struct node *position;
         position=head->next;
            while (position != NULL){
         strcpy(position->color,"RGBY\0");
        printf("%s\n",position->adjustent_countries);
               position=position->next;}

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If the error it reports is segmentation fault, then the links of the list are probably broken. You didn't insert the nodes correctly.

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Why do you assign in line 2 position to head->next? Do you want to start from the second node of the list?Because this is what you do.
    How is position->color declared?As a pointer to char?Have you allocated space for it?
    Are you sure position->adjustent_countries of every node has no garbage?

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    This code look ok. Your error must be somewhere else ... maybe right after the loop (when position has the value NULL) you try to dereference the pointer?

    Oh! Why do you use an extra character in the string literal? ?!?!?!?!!?
    "RGBY\0" is an array with 6 elements, the last two elements have the value '\0'.
    You might as well use the shorter array "RGBY" with 5 elements.
    Also the strcpy() call will never ever even see the 6th element of your original string

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    9
    here is my code , i'm just trying to print the linked list with the content of each node
    Code:
    #include<stdio.h>
    #include <string.h>
    struct node {
    
    
      char color[10] ;
      char adjustent_countries[17];
      char name;
      char assigned_colored ; // initally U --> not yet assigned
      int MRV; // =0 when the country is colored
      struct node *next;
    };
    void assigned(char country,struct node *head);
    //void print(struct node *head);
    
    
    //void forword_checking( char country,struct node *head);
    int main ()
    {
        //struct node *position;
       char assigned_colored ;
        ///----------------------------------------- create the countries and fill-----------------------------
        /// create node A-----------------------------------------------
            struct node *A;
    
    
        A= malloc(sizeof(struct node));
        head= malloc(sizeof(struct node));
        A->next= "NULL";
        A->MRV=4;
        A->assigned_colored ='U';
        A->name='A';
        printf("Node %c\n",A->name);
        printf("%c\n ",A->assigned_colored );
            printf("%d\n",A->MRV);
    
    
       // strcpy(A->color,"RGBY");
      //  A->color[0]='R';
        //A->color[1]='G';
        //A->color[2]='B';
        //A->color[3] = 'Y';
        //A->color[4]='\0';
            //printf("%s\n",A->color);
        strcpy(A->adjustent_countries, "BD\0");
        printf("%s\n",A->adjustent_countries);
        printf("**************************************************\n");
    
    
        /// create node B----------------------------------------------
      struct node *B;
        B= malloc(sizeof(struct node));
        A->next=B;
        B->next= "NULL";
        B->MRV=4;
       B->assigned_colored ='U';
     B->name='B';
        printf("Node %c\n",B->name);
        printf("%c\n ",B->assigned_colored );
            printf("%d\n",B->MRV);
    
    
        //strcpy(B->color,"RGBY\0");
           // printf("%s\n",B->color);
        strcpy(B->adjustent_countries, "CDE\0");
        printf("%s\n",B->adjustent_countries);
            printf("**************************************************\n");
    
    
    
    
    
    
    
    
     /// create node C----------------------------------------------
        struct node *C;
        C= malloc(sizeof(struct node));
        B->next=C;
        C->next= "NULL";
        C->MRV=4;
        C->assigned_colored ='U';
         C->name='C';
        printf("Node %c\n",C->name);
    
    
        printf("%c\n ",C->assigned_colored );
            printf("%d\n",C->MRV);
    
    
        //strcpy(C->color,"RGBY\0");
            //printf("%s\n",C->color);
        strcpy(C->adjustent_countries, "EH\0");
        printf("%s\n",C->adjustent_countries);
            printf("**************************************************\n");
    
    
        /// create node D----------------------------------------------
          struct node *D;
        D= malloc(sizeof(struct node));
        C->next=D;
        D->next= "NULL";
        D->MRV=4;
        D->assigned_colored ='U';
         D->name='D';
        printf("Node %c\n",D->name);
    
    
        printf("%c\n ",D->assigned_colored );
            printf("%d\n",D->MRV);
    
    
        //strcpy(D->color,"RGBY\0");
            //printf("%s\n",D->color);
        strcpy(D->adjustent_countries, "ABEFGI\0");
        printf("%s\n",D->adjustent_countries);
            printf("**************************************************\n");
    
    
        /// create node E----------------------------------------------
             struct node *E;
        E= malloc(sizeof(struct node));
        D->next=E;
        E->next= "NULL";
        E->MRV=4;
        E->assigned_colored ='U';
         E->name='E';
        printf("Node %c\n",E->name);
    
    
        printf("%c\n ",E->assigned_colored );
            printf("%d\n",E->MRV);
    
    
        //strcpy(E->color,"RGBY\0");
            //printf("%s\n",E->color);
        strcpy(E->adjustent_countries, "BCDGH\0");
        printf("%s\n",A->adjustent_countries);
            printf("**************************************************\n");
    
    
        /// create node F----------------------------------------------
        struct node *F;
        F= malloc(sizeof(struct node));
        E->next=F;
        F->next= "NULL";
        F->MRV=4;
        F->assigned_colored ='U';
         F->name='F';
        printf("Node %c\n",F->name);
    
    
        printf("%c\n ",F->assigned_colored );
            printf("%d\n",F->MRV);
    
    
        //strcpy(F->color,"RGBY\0");
           // printf("%s\n",F->color);
        strcpy(F->adjustent_countries, "DILO\0");
        printf("%s\n",F->adjustent_countries);
            printf("**************************************************\n");
    
    
        /// create node G----------------------------------------------
        struct node *G;
        G= malloc(sizeof(struct node));
        F->next=G;
        G->next= "NULL";
        G->MRV=4;
        G->assigned_colored ='U';
         G->name='G';
        printf("Node %c\n",G->name);
    
    
        printf("%c\n ",G->assigned_colored );
            printf("%d\n",G->MRV);
    
    
        //strcpy(G->color,"RGBY\0");
            //printf("%s\n",G->color);
        strcpy(G->adjustent_countries, "DEHIJK\0");
        printf("%s\n",G->adjustent_countries);
            printf("**************************************************\n");
    
    
        /// create node H----------------------------------------------
        struct node *H;
        H= malloc(sizeof(struct node));
        G->next=H;
        H->next= "NULL";
        H->MRV=4;
        H->assigned_colored ='U';
         H->name='H';
        printf("Node %c\n",H->name);
    
    
        printf("%c\n ",H->assigned_colored );
            printf("%d\n",H->MRV);
    
    
        //strcpy(H->color,"RGBY\0");
            //printf("%s\n",H->color);
        strcpy(H->adjustent_countries, "CEGJK\0");
        printf("%s\n",H->adjustent_countries);
            printf("**************************************************\n");
    
    
        /// create node I----------------------------------------------
        struct node *I;
        I= malloc(sizeof(struct node));
        H->next=I;
        I->next= "NULL";
        I->MRV=4;
        I->assigned_colored ='U';
         I->name='I';
        printf("Node %c\n",I->name);
    
    
        printf("%c\n ",I->assigned_colored );
            printf("%d\n",I->MRV);
    
    
        //strcpy(I->color,"RGBY\0");
            //printf("%s\n",I->color);
        strcpy(I->adjustent_countries, "DFGJLMO\0");
        printf("%s\n",I->adjustent_countries);
            printf("**************************************************\n");
    
    
        /// create node J----------------------------------------------
        struct node *J;
        J= malloc(sizeof(struct node));
        I->next=J;
        J->next= "NULL";
        J->MRV=4;
        J->assigned_colored ='U';
         J->name='J';
        printf("Node %c\n",J->name);
    
    
        printf("%c\n ",J->assigned_colored );
            printf("%d\n",J->MRV);
    
    
        //strcpy(J->color,"RGBY\0");
        //    printf("%s\n",J->color);
        strcpy(J->adjustent_countries, "GHIKMPQ\0");
        printf("%s\n",J->adjustent_countries);
            printf("**************************************************\n");
    
    
        /// create node K----------------------------------------------
        struct node *K;
        K= malloc(sizeof(struct node));
        J->next=K;
        K->next= "NULL";
        K->MRV=4;
        K->assigned_colored ='U';
         K->name='K';
        printf("Node %c\n",K->name);
    
    
        printf("%c\n ",K->assigned_colored );
            printf("%d\n",K->MRV);
    
    
    //    strcpy(K->color,"RGBY\0");
            //printf("%s\n",K->color);
        strcpy(K->adjustent_countries, "GHJNPQ\0");
        printf("%s\n",K->adjustent_countries);
            printf("**************************************************\n");
    
    
        /// create node L----------------------------------------------
        struct node *L;
        L= malloc(sizeof(struct node));
        K->next=L;
        L->next= "NULL";
        L->MRV=4;
        L->assigned_colored ='U';
         L->name='L';
        printf("Node %c\n",L->name);
    
    
        printf("%c\n ",L->assigned_colored );
            printf("%d\n",L->MRV);
    
    
        //strcpy(L->color,"RGBY\0");
            //printf("%s\n",L->color);
        strcpy(L->adjustent_countries, "FIO\0");
        printf("%s\n",L->adjustent_countries);
            printf("**************************************************\n");
    
    
        /// create node M----------------------------------------------
        struct node *M;
        M= malloc(sizeof(struct node));
        L->next=M;
        M->next= "NULL";
        M->MRV=4;
        M->assigned_colored ='U';
         M->name='M';
        printf("Node %c\n",M->name);
    
    
        printf("%c\n ",M->assigned_colored );
            printf("%d\n",M->MRV);
    
    
        //strcpy(M->color,"RGBY\0");
            //printf("%s\n",M->color);
        strcpy(M->adjustent_countries, "IJOP\0");
        printf("%s\n",M->adjustent_countries);
            printf("**************************************************\n");
    
    
        /// create node N----------------------------------------------
        struct node *N;
        N= malloc(sizeof(struct node));
        M->next=N;
        N->next= "NULL";
        N->MRV=4;
        N->assigned_colored ='U';
         N->name='N';
        printf("Node %c\n",N->name);
    
    
        printf("%c\n ",N->assigned_colored );
            printf("%d\n",N->MRV);
    
    
        //strcpy(N->color,"RGBY\0");
            //printf("%s\n",N->color);
        strcpy(N->adjustent_countries, "KQ\0");
        printf("%s\n",N->adjustent_countries);
            printf("**************************************************\n");
    
    
        /// create node O----------------------------------------------
        struct node *O;
        O= malloc(sizeof(struct node));
        N->next=O;
        O->next= "NULL";
        O->MRV=4;
        O->assigned_colored ='U';
         O->name='O';
        printf("Node %c\n",O->name);
    
    
        printf("%c\n ",O->assigned_colored );
            printf("%d\n",O->MRV);
    
    
    //    strcpy(O->color,"RGBY\0");
        //    printf("%s\n",O->color);
        strcpy(O->adjustent_countries, "FILM\0");
        printf("%s\n",O->adjustent_countries);
            printf("**************************************************\n");
    
    
        /// create node P----------------------------------------------
        struct node *P;
        P= malloc(sizeof(struct node));
        O->next=P;
        P->next= "NULL";
        P->MRV=4;
        P->assigned_colored ='U';
         P->name='P';
        printf("Node %c\n",P->name);
    
    
        printf("%c\n ",P->assigned_colored );
            printf("%d\n",P->MRV);
    
    
        //strcpy(P->color,"RGBY\0");
            //printf("%s\n",P->color);
        strcpy(P->adjustent_countries, "IJMQ\0");
        printf("%s\n",P->adjustent_countries);
            printf("**************************************************\n");
    
    
        /// create node Q----------------------------------------------
        struct node *Q;
        Q= malloc(sizeof(struct node));
        P->next=Q;
        Q->next= "NULL";
        Q->MRV=4;
        Q->assigned_colored ='U';
         Q->name='Q';
        printf("Node %c\n",Q->name);
    
    
        printf("%c\n ",Q->assigned_colored );
            printf("%d\n",Q->MRV);
    
    
    //    strcpy(Q->color,"RGBY\0");
        //    printf("%s\n",Q->color);
        strcpy(Q->adjustent_countries, "JKNP\0");
        printf("%s\n",Q->adjustent_countries);
            printf("**************************************************\n");
    
    
    ///----------------------------------------------------------------------///
    ///---------------------------------------------------------------------
    struct node *position;
         position=A;
            while (position != NULL){
         strcpy(position->color,"RGBY\0");
     printf(" %s \t  %s \t %c \t %c \t %d \n",position->color,position->adjustent_countries,position->name,position->assigned_colored,position->MRV);
               position=position->next;}
            return 0;
    }

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Code:
    B->next= "NULL";
    Wow I was right, you do this everywhere.

    "NULL" is not NULL. "NULL" is a string, and yes, it can eventually lead to segmentation faults if it is used to stand for NULL.

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Why don't you make a function insert that takes the list and append to it a node? This not the problem,but it is a good style.I mean if you had 100 nodes,what would you do?Write 100 times all this? :P

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Hey it could work! A, B, C, D ... Z, AA, AB, AC, ... ZZA ...

    C Programming/Procedures and functions - Wikibooks, open books for an open world

  12. #12
    Registered User
    Join Date
    Oct 2012
    Posts
    9
    yaaaay this was the problem ,,,thxxxxxx

  13. #13
    Registered User
    Join Date
    Oct 2012
    Posts
    9
    Quote Originally Posted by std10093 View Post
    Why don't you make a function insert that takes the list and append to it a node? This not the problem,but it is a good style.I mean if you had 100 nodes,what would you do?Write 100 times all this? :P
    i know it isn't a good style i'm just testing each function alone and then i would do it separated in a function of it's own

  14. #14
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Hayat Qyrt View Post
    i know it isn't a good style i'm just testing each function alone and then i would do it separated in a function of it's own
    Ok cool.Once you understand what is going on,take the next step to make the function

  15. #15
    Registered User
    Join Date
    Oct 2012
    Posts
    9
    Quote Originally Posted by qny View Post
    This code look ok. Your error must be somewhere else ... maybe right after the loop (when position has the value NULL) you try to dereference the pointer?

    Oh! Why do you use an extra character in the string literal? ?!?!?!?!!?
    "RGBY\0" is an array with 6 elements, the last two elements have the value '\0'.
    You might as well use the shorter array "RGBY" with 5 elements.
    Also the strcpy() call will never ever even see the 6th element of your original string
    if i use "RGBY" wirh strcpy it would print another character from a last use of strcpy .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Circular double linked list issue ..need help
    By spanlength in forum C Programming
    Replies: 3
    Last Post: 06-30-2012, 12:03 AM
  2. linked list issue
    By roaan in forum C Programming
    Replies: 6
    Last Post: 09-01-2009, 01:21 AM
  3. Linked List remove node issue
    By prihod in forum C Programming
    Replies: 1
    Last Post: 04-19-2008, 09:54 AM
  4. Doubly Linked List Deletion Issue
    By josephjah in forum C++ Programming
    Replies: 22
    Last Post: 07-22-2007, 03:00 PM
  5. Linked list copy constructor issue
    By Craptastic! in forum C++ Programming
    Replies: 1
    Last Post: 08-03-2003, 08:30 PM