and there is an error in my code as i changed the variable from head to head tmp and forgot to update the call to malloc
and there is an error in my code as i changed the variable from head to head tmp and forgot to update the call to malloc
and there is an error in my code as i changed the variable from head to head tmp and forgot to update the call to malloc
all i want to do is have the function return a pointer to a chunk of memory that is the size of node and assign it to a pointer called head. just like laserlight did in her code. just like every example i have seen dones
Ok. here's an interesting way to do it with polymorphism... Say you have a list_T type defined as:
And your note_T type defined as:Code:// structure to keep track of the list. typedef struct list_s { node_T *head; node_T *tail; } list_T;
If all your elements on the list is defined as:Code:// generic node. typedef struct node_s { struct node_s *next; } node_T;
You can deal with your list like this:Code:struct element_s { node_T *next; // this pointer MUST be here always; // your data (3D vetor coordinates as example)... double x, y, z; };
Of course you'll need to allocate your new element somewhere. Example of use:Code:// Used to initialize your empty list. #define EMPTY_LIST { NULL, NULL } void addhead( list_T *lstp, void *elemp ) { node_T *p = elemp; p->next = lstp->head; lstp->head = p; // our element will be the new head. // if the list is previously empty, we have a new tail. if ( ! lstp->tail ) lstp->tail = p; }
Code:list_T list = EMPTY_LIST; struct element_s *elemp; // allocate element: elemp = malloc( sizeof *elemp ); // fill the data elemp->x = 1.0f; elemp->y = 0.0f; elemp->z = sqrt(2.0); // insert element in the list: addhead( &list, elemp );
ok this is getting beyond a joke now
compiles no errors no warnings.Code:#include <stdio.h> #include <stdlib.h> typedef struct Node { int age; struct Node *next; }Node; typedef struct LinkedList { struct Node *head; struct Node *tail; }LinkedList; struct LinkedList *create_head(void); int main() { //declare my list LinkedList list; Node data; //set both head and tail pointers to null list.head = NULL; list.tail = NULL; list.head = malloc(sizeof(data)); if (!list.head) { printf("error allocating memory\n"); return -1; } list.tail = list.head; return 0; }
yet all i have done is the same code (literally i cut and pasted it) in a function
producesCode:#include <stdio.h> #include <stdlib.h> typedef struct Node { int age; struct Node *next; }Node; typedef struct LinkedList { struct Node *head; struct Node *tail; }LinkedList; struct LinkedList *create_head(Node data); int main() { //declare my list LinkedList list; Node data; //set both head and tail pointers to null list.head = NULL; list.tail = NULL; list.head = create_head(data); list.tail = list.head; return 0; } struct LinkedList *create_head(Node data) { LinkedList *head_tmp; head_tmp = malloc(sizeof(data)); if (!head_tmp) { printf("error allocating memory\n"); } return *head_tmp; }
so the way i read all that is it doesn't see the return statement even though it says the return statement is an error, the error on line 46 is because i passed it a linked list and it wanted a linked list. and s to the warning on line 28 god alone knowsCode:linked list simple/main.c|28|warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]| linked list simple/main.c||In function ‘create_head’:| linked list simple/main.c|46|error: incompatible types when returning type ‘LinkedList {aka struct LinkedList}’ but ‘struct LinkedList linked list simple/main.c|47|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build failed: 1 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|
ok i have tried again and managed to change everything so it compiles with no warnings or errors. so i went ahead and added a print and delete list functions
however when i run it get seg fault on line 37.Code:#include <stdio.h> #include <stdlib.h> typedef struct Node { int age; struct Node *next; }Node; typedef struct LinkedList { struct Node *head; struct Node *tail; }LinkedList; struct LinkedList *initalize_list(LinkedList *list); struct Node *create_head(void); struct Node *get_memory(void); int get_age(void); void print_list(LinkedList list); void delete_list(LinkedList *list); int main() { //declare my list LinkedList *list = NULL; list = initalize_list(list); print_list(*list); delete_list(list); return 0; } struct LinkedList *initalize_list(LinkedList *list) { list->head = NULL; list->tail = NULL; list->head = create_head(); list->head->next = NULL; list->tail = list->head; list->head->age = get_age(); return list; } struct Node *create_head(void) { Node *head_tmp; head_tmp = get_memory(); if (!head_tmp) { fprintf(stderr, "error allocating memory\n"); exit(EXIT_FAILURE);// if it cant allocate memory for the head node at startup somthing really wrong so bug out } return head_tmp; } struct Node *get_memory(void) { return malloc(sizeof(Node)); } int get_age(void) { int age; printf("PLease enter an age: "); scanf(" %d", &age); return age; } void print_list(LinkedList list) { int count = 1; Node *tmp = list.head; while (tmp) { printf("person%d's age is %d\n", count, list.head->age); count++; tmp = list.head->next; } } void delete_list(LinkedList *list) { int count = 1; Node *tmp; printf("freeing memory.....\n"); while (list->head) { tmp = list->head->next; printf("freeing element %d\n", count); free(list->head); list->head = tmp; count++; } printf("memory freed\n"); }
coop
ok after hours of searching and re-watching videos i just don't get what i have done wrong. it just will not let me set head to anything be it a null or a pointer derived from malloc so either there is something really wrong or this struct idea holding a pointer to the head and tail just doesn't work.
the whole thing has been an exercise in futility i just don't understand this at all.
Have a closer look at what create_head is returning - Hint: It's not the same as list.head
its returning the pointer from malloc
list.head is a struct Node *
create_head returns a struct _ *
or more precisely it returns head_tmp which is of type node and set to the pointer returned from malloc
and as list_head is of type node there is no miss match if i have it return a type of linked list it whines like a moped with its throttle stuck open
however when i run it get seg fault on line 37.You create a list pointer and assign NULL to that pointer, then pass that NULL pointer into your initialize_list() function, next you try to assign NULL to a field of a NULL pointer object, result CRASH. You need to assign memory to that pointer before you try to use it.Code:int main() { //declare my list LinkedList *list = NULL; list = initalize_list(list); ... struct LinkedList *initalize_list(LinkedList *list) { list->head = NULL; ...
it crashes when i try to assign memory as well
however i didn't realise that linkedlist *list = null is the same as linked list *list ...... list->head = null list->tail = null