链表的比较排序算法

来源:互联网 发布:淘宝网首页说明文字 编辑:程序博客网 时间:2024/06/07 13:49

//这个程序是我昨天写的,其实,是写了两个,还有一个链表的选择排序,但目前,先把这个放在这里,大家可以参考一下.(这个程序是在VC6.0中测试并已通过)

struct employee* Sortdata(struct employee* head) //是对结构体中的数据排序,

                                                                                                  //返回的是链表的头指针.
{
employee *p,*q,*r,*s;
r=new employee;
r->next=head;

for(s=head;s->next;s=s->next)
{
   p=s; q=p->next;
   while(q)
   {
    if(p->data>q->data)
    {
     if(p==head)
     {
      p->next=q->next;
      q->next=p;
      head=q;
      q=p->next;
     }
     else
     {
      r->next=q;
      p->next=q->next;
      q->next=p;
      r=q;q=p->next;
     }
    }
       else
    {
     r=r->next;
     p=p->next;
     q=q->next;
    }
    
   }
   r=s;
}
return head;
}

//我的QQ空间 感谢大家提意见.

 


 
原创粉丝点击