Home > Back-end >  List USES bubble sort from first line solution. (switching nodes)
List USES bubble sort from first line solution. (switching nodes)

Time:03-15

Recently in school requirements management system, use the list, because I am lazy, so sort you want to use the simplest bubble sort, the Internet for a long time, the first line of the data is not involved in the bubble, flashes thought of before the first node to add a node, in order to avoid the head node does not sort of problem, and finally passed to other function, only need to pass to add the node next to go, (welcome bosses animadversion correct)

 void bubble_sort (struct account * head) 
{
Int I, count=0, num.//count records list the number of nodes, num of inner circulation,
Struct account * p, * q, * tail;//create a three pointer, bubble sort
Struct account * add_head=NULL;
Add_head=(struct account *) malloc (LEN);//the three lines up and down the main is to add a head node, make the first line of data involved in the bubbling
Add_head - & gt; Next=head;
P=add_head;
While (p - & gt; Next!=NULL)
{
count++;
P=p - & gt; Next;
}
for(i=0; i {
Num=count - I - 1;
Q=add_head - & gt; Next;//make q point to the first node
P=q - & gt; Next;//after make p points to a node
Tail=add_head;//let the tail always points to a node before the q, convenient exchange, also make the operation and on to the next step
While (num -)
{
If (STRCMP (q - & gt; The name, p - & gt; Name) & gt; 0)
{
Q - & gt; Next=p - & gt; Next;//let q next next to p, skip p
P - & gt; Next=q;//p again next to q
Tail - & gt; Next=p;//the last the head node Pointers p
}
Tail=tail - & gt; Next;
Q=tail - & gt; Next;
P=q - & gt; Next;
}
}

Mem_print (add_head - & gt; Next);//directly next to it is not sweet, waste I've been racking my brains!!!!!!!!!!!!!!!
}

CodePudding user response:

This is why some data structure teaching materials like head nodes rather than the head pointer,
  • Related