This fuction of insert in descending order is not working.Can't find the problem.Can anyone help?
class LinkedList
{
private:
class ListNode
{
public:
int data;
ListNode *next;
ListNode(int d)
{
data=d;
next = NULL;
}
};
ListNode *head;
public:
LinkedList()
{
head=NULL;
}
void Inorder(int d)
{
ListNode *tmp = head;
ListNode *m = head;
if(head == NULL)
{
head= new ListNode(d); // if the first node is to be inserted
}
else
{
if(d > head->data)
{
ListNode *x=new ListNode(d); //if the entered node is greater than the first place it before the first.
x->next = head;
head=x;
/*delete x;*/
}
}
while(tmp != NULL)
{
if(d >tmp->next->data)
{
tmp->next=new ListNode(d);//if the node inserted is smaller than the first but larger than some value in between
tmp->next->next=m->next;
}
tmp=tmp->next;
m=m->next;
}
tmp=new ListNode (d); //if its least of all then append at the end;
}