zoukankan      html  css  js  c++  java
  • 带头链表排序

    书上的题目 链表元素排序,一开始准备交换节点,结果没搞好

     1 #include <iostream>
     2 #include <cstdio>
     3 using namespace std;
     4 typedef struct node
     5 {
     6     int date;
     7     struct node *next;
     8 }*linklist,listnode;
     9 void initlist(linklist *head)
    10 {
    11     *head=new listnode;
    12     (*head)->next=NULL;
    13 }
    14 bool isempty(linklist head)
    15 {
    16     if(head->next==NULL)
    17         return 1;
    18     return 0;
    19 }
    20 void input(linklist head,int n)
    21 {
    22     linklist p=head,temp=NULL;
    23     while(n--)
    24     {
    25         temp=new listnode;
    26         cin>>temp->date;
    27         temp->next=NULL;
    28         p->next=temp;
    29         p=p->next;
    30     }
    31 }
    32 void output(linklist head)
    33 {
    34     linklist p=head->next;
    35     while(p!=NULL)
    36     {
    37         cout<<p->date<<' ';
    38         p=p->next;
    39     }
    40     cout<<endl;
    41 }
    42 void _sort1(linklist head)
    43 {
    44     linklist p,q;
    45     int temp,i,j;
    46     cout<<"冒泡排序:
    ";
    47     for(i=0;i<4;i++)
    48     {
    49         p=head->next;
    50         q=p->next;
    51         for(j=0;j<5-i-1;j++)
    52         {
    53             if(p->date>q->date)
    54             {
    55                 temp=q->date;
    56                 q->date=p->date;
    57                 p->date=temp;    
    58             }
    59             p=p->next;
    60             q=q->next;
    61         }
    62     }
    63 }
    64 void _sort2(linklist head)
    65 {
    66     linklist p=head->next,q=p->next;
    67     int temp;
    68     cout<<"选择排序:
    ";
    69     while(p!=NULL)
    70     {
    71         q=p->next;
    72         while(q!=NULL)
    73         {
    74             if(p->date>q->date)
    75             {
    76                 temp=q->date;
    77                 q->date=p->date;
    78                 p->date=temp;
    79             }
    80             q=q->next;
    81         }
    82         p=p->next;
    83     }
    84 }
    85 int main()
    86 {
    87     freopen("in.txt","r",stdin);
    88     linklist head;
    89     initlist(&head);
    90     input(head,5);
    91     _sort1(head);
    92     output(head);
    93     _sort2(head);
    94     output(head);
    95 
    96 }
  • 相关阅读:
    抗变与协变
    border-width实现博客园回复效果
    福利到!分享我的分页控件
    linq distinct 不够用了!
    sql server 使用nolock提升性能
    Graph-Based image segmentation method
    纹理特征提取【转】
    ROC曲线【转】
    LBP纹理特征[转自]
    SPAMS:稀疏建模工具箱
  • 原文地址:https://www.cnblogs.com/a1225234/p/4663894.html
Copyright © 2011-2022 走看看