I am trying to sort nodes by their first element and I keep switching different nodes' first elements with other nodes second and third elements.
My goal: 1, 1, 1 -> 2, 2, 2 -> NULL My actual outcome: 1, 2, 2 -> 2, 1, 1-> NULL
I am getting really confused comparing the pointers and making sense of the sort before printing. My display function:
void display()
{
struct node *s, *ptr;
int value;
if (start == NULL)
{
cout<<"Try Again";
}
ptr = head;
cout<<"Elements of list are: ";
while (ptr != NULL)
{
for (s = ptr->next; s !=NULL; s = s->next)
{
if (ptr->x > s->x)
{
value = ptr->x, ptr->y, ptr->z;
ptr->x, ptr->y, ptr->z = s->x, s->y, s->z;
s->x, s->y, s->y = value;
}
cout<< ptr->x <<", "<< ptr->y <<", "<<ptr->z << " -> ";
}
ptr = ptr->next;
}
cout<<"NULL";
}