I'm trying to make sorted linked list in ascending order , but for some reason the code give me one value if some one can tell me why I will be thankful see this function:
void AddNode(Node *&head){
int temp;
Node *q;
Node *prev = 0;
Node *t;
cout <<"enter a number : ";
cin >> temp;
t = new Node ;
t->number = temp;
t->next = 0;
if (head == 0) {
head = t;
} else {
q = head;
while (q != 0 ){
prev = q;
if(q->number > t->number){
prev->next = t;
t->next = q;
break;
}
q = q->next; // counter
}
if (q == 0 ) // if it is the last value
q = t;
}
}
headwhen the new node is inserted in front. Also,q = tat the end doesn't do anything useful - you are updating a local variable but aren't using this new value.