I'm trying to bubble sort a linked list with one pointer, p_previous. The pointer is supposed to look ahead one node and also look ahead two nodes, and if the first is greater than the second, they are to be switched using a temporary variable while p_previous stays out of the swap. p_previous should also check down two nodes to see if the list trailer is there, stopping the sort. I honestly have no clue what I am doing when it comes to bubble sorts, and the linked list implementation isn't helping.
Here is some code:
int sort_list(ID_NUMBER *p_list, int list_count)
{
ID_NUMBER *p_previous = p_list; /* Previous node, use to swap next */
ID_NUMBER *p_temp; /* Temporary variable */
int count; /* Counts number of nodes passed */
for(count = 0; count < list_count; count++)
{
while(p_previous->p_next_student->p_next_student != NULL)
{
if(p_previous->p_next_student->student_id >
p_previous->p_next_student->p_next_student->student_id)
{
p_temp = p_previous->p_next_student->p_next_student;
p_previous->p_next_student = p_temp->p_next_student;
p_temp = p_previous->p_next_student;
p_previous->p_next_student = p_temp;
}
p_previous = p_previous->p_next_student;
}
}
return 0;
}
Here is what I know.
If this is my list as entered.
H-->1-->3-->2-->4-->T
1 and 3 are already in order, move p_previous down.
3 and 2 are out of order, make the temp variable point to 2.
Make 3 point to the number 4.
Make 2 point to the number 3.
Make 1 point to the number 2.
I think thats how I'm supposed to do it, I just don't know how to put it into code.
I am pretty sure a while loop inside a for loop inside is all that is necessary.
If someone could help, that would be great.
Also, if you need more information, just ask.
ifstatement in main that checks for that by getting the length of the linked list from another function.