Having another problem with this linked list so thought I would post it in the same thread.
Basically I have already found a way around the problem that I am having but do not understand why my solution is different to what I was doing in the first place.
The function I am writing is one to remove a node from a list, the problem is specifically occuring when I try to remove the head by making the next node in the list the head and changing its "prev" pointer to NULL.
These are the structure definitions:
Code:
typedef struct _llnode LLNODE;
struct _llnode {
LLNODE *next;
LLNODE *prev;
int x;
};
typedef struct _llist LLIST;
struct _llist {
LLNODE *head;
LLNODE *tail;
unsigned char cnt;
};
This is what I originally had as my code:
Code:
void llist_remove(LLIST *list, LLNODE *node) {
LLNODE *ptr;
if(node == list->head) {
//Then assign the second node the head status.
list->head = list->head->next;
//Remove the link to the previous head.
list->head->prev = NULL; //WRONG!!!
}
}
But this did not nullify the prev pointer on the new head. (I am not using sentinal nodes and the list is correct in that each node points where it should.)
The code that I am now using that correctly nullifys the prev pointer is:
Code:
void llist_remove(LLIST *list, LLNODE *node) {
LLNODE *ptr;
if(node == list->head) {
//Then assign the second node the head status.
list->head = list->head->next;
//Point ptr at new head.
ptr = list->head;
//Remove the link to the previous head.
ptr->prev = NULL;
}
}
I do not understand the difference between the two above proceedures and although I have got it working in the bottom code snippet I would like to know what I was doing wrong in the first place!
Many thanks!
Pheetuz.