zoukankan      html  css  js  c++  java
  • c++链表

      1 #include <iostream.h>
      2 
      3 struct fbplayer
      4 {
      5     int age;//年龄
      6     char name[20];//姓名
      7     int num;//编号
      8     float score;//综合评分
      9     fbplayer *next;
     10 };
     11 
     12 
     13 
     14 fbplayer *Input(int n)//输入
     15 {
     16     fbplayer *head  ;
     17     fbplayer *p1,*p2;
     18     int i;
     19     head= NULL;
     20 
     21     p2 = new fbplayer;
     22     for(i=1;i<=n;i++)
     23     {
     24         p1= new fbplayer;
     25         cout<<"年龄、姓名、编号、综合评分"<<endl;
     26         cin>>p1->age;
     27         cin>>p1->name;
     28         cin>>p1->num;
     29         cin>>p1->score;
     30         cout<<endl;
     31         if(i==1)head = p1;
     32         else p2->next = p1;
     33         p2 = p1;
     34     }
     35     p2->next = NULL;
     36     return head;
     37 }
     38 void  Output(fbplayer *head)//输出
     39 {
     40     fbplayer *p;
     41     p = head;
     42     while(p!=NULL)
     43     {
     44         cout<<"年龄:"<<p->age<<",姓名:"<<p->name<<",编号:"<<p->num<<",综合评分:"<<p->score<<endl;
     45         p = p->next;
     46     }
     47     cout<<endl;
     48 }
     49 
     50 void  Destroy(fbplayer *head)//销毁
     51 {
     52     fbplayer * p=head;
     53     fbplayer * t;
     54      while (p)
     55      {
     56           t=p;
     57           p=p->next;
     58           delete t ;
     59      }
     60 }
     61 
     62 fbplayer *sort(fbplayer *head)//冒泡排序
     63 {
     64     fbplayer *p,*p1,*p2,*p3;
     65     fbplayer h, t;
     66     if (head == NULL) return NULL;
     67     h.next=head;
     68     p=&h;
     69     while (p->next!=NULL)
     70     {
     71         p=p->next;
     72     }
     73     p=p->next=&t;
     74     while (p!=h.next)
     75     {
     76         p3=&h;
     77         p1=p3->next;
     78         p2=p1->next;
     79         while (p2!=p) 
     80         {
     81             if ((p1->score)>(p2->score))
     82             {
     83                 p1->next=p2->next;
     84                 p2->next=p1;
     85                 p3->next=p2;
     86 
     87                 p3=p2; 
     88                 p2=p1->next;
     89 
     90             } else {
     91                 p3=p1;
     92                 p1=p2;
     93                 p2=p2->next;
     94             }
     95         }
     96         p=p1;
     97     }
     98     while (p->next!=&t)
     99     {
    100         p=p->next;
    101     }
    102     p->next=NULL;
    103     return h.next;
    104 }
    105 
    106 fbplayer *Insert(fbplayer *head)//插入
    107 {
    108     fbplayer *q=NULL;
    109     fbplayer *p=NULL;
    110     fbplayer *s=NULL;
    111     q=head;//头结点
    112     p=q;
    113 
    114     while(p!=NULL)//找到尾指针
    115     {
    116         q=p;
    117         p=q->next;
    118     }
    119     s=new fbplayer;
    120         
    121     cout<<"请输入要插入的年龄、姓名、编号、综合评分"<<endl;
    122     cin>>s->age;
    123     cin>>s->name;
    124     cin>>s->num;
    125     cin>>s->score;
    126     q->next=s;
    127     s->next=p;
    128     return head;
    129 }
    130 
    131 fbplayer *Delete(fbplayer *head)//删除
    132 {
    133     fbplayer* q = head;
    134     fbplayer* p = 0;
    135  
    136     int  x;
    137     cout<<"请输入要删除球员的编号:";
    138     cin>>x;
    139  
    140     while (q)
    141     {
    142         if (q->num==x)
    143         {
    144             if (!p)
    145             {
    146                 q = q->next;
    147                 delete head;
    148                 head = q;
    149             }
    150             else
    151             {
    152                 p->next = q->next;
    153                 delete q;
    154                 q = p->next;
    155             }
    156         }
    157         else
    158         {
    159             p = q;
    160             q = q->next;
    161         }
    162     }
    163  
    164     return head;
    165 }
    166 
    167 
    168 fbplayer *Find(fbplayer *head)//按编号查找
    169 {
    170     fbplayer *q;
    171     q=head;
    172     int x;
    173     cout<<"请输入要查找球员的编号:";
    174     cin>>x;
    175     int flags = 1;
    176     while(q!=NULL){
    177         if(q->num ==x)
    178         {
    179             return q;
    180             flags = 0;
    181             break;
    182         }
    183         q = q->next;
    184     }
    185     if(flags = 1)q=0;
    186     return q;
    187     
    188 }
    189 void main()
    190 {
    191     
    192     int n;
    193     cout<<"请输入球员数目:";
    194     cin>>n;
    195     fbplayer *head = new fbplayer;
    196     head = Input(n);
    197     head = Insert(head);
    198     Output(head);
    199     head = sort(head);
    200     Output(head);
    201     cout<<Find(head)<<endl;
    202     Output(head);
    203     Destroy(head);
    204 }
  • 相关阅读:
    每日日报31
    每日日报30
    每日日报29
    《精益软件开发管理之道》阅读笔记03
    每日日报28
    ip地址访问vue项目
    eslint prettier vetur eslint
    线上环境去除console
    vue 多环境打包
    2申请高德地图key 初始化地图
  • 原文地址:https://www.cnblogs.com/minmsy/p/5279354.html
Copyright © 2011-2022 走看看