I need C linked lists help.....

Pelleas

Veteran XX
int insert(Field *list, Field *f){
Field *p = list;
Field *c = list;

f->next = NULL;
p->next = f;

}

F is the node i want to insert, list is the list i want to insert it too. Im having endless troubles doing this, i can do it with global variables, but not with pointers to the list. If someone could help i really need it
 
1) why do you have an int function with no return?
2) I think (been a while) if you have a pointer to a pointer, you need two stars (so p and c should both have TWO stars in front)
3) I think your last two commands are out of order, but I'm not sure how. And I've been working on a very crappy, weak implementation of C called Interactive C a good part of the day. And my brain is tired. And I don't have my books handy right now.
 
dosent seem to mind that it has no return, but will make it void now anyways
well p and c work with one star
i have no idea what will work or what isnt, i have tried every combination of the last 2 lines and i cant get it to work...i am going insane
 
Eh, if I remember, I'll dig out Kerrigan and Ritchy later, and drag it to school with me and look up linked lists. I've forgotten them for tonight.

Speaking of, have a good one. I'm going to crash.
 
I take it your inserting at the head of the list?

Code:
 int insert(Field *list, Field *f){
   f->next = list;
   list = f;
 
  }
 
Helado said:
I take it your inserting at the head of the list?

Code:
 int insert(Field *list, Field *f){
   f->next = list;
   list = f;
 
  }

yes, but i think i found part of the problem, im suppose to be getting a pointer of a pointer to the list im modifing outside the function...so its (Field **list, Field *f) instead. Still im not sure how to access the information in the pointer of a pointer properly.
 
1) You're inserting it wrong. If list always points to the first node of your ll, then you're not adding it to the end.. you're adding it after the first node (i.e. always as a second node, replacing whatever is there). What you need to do is iterate through the list until you reached the end and only then add the new node (this is assuming list always points to the first node, which it usually does in linked lists):


Code:
/* get the first node */
Field *endOfList = list;

/* jump through the nodes until endOfList is pointing to the last node */
while (endOfList->next != NULL) 
   endOfList = endOfList->next

/* add our new node after this last node */
endOfList->next = f;

/* make sure that f is not pointing anywhere */
f->next = NULL;

2) I'm guessing you're calling the function wrong... post that too.
 
It should be noted that the above code could break the integrity of the list if you have a pointer to the middle of a linked list.
 
Pavi said:
1) You're inserting it wrong. If list always points to the first node of your ll, then you're not adding it to the end.. you're adding it after the first node (i.e. always as a second node, replacing whatever is there). What you need to do is iterate through the list until you reached the end and only then add the new node (this is assuming list always points to the first node, which it usually does in linked lists):


Code:
/* get the first node */
Field *endOfList = list;

/* jump through the nodes until endOfList is pointing to the last node */
while (endOfList->next != NULL) 
   endOfList = endOfList->next

/* add our new node after this last node */
endOfList->next = f;

/* make sure that f is not pointing anywhere */
f->next = NULL;

2) I'm guessing you're calling the function wrong... post that too.


insert(&sx->label_list,new_node); is how im calling it
sx is a structore containing a list...im trying ti modify the list within that structure.
 
Pelleas said:
insert(&sx->label_list,new_node); is how im calling it
sx is a structore containing a list...im trying ti modify the list within that structure.

if it's not a pointer to a struct then just do &sx.label_list, assuming that the label_list member is a Field*.
 
Pelleas said:
yes, but i think i found part of the problem, im suppose to be getting a pointer of a pointer to the list im modifing outside the function...so its (Field **list, Field *f) instead. Still im not sure how to access the information in the pointer of a pointer properly.

maybe this? I'm fuzzy on dereferencing a double pointer.
Code:
 int insert(Field **list, Field *f){
   f->next = *list;
   *list = f;
 
  }
 
alright, finally got it working thanks alot guys, it was a combination of double pointers, and your insert algorithims, thanks again.
 
Back
Top