Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. linked list adding operation with two structure type

linked list adding operation with two structure type

Scheduled Pinned Locked Moved C / C++ / MFC
helpdata-structuresperformancetutoriallearning
13 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    quartaela
    wrote on last edited by
    #1

    hi there i am working on a project that reads some data from a file and storing it into nodes. i actually made some operations like adding,deleting,search ect. in singly linked list with one structure type. now i am trying to use two structure types which are;

    typedef struct node {

    int birth\_date;
    int zipcode;
    int phone\_num;
    char first\_name\[50\];
    char last\_name\[50\];
    char city\[50\];
    char address\[50\];
    char email\_addr\[50\];
    
    struct node\* fn\_next;
    struct node\* ln\_next;
    struct node\* birdat\_next;
    

    } NODE;

    typedef struct {

    int count;
    NODE\* head;
    NODE\* tail;
    NODE\* current;
    

    }LIST;

    and this is the block which i read and store the data and finally call add_Node function;

    while ( !feof(myfile) ) {

        if( !(student = (NODE\*) malloc(sizeof(NODE))) ) {
    
            printf("ERROR NOT ENOUGH MEMORY!!!\\n");
            exit(100);
        }
    
        fscanf(myfile,"%s", &(student->first\_name) );
        fscanf(myfile,"%s", &(student->last\_name) );
        fscanf(myfile,"%s", &(student->email\_addr) );
        fscanf(myfile,"%d", &(student->phone\_num) );
        fscanf(myfile,"%s", &(student->address) );
        fscanf(myfile,"%s", &(student->city) );
        fscanf(myfile,"%d", &(student->zipcode) );
    
        add\_Node(&list,student);
    
        printf("%s\\n", student->first\_name );
    
    }
    

    }

    and of course add_Node function;

    void add_Node(LIST** list, NODE* student) {

        if( (\*list)->head == NULL ) {
    
            student->fn\_next = (\*list)->head;
            (\*list)->head = student;
            (\*list)->tail = student;
    
            printf("Adding operation is succesfull\\n");
    
            (\*list)->count++;
    
            return;
        }
    
        else {
    
            (\*list)->current = (\*list)->head;
    
            while ( (\*list)->current != NULL )
    
                (\*list)->current = (\*list)->current->fn\_next;
    
            (\*list)->current->fn\_next = student;
            student->fn\_next = NULL;
    
            (\*list)->tail = (\*list)->current;
    
            return;
        }
    

    }

    i am taking segmentation faults in add_Node and i don't know how to fix it.

    L A 2 Replies Last reply
    0
    • Q quartaela

      hi there i am working on a project that reads some data from a file and storing it into nodes. i actually made some operations like adding,deleting,search ect. in singly linked list with one structure type. now i am trying to use two structure types which are;

      typedef struct node {

      int birth\_date;
      int zipcode;
      int phone\_num;
      char first\_name\[50\];
      char last\_name\[50\];
      char city\[50\];
      char address\[50\];
      char email\_addr\[50\];
      
      struct node\* fn\_next;
      struct node\* ln\_next;
      struct node\* birdat\_next;
      

      } NODE;

      typedef struct {

      int count;
      NODE\* head;
      NODE\* tail;
      NODE\* current;
      

      }LIST;

      and this is the block which i read and store the data and finally call add_Node function;

      while ( !feof(myfile) ) {

          if( !(student = (NODE\*) malloc(sizeof(NODE))) ) {
      
              printf("ERROR NOT ENOUGH MEMORY!!!\\n");
              exit(100);
          }
      
          fscanf(myfile,"%s", &(student->first\_name) );
          fscanf(myfile,"%s", &(student->last\_name) );
          fscanf(myfile,"%s", &(student->email\_addr) );
          fscanf(myfile,"%d", &(student->phone\_num) );
          fscanf(myfile,"%s", &(student->address) );
          fscanf(myfile,"%s", &(student->city) );
          fscanf(myfile,"%d", &(student->zipcode) );
      
          add\_Node(&list,student);
      
          printf("%s\\n", student->first\_name );
      
      }
      

      }

      and of course add_Node function;

      void add_Node(LIST** list, NODE* student) {

          if( (\*list)->head == NULL ) {
      
              student->fn\_next = (\*list)->head;
              (\*list)->head = student;
              (\*list)->tail = student;
      
              printf("Adding operation is succesfull\\n");
      
              (\*list)->count++;
      
              return;
          }
      
          else {
      
              (\*list)->current = (\*list)->head;
      
              while ( (\*list)->current != NULL )
      
                  (\*list)->current = (\*list)->current->fn\_next;
      
              (\*list)->current->fn\_next = student;
              student->fn\_next = NULL;
      
              (\*list)->tail = (\*list)->current;
      
              return;
          }
      

      }

      i am taking segmentation faults in add_Node and i don't know how to fix it.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Sure it goes wrong, as this statement

      while ( (*list)->current != NULL ) (*list)->current = (*list)->current->fn_next;

      is bound to result in (*list)->current being zero/null, hence the following

      (*list)->current->fn_next=...

      will bomb. Some ideas: 1. if you want to add a node at the end of the list, and your data structures have a tail member, why not use it, rather than scanning the list? 2. try not to have special cases. When done properly, one often does not need them. And when one does, try making the alternate paths as short as possible, they tend to have common things (such as return, and count++ of which you forgot one). :)

      Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

      Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

      Q 1 Reply Last reply
      0
      • L Luc Pattyn

        Sure it goes wrong, as this statement

        while ( (*list)->current != NULL ) (*list)->current = (*list)->current->fn_next;

        is bound to result in (*list)->current being zero/null, hence the following

        (*list)->current->fn_next=...

        will bomb. Some ideas: 1. if you want to add a node at the end of the list, and your data structures have a tail member, why not use it, rather than scanning the list? 2. try not to have special cases. When done properly, one often does not need them. And when one does, try making the alternate paths as short as possible, they tend to have common things (such as return, and count++ of which you forgot one). :)

        Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

        Q Offline
        Q Offline
        quartaela
        wrote on last edited by
        #3

        i changed my way to implement the linked list with only NODE structure which is the way i know well. but this fault is not acceptable for me, i am angry to myself now because i want implement to list with using LIST structure. anyway, could you explain why i can't print the data with using code like this --> list->head->first_name

        L 1 Reply Last reply
        0
        • Q quartaela

          hi there i am working on a project that reads some data from a file and storing it into nodes. i actually made some operations like adding,deleting,search ect. in singly linked list with one structure type. now i am trying to use two structure types which are;

          typedef struct node {

          int birth\_date;
          int zipcode;
          int phone\_num;
          char first\_name\[50\];
          char last\_name\[50\];
          char city\[50\];
          char address\[50\];
          char email\_addr\[50\];
          
          struct node\* fn\_next;
          struct node\* ln\_next;
          struct node\* birdat\_next;
          

          } NODE;

          typedef struct {

          int count;
          NODE\* head;
          NODE\* tail;
          NODE\* current;
          

          }LIST;

          and this is the block which i read and store the data and finally call add_Node function;

          while ( !feof(myfile) ) {

              if( !(student = (NODE\*) malloc(sizeof(NODE))) ) {
          
                  printf("ERROR NOT ENOUGH MEMORY!!!\\n");
                  exit(100);
              }
          
              fscanf(myfile,"%s", &(student->first\_name) );
              fscanf(myfile,"%s", &(student->last\_name) );
              fscanf(myfile,"%s", &(student->email\_addr) );
              fscanf(myfile,"%d", &(student->phone\_num) );
              fscanf(myfile,"%s", &(student->address) );
              fscanf(myfile,"%s", &(student->city) );
              fscanf(myfile,"%d", &(student->zipcode) );
          
              add\_Node(&list,student);
          
              printf("%s\\n", student->first\_name );
          
          }
          

          }

          and of course add_Node function;

          void add_Node(LIST** list, NODE* student) {

              if( (\*list)->head == NULL ) {
          
                  student->fn\_next = (\*list)->head;
                  (\*list)->head = student;
                  (\*list)->tail = student;
          
                  printf("Adding operation is succesfull\\n");
          
                  (\*list)->count++;
          
                  return;
              }
          
              else {
          
                  (\*list)->current = (\*list)->head;
          
                  while ( (\*list)->current != NULL )
          
                      (\*list)->current = (\*list)->current->fn\_next;
          
                  (\*list)->current->fn\_next = student;
                  student->fn\_next = NULL;
          
                  (\*list)->tail = (\*list)->current;
          
                  return;
              }
          

          }

          i am taking segmentation faults in add_Node and i don't know how to fix it.

          A Offline
          A Offline
          Andrew Phillips
          wrote on last edited by
          #4

          This is obviously an assignment but I'll give you some ideas. a. It's better to use an existing container like std::list but if this is C and you don't have one or you have to write your own linked list then ... b. You don't need to store current. I also assume you store tail and count for efficiency as you can work these out too. c. You store tail but then in add_Node() you scan the list for the end anyway. d. It may be simpler just to add new students to the start of the list rather than the end.

          NODE \* tmp = head;
          head = student;
          student->next = tmp;
          

          e. If you step through your code the bvious problem is assigning through a NULL pointer. Why not do it like this:

          void add_Node(LIST** list, NODE* student)
          {
          NODE ** pcurr;
          for (pcurr = &head; *pcurr != NULL; pcurr = &((*pcurr)->next) )
          ; // no loop body
          *pcurr = student;
          student->next = NULL;
          }

          Andrew Phillips http://www.hexedit.com andrew @ hexedit.com

          Q 1 Reply Last reply
          0
          • A Andrew Phillips

            This is obviously an assignment but I'll give you some ideas. a. It's better to use an existing container like std::list but if this is C and you don't have one or you have to write your own linked list then ... b. You don't need to store current. I also assume you store tail and count for efficiency as you can work these out too. c. You store tail but then in add_Node() you scan the list for the end anyway. d. It may be simpler just to add new students to the start of the list rather than the end.

            NODE \* tmp = head;
            head = student;
            student->next = tmp;
            

            e. If you step through your code the bvious problem is assigning through a NULL pointer. Why not do it like this:

            void add_Node(LIST** list, NODE* student)
            {
            NODE ** pcurr;
            for (pcurr = &head; *pcurr != NULL; pcurr = &((*pcurr)->next) )
            ; // no loop body
            *pcurr = student;
            student->next = NULL;
            }

            Andrew Phillips http://www.hexedit.com andrew @ hexedit.com

            Q Offline
            Q Offline
            quartaela
            wrote on last edited by
            #5

            hi andrew; firstly, i must create my own linked and i know how to create it and i know how to make some simple operations with linked list. but this is the first time i want to use LIST structure(as you can see there is a head,tail,current and count). for example i wanna use like this;

            list->head->first_name

            and logically isn't it true_?. in main function the code is work well if i use like this

            printf("%s\n", student->first_name );

            but if use like this it isn't working. so what is the difference_?.

            printf("%s\n", list->head->first_name );

            A 1 Reply Last reply
            0
            • Q quartaela

              i changed my way to implement the linked list with only NODE structure which is the way i know well. but this fault is not acceptable for me, i am angry to myself now because i want implement to list with using LIST structure. anyway, could you explain why i can't print the data with using code like this --> list->head->first_name

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              I did not tell you to drop the LIST structure; if you want new nodes added at the tail, having a tail pointer is the obvious way of doing it, as it is much faster than searching for the last node each time. And your original code would never reach the print statement as it bombs when you use the null reference that is the resulting of spinning the while loop. So either use the tail pointer and drop the while loop, or fix the while loop. :)

              Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

              1 Reply Last reply
              0
              • Q quartaela

                hi andrew; firstly, i must create my own linked and i know how to create it and i know how to make some simple operations with linked list. but this is the first time i want to use LIST structure(as you can see there is a head,tail,current and count). for example i wanna use like this;

                list->head->first_name

                and logically isn't it true_?. in main function the code is work well if i use like this

                printf("%s\n", student->first_name );

                but if use like this it isn't working. so what is the difference_?.

                printf("%s\n", list->head->first_name );

                A Offline
                A Offline
                Andrew Phillips
                wrote on last edited by
                #7

                > printf("%s\n", list->head->first_name ); Yes, this should work. It could be that list or list->head are NULL or uninitialised. Try: printf("%p %p %s\n", list, list->head, list->head->first_name ); which will print out the values of the pointers. Or step through the code and look at the values in the debugger.

                Andrew Phillips http://www.hexedit.com andrew @ hexedit.com

                L 1 Reply Last reply
                0
                • A Andrew Phillips

                  > printf("%s\n", list->head->first_name ); Yes, this should work. It could be that list or list->head are NULL or uninitialised. Try: printf("%p %p %s\n", list, list->head, list->head->first_name ); which will print out the values of the pointers. Or step through the code and look at the values in the debugger.

                  Andrew Phillips http://www.hexedit.com andrew @ hexedit.com

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  Andrew Phillips wrote:

                  printf("%p %p %s\n", list, list->head, list->head->first_name );

                  I guess one needs separate print statements here, as if one argument bombs, nothing will be printed at all. :)

                  Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                  Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                  Q 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    Andrew Phillips wrote:

                    printf("%p %p %s\n", list, list->head, list->head->first_name );

                    I guess one needs separate print statements here, as if one argument bombs, nothing will be printed at all. :)

                    Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                    Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                    Q Offline
                    Q Offline
                    quartaela
                    wrote on last edited by
                    #9

                    i opened another project to implement the list with LIST and NODE structure. but i am facing with an error. i couldn't fix it. the error is; expected '=', ',', ';', 'asm' or '__attribute__' before '*' token and the error is in create_list function which is;

                    LIST* create_list(void)
                    {

                    LIST\* list;
                    
                    list = (LIST\*) malloc(sizeof(LIST));
                    
                    if (!list) {
                    
                        printf("ERROR NOT ENOUGH MEMORY!!!\\n");
                        exit(100);
                    }
                    
                    else {
                    
                        list->count = 0;
                        list->current = NULL;
                        list->head = NULL;
                        list->tail = NULL;
                    
                    }
                    
                    return list;
                    

                    }

                    and this is the main function;

                    LIST* create_list(void);

                    int main( void ) {

                    LIST\* list;
                    
                    list = create\_list();
                    
                    build\_list(list);
                    
                    print\_list(list);
                    
                    return 0;
                    

                    }

                    L 1 Reply Last reply
                    0
                    • Q quartaela

                      i opened another project to implement the list with LIST and NODE structure. but i am facing with an error. i couldn't fix it. the error is; expected '=', ',', ';', 'asm' or '__attribute__' before '*' token and the error is in create_list function which is;

                      LIST* create_list(void)
                      {

                      LIST\* list;
                      
                      list = (LIST\*) malloc(sizeof(LIST));
                      
                      if (!list) {
                      
                          printf("ERROR NOT ENOUGH MEMORY!!!\\n");
                          exit(100);
                      }
                      
                      else {
                      
                          list->count = 0;
                          list->current = NULL;
                          list->head = NULL;
                          list->tail = NULL;
                      
                      }
                      
                      return list;
                      

                      }

                      and this is the main function;

                      LIST* create_list(void);

                      int main( void ) {

                      LIST\* list;
                      
                      list = create\_list();
                      
                      build\_list(list);
                      
                      print\_list(list);
                      
                      return 0;
                      

                      }

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #10

                      most compilers give file name (and/or class name) and line number; tell your IDE to always show line numbers in editor windows. And try double-clicking the error message, any of these measures should bring you right to the objectionable statement. BTW: Did you provide the definition of NODE and LIST before referencing them in your source code? :)

                      Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                      Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                      Q 2 Replies Last reply
                      0
                      • L Luc Pattyn

                        most compilers give file name (and/or class name) and line number; tell your IDE to always show line numbers in editor windows. And try double-clicking the error message, any of these measures should bring you right to the objectionable statement. BTW: Did you provide the definition of NODE and LIST before referencing them in your source code? :)

                        Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                        Q Offline
                        Q Offline
                        quartaela
                        wrote on last edited by
                        #11

                        i can see the location of error when i double clicked :D. this is the line which error appears in create_list.h

                        LIST* create_list(void)

                        1 Reply Last reply
                        0
                        • L Luc Pattyn

                          most compilers give file name (and/or class name) and line number; tell your IDE to always show line numbers in editor windows. And try double-clicking the error message, any of these measures should bring you right to the objectionable statement. BTW: Did you provide the definition of NODE and LIST before referencing them in your source code? :)

                          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                          Q Offline
                          Q Offline
                          quartaela
                          wrote on last edited by
                          #12

                          well i solved the problem. and this is the most stupid problem i have ever faced. it takes for hours to see. i only changed the location include "structures.h" with include "create_list.h" :D. so know create_list function can realize the structures :)

                          L 1 Reply Last reply
                          0
                          • Q quartaela

                            well i solved the problem. and this is the most stupid problem i have ever faced. it takes for hours to see. i only changed the location include "structures.h" with include "create_list.h" :D. so know create_list function can realize the structures :)

                            L Offline
                            L Offline
                            Luc Pattyn
                            wrote on last edited by
                            #13

                            :zzz:

                            Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                            Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups