Tuesday, August 28, 2007

Given only a pointer to a node to be deleted in a singly linked list, how do you delete it?

struct linkedList{
int element;
struct linkedList* next;
};

typedef struct linkedList* List;

void deleteNode(List Node)
{
List tmp;
if(Node)
{ // If current node is not NULL

tmp = List->next; // take backup of next Node
Node->element = Node->next->element; // replace current node element with next Node element
Node->next = Node->next->next; // change next pointer to next to next
free(tmp); // free the next Node which was taken backup

}
return;
}


Powered by ScribeFire.

No comments:

Post a Comment