Tuesday, March 8, 2011

Insert a node a sorted linked list

void sortedInsert(Node * head, Node* newNode)
{
Node *current = head;

// traverse the list until you find item bigger the // new node value
//
while (current!= NULL && current->data < newNode->data)
{
current = current->next);
}
//
// insert the new node before the big item
//
newNode->next = current->next;
current = newNode;
}

No comments:

Post a Comment