Hello, I am trying to delete an entry from a doubly linked list, where the function takes a pointer to the entry to be removed as its argument. I have tried various different things, and I can get the insertEntry function to work, but I am at a loss for what to put for the remove Entry.
I can get it if I put that it will receive the entry before the entry to be removed as the argument but not when it receives the entry to be removed as the argument. Can you guys help me? This is not homework, I am learning on my own. Here is the code I have so far.
Code:
/* 6. Develop insertEntry and removeEntry functions for a doubly linked list that are
similar in function to those developed in previous exercises for a singly linked list.
Why can your removeEntry function now take as its argument a direct pointer to
the entry to be removed from the list?
*/
#include <stdio.h>
struct entry
{
int value;
struct entry *next;
struct entry *previous;
};
struct entry n2, n3, n4, insert;
int main (void)
{
void insertEntry (struct entry *here, struct entry *insert);
void removeEntry (struct entry *remove);
struct entry *list_pointer = &n2;
n2.value = 200;
n2.next = &n3;
n2.previous = (struct entry *) 0;
n3.value = 300;
n3.next = &n4;
n3.previous = &n2;
n4.value = 400;
n4.next = (struct entry *) 0;
n4.previous = &n3;
insert.value = 250;
while ( list_pointer != (struct entry *) 0 )
{
printf ("%i\n", list_pointer->value);
list_pointer = list_pointer->next;
}
list_pointer = &n2;
insertEntry(&n2, &insert);
removeEntry(&n4);
while ( list_pointer != (struct entry *) 0 )
{
printf ("%i\n", list_pointer->value);
list_pointer = list_pointer->next;
}
}
void insertEntry (struct entry *here, struct entry *insert)
{
insert->next = here->next;
here->next = insert;
}
void removeEntry (struct entry *remove)
{
remove->previous = remove->next;
}