单向链表排序(冒泡)

struct   student  
{
  int   num;
  struct   student   *next;
}

struct   student   *paixu(struct   student   *head)
{
      struct   student   *p,*q;
      int   temp;
      q=head;
      while(q->next!=null)
      {
          p=head;
          while(p->next!=null)             //这个判断有冗余,待改进
          {      
            if   (p->num   >   p->next->num)       //交换数值
              {
                  temp=p->num;
                  p->num=p->next->num;
                  p->next->num=temp;
                }
              p=p->next;
            }  
          q=q->next;
        }
      return   *head;  
}

2   void  arrange(NODE *head)   /*   按照单词出现频率排序函数   */
{
 
  NODE *p1,*p2,*p3;
  p1=head->next; 
  p2=NULL;
  p3=head;        
  while(p2!=head->next->next)   /* p2作为尾指针控制p1循环,采用下沉法排序,从head->next开始排序*/     
  {
  while(p1->next!=p2)        
  {
  if (p1->countnext->count)
  {       p3->next=p1->next;
              p1->next=p1->next->next;
          p3->next->next=p1;
  }

  p3=p1;          /* P3为P1的前驱  */
  p1=p1->next;

 
  }
  p2=p1;    /* 尾指针上移一位  */
  p1=head->next;   /* P1,P3指向初始位置  */
  p3=head;
  } 

你可能感兴趣的:(数据结构)