Double Link List help
hello,
i was wondering if anyone knows how to delete a node from a double link list. here is the struct..
struct node {
int num;
struct node *previous;
struct node *next;
};
i need it to work in a function like this...
void delFromList(sruct node *list, int data);
thank you in advance.
-Jay Miesionczek
"where is your savior now?"
-uncreativpresidenthalosoft, inc.halosoft.hypermart.net
Assuming that the node pointed to by list is a sentinal node, It would look something like:
void delFromList(struct node * list, int data) { struct node * prev; struct node * next; struct node * ptr = list->next; while (ptr != list && ptr->num != data) { ptr = ptr->next; } if (ptr == list) return; prev = ptr->previous; next = ptr->next; prev->next = next; next->previous = prev; ptr->previous = NULL; ptr->next = NULL; free(ptr);}
Ok. Basically you would want to do this:
PNODE pTemp;
while (List)
{
if (List->Item == Num)
break;
List = List->pNext;
}
if (!List) return NULL; // Either passed NULL or couldn''t find it.
pTemp = List->pPrev;
if (pTemp)
pTemp->pNext = List->pNext;
pTemp = List->pNext;
if (pTemp)
pTemp->pPrev = List->pPrev;
return List;
Now, your structure is a little bit off here. First of all, this will usually blow up if you find the first item in the list (the head), since you''ll effectively be removing the head of the list, causing future deletes to screw up (since you don''t know where to start!). I would come up with a structure more like:
struct tagLIST_HEAD
{
tagNODE
*pHead;
};
and passing that into the function. Then, you can modify the head node and effectively update your list when you run into this case.
Pythius
PNODE pTemp;
while (List)
{
if (List->Item == Num)
break;
List = List->pNext;
}
if (!List) return NULL; // Either passed NULL or couldn''t find it.
pTemp = List->pPrev;
if (pTemp)
pTemp->pNext = List->pNext;
pTemp = List->pNext;
if (pTemp)
pTemp->pPrev = List->pPrev;
return List;
Now, your structure is a little bit off here. First of all, this will usually blow up if you find the first item in the list (the head), since you''ll effectively be removing the head of the list, causing future deletes to screw up (since you don''t know where to start!). I would come up with a structure more like:
struct tagLIST_HEAD
{
tagNODE
*pHead;
};
and passing that into the function. Then, you can modify the head node and effectively update your list when you run into this case.
Pythius
"The object of war is not to die for your country, but to make the other bastard die for his"
Yeah, double linked-lists are simpler than single ones for deletion. Schematically, your general case looks like this:
To remove y, you just want this:
So it is just a case of making x''s next pointer point to z, and z''s previous pointer point to x. Y is now removed.
This is a lot easier if you use some sort of circular list, as then you don''t have to worry about the special cases (ie. where y is the start or the end of the list).
x <--> y <--> z
To remove y, you just want this:
x <---------> z
So it is just a case of making x''s next pointer point to z, and z''s previous pointer point to x. Y is now removed.
This is a lot easier if you use some sort of circular list, as then you don''t have to worry about the special cases (ie. where y is the start or the end of the list).
One question : why do you need to implement a doubly-linked list ? Everything is in the STL, you should take advantage of it. It makes programs faster (through cache-coherence), more robust (less debugging), more readable and easier to write.
Be reading you,
David
Be reading you,
David
to everyone: thank you. it works as expected.
altman: it''s an assignment for my data structures class.
-Jay Miesionczek
"where is your savior now?"
altman: it''s an assignment for my data structures class.
-Jay Miesionczek
"where is your savior now?"
-uncreativpresidenthalosoft, inc.halosoft.hypermart.net
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement