10_6.cpp
资源名称:c.rar [点击查看]
上传用户:puke2000
上传日期:2022-07-25
资源大小:912k
文件大小:1k
源码类别:

C#编程

开发平台:

Visual C++

  1. //10_6.cpp
  2. #include <iostream.h>
  3. #include <iomanip.h>
  4. struct Student{
  5.   int code;
  6.   char name[20];
  7.   char sex;
  8.   unsigned age;
  9. };
  10. struct Node{
  11.   Student* s;
  12.   Node* next;
  13. };
  14. void Insert(Node*& head, Node*& t);
  15. void Display(const Node*& head);
  16. void main()
  17. {
  18.   Student a[10] ={{8311001, "Smith", 'M', 18},
  19. {8512901, "Kerry", 'F', 19},
  20. {9022101, "Levy", 'M', 16},
  21. {8508020, "Doris", 'F', 20},
  22. {8881232, "Ella", 'F', 18},
  23. {9123001, "Carrie", 'M', 22},
  24. {8100825, "Barbara", 'F', 23},
  25. {9012120, "Carmen", 'M', 20},
  26. {8712001, "Brice", 'M', 19},
  27. {8100923, "Auden", 'M', 20}};
  28.   Node* first=NULL;
  29.   for(int i=0; i<10; i++){
  30.     Node* pN = new Node;
  31.     pN->s = &a[i];
  32.   Insert(first, pN);
  33.   }
  34.   Display(first);
  35. }
  36. void Insert(Node*& head, Node*& t)
  37. {
  38.   if(!head || t->s->code < head->s->code){
  39.     t->next = head;
  40.   head = t;
  41.   return;
  42.   }
  43.   for(Node* p=head; p->next; p=p->next)
  44.     if(t->s->code < p->next->s->code)
  45.     break;
  46.   
  47.   t->next = p->next;
  48.   p->next = t;
  49. }
  50. void Display(const Node*& head)
  51. {
  52.   cout <<setiosflags(ios::left);
  53.   for(const Node* p=head; p; p=p->next)
  54.     cout <<setw(10) <<p->s->code
  55.        <<setw(12) <<p->s->name
  56.      <<setw(8) <<(p->s->sex=='M'?"male":"female")
  57.      <<setw(6) <<p->s->age <<endl;
  58. }