creating a linked list
-
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;
}
-
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;
}
Me think you don't need the
list
structure, you only need theproduct_data
structure. in your code, you only need a pointer to the first element in the list.This signature was proudly tested on animals.
-
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;
}
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
-
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;
}
-
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
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,
-
steve_rm wrote:
I am creating a linked list.
Is there some reason you are not using the STL list?
-
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,
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
-
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,
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