LInked Lists
HERE''S A GENERIC SETUP SO I CAN ASK MY QUESTION:
Please ignore the fact that lists are uninitialized, that''s not part of my problem or question.
struct data{
int x;
struct data *next;
}
Head; //pointers to struct type data//
first;
last;
head = first; //address of first stored in head?//
head->next = first; //address of first held in next?//
head->next = first->next; //what''s the difference between all three of these? And when do I want each of them?//
THANKS, THIS WILL ALLOW ME TO MOVE ON IN MY BEGINNING BOOK.
Hi,
That looks rather circular to me!
head = first; //now head points to first
head->next = first; //next in head(first) points to the same first, thus it points to ITSELF
head->next = first->next; //nothing is changed as head points to same first
I hope this is right and simplifies a bit
Newbie
That looks rather circular to me!
head = first; //now head points to first
head->next = first; //next in head(first) points to the same first, thus it points to ITSELF
head->next = first->next; //nothing is changed as head points to same first
I hope this is right and simplifies a bit
Newbie
/Mankind gave birth to God.
when I work out linked list logic I always end up drawing myself diagrams, just get a big peice of paper
One person said that the games industry is "a transfer of funds from the rich to the lucky"
One person said that the games industry is "a transfer of funds from the rich to the lucky"
Just because the church was wrong doesn't mean Galileo wasn't a heretic.It just means he was a heretic who was right.
aye, diagrams are good
also, when using linked lists i always use the approach of creating a struct to represent a node ( cotains data field and pointer to next node ) and a list struct with a pointer to the head.
if you''re going for doubly linked list, just add a previous pointer to the node struct, and an end pointer to the list struct.
hope that at least helped some
also, when using linked lists i always use the approach of creating a struct to represent a node ( cotains data field and pointer to next node ) and a list struct with a pointer to the head.
if you''re going for doubly linked list, just add a previous pointer to the node struct, and an end pointer to the list struct.
hope that at least helped some
Helps a little, but not my specific question:
ADDING AN ELEMENT TO THE BEGINNING OF A LIST
1. new = (person *)malloc(sizeof(struct person));
2. new->next = head;
3. head = new;
/* Wouldn't line #2 be new->next = head->next? */
/* Because it's in the next pointer that NULL is held, not
/* the struct name (head)? The book says it's doing this to
/* assign the value of head to new->next, but the value of
/* head should be in head->next, just like NULL for
/* new goes in new->next??
/* By the way, does a head pointer have the same attributes
/* as the rest of the linked list, so it's the 'first' one?
/* or is the next one the first one?
thanks a lot
Edited by - boltthrower on 3/20/00 1:00:46 AM
ADDING AN ELEMENT TO THE BEGINNING OF A LIST
1. new = (person *)malloc(sizeof(struct person));
2. new->next = head;
3. head = new;
/* Wouldn't line #2 be new->next = head->next? */
/* Because it's in the next pointer that NULL is held, not
/* the struct name (head)? The book says it's doing this to
/* assign the value of head to new->next, but the value of
/* head should be in head->next, just like NULL for
/* new goes in new->next??
/* By the way, does a head pointer have the same attributes
/* as the rest of the linked list, so it's the 'first' one?
/* or is the next one the first one?
thanks a lot
Edited by - boltthrower on 3/20/00 1:00:46 AM
Linked Lists can be circular in nature so it could be possible that for a single node list that new->next = head. Note though that this changes your traversal method because now you need to store a pointer to head->value and traverse the list until you arrive again at head->value. Circular linked lists bring other needs as well and if your interested I or someone else can elaborate.
For your question above, I am assuming that it is a straight linked list and yes new->next should point to NULL or head->next during the first insert of a node. Head->next wil not be null on the second and subsequent insertions. It is best to draw a diagram to help you out.
As far as head and any node on the list being different, they are not. Head keeps track of the first node. You will need that node when you traverse the list. See the following for loop for an example.
The for loop sets a iterator pointer of the same struct to be equal to the first node. The for loop will then traverse the list incrementing tmp by setting it equal to tmp->next until tmp is NULL. This is why you need head. If you use head as your iterator you will lose the list and leak memory in your application. I hope this helps.
Derek
For your question above, I am assuming that it is a straight linked list and yes new->next should point to NULL or head->next during the first insert of a node. Head->next wil not be null on the second and subsequent insertions. It is best to draw a diagram to help you out.
As far as head and any node on the list being different, they are not. Head keeps track of the first node. You will need that node when you traverse the list. See the following for loop for an example.
struct LIST_NODE *head, *tmp...Some insertions, ... on the list...for (tmp = head; !tmp; tmp = tmp->next);
The for loop sets a iterator pointer of the same struct to be equal to the first node. The for loop will then traverse the list incrementing tmp by setting it equal to tmp->next until tmp is NULL. This is why you need head. If you use head as your iterator you will lose the list and leak memory in your application. I hope this helps.
Derek
Derek Licciardi (Kressilac)Elysian Productions Inc.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement