Friday 14 August 2015

Insert a node in a sorted linked list. Write a C/C++ program.


Solutions:

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