how do i insert a given item into list without destroying non decreasing order using a if else statement and node ? c++
Sol.
According to the question non decreasing order are two types.
1. It might be increasing order. // Ex. 1 2 3 4 5 6
2. it might be niether increasing nor decresing order //Ex. 2 2 2 2 2 2
So, if we want to prevent the given order then we need to insert node at specific location using while loop
psuedo code.
head = node; // node id pointing to the head of list
while(node!==NULL){
node = node->next;
if the value of item is greater then current node and next node then node = node->next
if the value of item is greater then current node and less then next node then insert item here.
make new node
new node ->next = current node -> next
current node->next = new node
}
Overcome: if we want to reach at specific location in a link list then we need a loop it is neccesary. So, only if else statement caan not be usefull in every case of the above problem.
Get Answers For Free
Most questions answered within 1 hours.