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. creating a linked list

creating a linked list

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresperformanceannouncement
8 Posts 4 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.
  • S Offline
    S Offline
    steve_rm
    wrote on last edited by
    #1

    Hello, I am creating a linked list. I have found that the best way to develop the linked list is to have the head and tail in another structure. My products struct will be nested inside this structure. And I should be passing the list to the function for adding and deleting. I find this concept confusing. I have implemented the initialize, add, and clean_up. However, I am not sure that I have done that correctly. When I add a product to the list I declare some memory using calloc. But I am thinking shouldn't I be declaring the memory for the product instead. I am really confused about this adding. Many thanks for any suggestions,

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

    #define PRODUCT_NAME_LEN 128

    typedef struct product_data
    {
    int product_code;
    char product_name[PRODUCT_NAME_LEN];
    int product_cost;
    struct product_data_t *next;
    }product_data_t;

    typedef struct list
    {
    product_data_t *head;
    product_data_t *tail;
    }list_t;

    void add(list_t *list, int code, char name[], int cost);
    void initialize(list_t *list);
    void clean_up(list_t *list);

    int main(void)
    {
    list_t *list = NULL;

    initialize(list);
    add(list, 10, "Dell Inspiron", 1500);
    clean\_up(list);
    
    getchar();
    
    return 0;
    

    }

    void add(list_t *list, int code, char name[], int cost)
    {
    // Allocate memory for the new product
    list = calloc(1, sizeof(list_t));
    if(!list)
    {
    fprintf(stderr, "Cannot allocated memory");
    exit(1);
    }

    if(list)
    {
        // First item to add to the list
        list->head->product\_code = code;
        list->head->product\_cost = cost;
        strncpy(list->head->product\_name, name, sizeof(list->head->product\_name));
        // Terminate the string
        list->head->product\_name\[127\] = '/0';
    } 
    

    }

    // Initialize linked list
    void initialize(list_t *list)
    {
    // Set list node to null
    list = NULL;
    list = NULL;
    }

    // Release all resources
    void clean_up(list_t *list)
    {
    list_t *temp = NULL;

    while(list)
    {
        temp = list->head;
        list->head = list->head->next;
        free(temp);    
    }
    list = NULL;
    list = NULL;
    temp = NULL;
    

    }

    M D L 3 Replies Last reply
    0
    • S steve_rm

      Hello, I am creating a linked list. I have found that the best way to develop the linked list is to have the head and tail in another structure. My products struct will be nested inside this structure. And I should be passing the list to the function for adding and deleting. I find this concept confusing. I have implemented the initialize, add, and clean_up. However, I am not sure that I have done that correctly. When I add a product to the list I declare some memory using calloc. But I am thinking shouldn't I be declaring the memory for the product instead. I am really confused about this adding. Many thanks for any suggestions,

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

      #define PRODUCT_NAME_LEN 128

      typedef struct product_data
      {
      int product_code;
      char product_name[PRODUCT_NAME_LEN];
      int product_cost;
      struct product_data_t *next;
      }product_data_t;

      typedef struct list
      {
      product_data_t *head;
      product_data_t *tail;
      }list_t;

      void add(list_t *list, int code, char name[], int cost);
      void initialize(list_t *list);
      void clean_up(list_t *list);

      int main(void)
      {
      list_t *list = NULL;

      initialize(list);
      add(list, 10, "Dell Inspiron", 1500);
      clean\_up(list);
      
      getchar();
      
      return 0;
      

      }

      void add(list_t *list, int code, char name[], int cost)
      {
      // Allocate memory for the new product
      list = calloc(1, sizeof(list_t));
      if(!list)
      {
      fprintf(stderr, "Cannot allocated memory");
      exit(1);
      }

      if(list)
      {
          // First item to add to the list
          list->head->product\_code = code;
          list->head->product\_cost = cost;
          strncpy(list->head->product\_name, name, sizeof(list->head->product\_name));
          // Terminate the string
          list->head->product\_name\[127\] = '/0';
      } 
      

      }

      // Initialize linked list
      void initialize(list_t *list)
      {
      // Set list node to null
      list = NULL;
      list = NULL;
      }

      // Release all resources
      void clean_up(list_t *list)
      {
      list_t *temp = NULL;

      while(list)
      {
          temp = list->head;
          list->head = list->head->next;
          free(temp);    
      }
      list = NULL;
      list = NULL;
      temp = NULL;
      

      }

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #2

      Me think you don't need the list structure, you only need the product_data structure. in your code, you only need a pointer to the first element in the list.

      This signature was proudly tested on animals.

      1 Reply Last reply
      0
      • S steve_rm

        Hello, I am creating a linked list. I have found that the best way to develop the linked list is to have the head and tail in another structure. My products struct will be nested inside this structure. And I should be passing the list to the function for adding and deleting. I find this concept confusing. I have implemented the initialize, add, and clean_up. However, I am not sure that I have done that correctly. When I add a product to the list I declare some memory using calloc. But I am thinking shouldn't I be declaring the memory for the product instead. I am really confused about this adding. Many thanks for any suggestions,

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

        #define PRODUCT_NAME_LEN 128

        typedef struct product_data
        {
        int product_code;
        char product_name[PRODUCT_NAME_LEN];
        int product_cost;
        struct product_data_t *next;
        }product_data_t;

        typedef struct list
        {
        product_data_t *head;
        product_data_t *tail;
        }list_t;

        void add(list_t *list, int code, char name[], int cost);
        void initialize(list_t *list);
        void clean_up(list_t *list);

        int main(void)
        {
        list_t *list = NULL;

        initialize(list);
        add(list, 10, "Dell Inspiron", 1500);
        clean\_up(list);
        
        getchar();
        
        return 0;
        

        }

        void add(list_t *list, int code, char name[], int cost)
        {
        // Allocate memory for the new product
        list = calloc(1, sizeof(list_t));
        if(!list)
        {
        fprintf(stderr, "Cannot allocated memory");
        exit(1);
        }

        if(list)
        {
            // First item to add to the list
            list->head->product\_code = code;
            list->head->product\_cost = cost;
            strncpy(list->head->product\_name, name, sizeof(list->head->product\_name));
            // Terminate the string
            list->head->product\_name\[127\] = '/0';
        } 
        

        }

        // Initialize linked list
        void initialize(list_t *list)
        {
        // Set list node to null
        list = NULL;
        list = NULL;
        }

        // Release all resources
        void clean_up(list_t *list)
        {
        list_t *temp = NULL;

        while(list)
        {
            temp = list->head;
            list->head = list->head->next;
            free(temp);    
        }
        list = NULL;
        list = NULL;
        temp = NULL;
        

        }

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        steve_rm wrote:

        Many thanks for any suggestions,

        Compare what you have to:

        typedef struct product_data
        {
        int product_code;
        char product_name[PRODUCT_NAME_LEN];
        int product_cost;

        product\_data \*next;
        

        } product_data_t;

        typedef struct list
        {
        product_data_t *head;
        product_data_t *tail;
        } list_t;

        // adds to end of list
        void add(list_t *l, int code, char name[], int cost)
        {
        product_data_t *node;

        // create a new node and populate its members
        node = new product\_data\_t;
        node->product\_code = code;
        node->product\_cost = cost;
        strcpy(node->product\_name, name);    
        node->next = NULL;
        
        if (l->head == NULL)
            l->head = node;       // head points to new node
        
        if (l->tail != NULL)
            l->tail->next = node; // tail node points to new node
          
        // tail points to new node
        l->tail = node;           
        

        }

        void clean_up( list_t *l )
        {
        while (l->head != NULL)
        {
        product_data_t *node = l->head;

            // point head to next node in list
            l->head = node->next;
              
            delete node;
            node = NULL;
        }
        

        }

        void main(void)
        {
        list_t l;

        l.head = NULL;
        l.tail = NULL;
        
        add(&l, 10, "Dell Inspiron", 1500);
        add(&l, 11, "Dell Inspirona", 1501);
        add(&l, 12, "Dell Inspironb", 1502);
        add(&l, 13, "Dell Inspironc", 1503);
          
        clean\_up(&l);
        

        }

        "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        S 1 Reply Last reply
        0
        • S steve_rm

          Hello, I am creating a linked list. I have found that the best way to develop the linked list is to have the head and tail in another structure. My products struct will be nested inside this structure. And I should be passing the list to the function for adding and deleting. I find this concept confusing. I have implemented the initialize, add, and clean_up. However, I am not sure that I have done that correctly. When I add a product to the list I declare some memory using calloc. But I am thinking shouldn't I be declaring the memory for the product instead. I am really confused about this adding. Many thanks for any suggestions,

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

          #define PRODUCT_NAME_LEN 128

          typedef struct product_data
          {
          int product_code;
          char product_name[PRODUCT_NAME_LEN];
          int product_cost;
          struct product_data_t *next;
          }product_data_t;

          typedef struct list
          {
          product_data_t *head;
          product_data_t *tail;
          }list_t;

          void add(list_t *list, int code, char name[], int cost);
          void initialize(list_t *list);
          void clean_up(list_t *list);

          int main(void)
          {
          list_t *list = NULL;

          initialize(list);
          add(list, 10, "Dell Inspiron", 1500);
          clean\_up(list);
          
          getchar();
          
          return 0;
          

          }

          void add(list_t *list, int code, char name[], int cost)
          {
          // Allocate memory for the new product
          list = calloc(1, sizeof(list_t));
          if(!list)
          {
          fprintf(stderr, "Cannot allocated memory");
          exit(1);
          }

          if(list)
          {
              // First item to add to the list
              list->head->product\_code = code;
              list->head->product\_cost = cost;
              strncpy(list->head->product\_name, name, sizeof(list->head->product\_name));
              // Terminate the string
              list->head->product\_name\[127\] = '/0';
          } 
          

          }

          // Initialize linked list
          void initialize(list_t *list)
          {
          // Set list node to null
          list = NULL;
          list = NULL;
          }

          // Release all resources
          void clean_up(list_t *list)
          {
          list_t *temp = NULL;

          while(list)
          {
              temp = list->head;
              list->head = list->head->next;
              free(temp);    
          }
          list = NULL;
          list = NULL;
          temp = NULL;
          

          }

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          steve_rm wrote:

          I am creating a linked list.

          Is there some reason you are not using the STL list?

          S 1 Reply Last reply
          0
          • D David Crow

            steve_rm wrote:

            Many thanks for any suggestions,

            Compare what you have to:

            typedef struct product_data
            {
            int product_code;
            char product_name[PRODUCT_NAME_LEN];
            int product_cost;

            product\_data \*next;
            

            } product_data_t;

            typedef struct list
            {
            product_data_t *head;
            product_data_t *tail;
            } list_t;

            // adds to end of list
            void add(list_t *l, int code, char name[], int cost)
            {
            product_data_t *node;

            // create a new node and populate its members
            node = new product\_data\_t;
            node->product\_code = code;
            node->product\_cost = cost;
            strcpy(node->product\_name, name);    
            node->next = NULL;
            
            if (l->head == NULL)
                l->head = node;       // head points to new node
            
            if (l->tail != NULL)
                l->tail->next = node; // tail node points to new node
              
            // tail points to new node
            l->tail = node;           
            

            }

            void clean_up( list_t *l )
            {
            while (l->head != NULL)
            {
            product_data_t *node = l->head;

                // point head to next node in list
                l->head = node->next;
                  
                delete node;
                node = NULL;
            }
            

            }

            void main(void)
            {
            list_t l;

            l.head = NULL;
            l.tail = NULL;
            
            add(&l, 10, "Dell Inspiron", 1500);
            add(&l, 11, "Dell Inspirona", 1501);
            add(&l, 12, "Dell Inspironb", 1502);
            add(&l, 13, "Dell Inspironc", 1503);
              
            clean\_up(&l);
            

            }

            "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            S Offline
            S Offline
            steve_rm
            wrote on last edited by
            #5

            Hello, Thanks for the source. However, I had done it a similar way like that. However, I changed to having the list_t structure as I have been informed that is not good practice to have the head and tail as global and should be contained in a structure. I think this makes it more generic. That was why I was having some problems with my source code. Thanks,

            D 1 Reply Last reply
            0
            • L led mike

              steve_rm wrote:

              I am creating a linked list.

              Is there some reason you are not using the STL list?

              S Offline
              S Offline
              steve_rm
              wrote on last edited by
              #6

              Hello, The reason I am not using the STL list, is I am practicing C programming and wanted to get up to speed on both pointers and lists. Thanks,

              D 1 Reply Last reply
              0
              • S steve_rm

                Hello, Thanks for the source. However, I had done it a similar way like that. However, I changed to having the list_t structure as I have been informed that is not good practice to have the head and tail as global and should be contained in a structure. I think this makes it more generic. That was why I was having some problems with my source code. Thanks,

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                A few small changes will correct that.

                "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                1 Reply Last reply
                0
                • S steve_rm

                  Hello, The reason I am not using the STL list, is I am practicing C programming and wanted to get up to speed on both pointers and lists. Thanks,

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  steve_rm wrote:

                  ...I am practicing C programming and wanted to get up to speed on both pointers and lists.

                  So did you ever get a grasp on this topic?

                  "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  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