tungggg

Quan ly sinh vien _ LinkedList

Mar 27th, 2022
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.80 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct sinhvien {
  5.     string name ;
  6.     int age ;
  7.     double gpa ;
  8. };
  9.  
  10. struct Node {
  11.     sinhvien s ;
  12.     Node* next ;
  13. };
  14.  
  15. #define node Node*
  16.  
  17. int size (node head){
  18.     int ans =0 ;
  19.     while (head!= NULL ){
  20.         ans++ ;
  21.         head= head-> next ;
  22.     }
  23.     return ans ;
  24. }
  25.  
  26.  
  27. node makeNode (){
  28.     sinhvien s ;
  29.     cin.ignore();
  30.     cout<<"Nhap ten sinh vien "<<endl;
  31.     getline ( cin ,s.name );
  32.    
  33.     cout<<"Nhap tuoi : "<<endl;
  34.     cin >> s.age;
  35.     cout<<"Nhap gpa : "<<endl;
  36.     cin >> s.gpa;
  37.     node tmp = NULL;
  38.     tmp = new Node ();
  39.     tmp->s=s ;
  40.     tmp->next= NULL;
  41.     return tmp ;
  42. }
  43.  
  44. void addFirst (node & head  ){
  45.     node tmp = makeNode();
  46.     if ( head == NULL ){
  47.         head= tmp ;
  48.         return ;
  49.     }
  50.     tmp->next = head;
  51.     head= tmp;
  52.    
  53. }
  54.  
  55. void addLast (node& head){
  56.     node tmp = makeNode();
  57.     if ( head== NULL){
  58.         head= tmp;
  59.         return ;
  60.     }
  61.     node p= head ;
  62.     while ( p->next != NULL  ){
  63.         p=p->next ;
  64.        
  65.     }
  66.     p->next = tmp;
  67. }
  68.  
  69. void addMiddle ( node& head , int pos ){
  70.     int n= size(head);
  71.  
  72.     if (head == NULL){
  73.         node tmp = makeNode();
  74.         head=tmp;
  75.         return ;
  76.     }
  77.     if ( pos <=0 || pos > n+1   ){
  78.         return ;
  79.     }
  80.     else if ( pos == 1 ){
  81.         addFirst (head);
  82.        
  83.     }
  84.     else if (pos== n+1 ){
  85.         addLast(head);
  86.     }
  87.     else {
  88.         node tmp = makeNode();
  89.         node p= head;
  90.         for (int i=1;i<pos-1;i++){
  91.             p=p->next;
  92.         }
  93.         tmp->next = p->next ;
  94.         p-> next = tmp;
  95.     }
  96. }
  97.  
  98.  
  99. void inSV(sinhvien s ){
  100.     cout<<"--------------------------"<<endl;
  101.     cout<< "Ho ten sinh vien "<<s.name <<endl;
  102.     cout<<"Tuoi sinh vien : "<< s.age <<endl;
  103.     cout<<"Gpa : "<<s.gpa <<endl;
  104.     cout<<"--------------------------"<<endl;
  105. }
  106.  
  107.  
  108. void inDSLK(node head ){
  109.     while ( head!= NULL ){
  110.         // insv
  111.         inSV(head->s);
  112.         head=head->next ;
  113.     }
  114. }
  115.  
  116.  
  117. void removeFirst (node & head){
  118.     head= head->next ;
  119. }
  120. void removeLast( node & head)
  121. {
  122.     node p = head ;
  123.     node truoc  = p;
  124.     while ( p->next != NULL ){
  125.         truoc  = p;
  126.         p=p->next ;
  127.     }
  128.     truoc->next = NULL;
  129. }
  130.  
  131. void removeMiddle (node & head, int pos ){
  132.     int n= size(head);
  133.     if ( head == NULL) return ;
  134.     if ( pos == 1 ) {
  135.         removeFirst(head);
  136.     }
  137.     else if ( pos == n ) {
  138.         removeLast(head);
  139.     }
  140.     else {
  141.         node p = head;
  142.         for (int i=1;i<pos-1;i++){
  143.             p=p->next ;
  144.         }
  145.         p->next= (p->next)->next ;
  146.     }
  147. }
  148.  
  149. void sapXep (node & head ){
  150.     for (node p= head;p->next!=NULL;p=p->next ){
  151.         node q= p;
  152.         for (node m = q ; m->next!=NULL;m=m->next ){
  153.             if ( q->s.gpa > m->s.gpa   ){
  154.                 q=m;
  155.             }
  156.         }
  157.         sinhvien tmp  = p->s ;
  158.         p->s=q->s;
  159.         q->s= tmp;
  160.        
  161.     }
  162. }
  163.  
  164. int main(){
  165.     node head= NULL;
  166.     // dslk ban dau la rong (null)
  167.     while ( 1 ){
  168.         for (int i=1;i<=50;i++){
  169.             cout<<"                                           "<<endl;
  170.         }
  171.         cout<<"---------------------------------"<<endl;
  172.         cout <<"1.Them SV vao dau danh sach "<<endl;
  173.         cout<<"2.Them SV vao cuoi danh sach "<<endl;
  174.         cout<<"3.Them SV vao giua danh sach "<<endl;
  175.         cout<<"4.Xoa SV o dau danh sach "<<endl;
  176.         cout<<"5.Xoa SV o cuoi danh sach "<<endl;
  177.         cout<<"6.Xoa SV o giua danh sach "<<endl;
  178.         cout<<"7.In DSLK "<<endl;
  179.         cout<<"8.Thoat "<<endl;
  180.         cout<<"9.Sap xep theo GPA "<<endl;
  181.         cout<<"Nhap lua chon "<<endl;
  182.        
  183.         cout<<"---------------------------------"<<endl;
  184.        
  185.         int lc ;
  186.         cin >> lc ;
  187.        
  188.         if ( lc== 1) {
  189.             addFirst (head);
  190.         }
  191.         else if (lc == 2 ){
  192.             addLast(head);
  193.         }
  194.         else if ( lc== 3 ){
  195.             int pos ;
  196.             cout<<"Nhap vi tri can them "<<endl;
  197.             cin.ignore();
  198.             cin >> pos ;
  199.             addMiddle(head,pos);
  200.         }
  201.         else if ( lc == 4 ){
  202.                  removeFirst ( head) ;
  203.         }
  204.         else if ( lc == 5 ){
  205.             removeLast(head);  
  206.         }
  207.         else if ( lc==6 ){
  208.             int pos ;
  209.             cout<<"Nhap vi tri can xoa "<<endl;
  210.             cin >> pos ;
  211.             removeMiddle(head,pos );
  212.         }
  213.         else if ( lc==7) {
  214.             inDSLK(head);
  215.         }
  216.         else if ( lc == 8 ){
  217.             break;
  218.         }
  219.         else if ( lc == 9){
  220.             sapXep(head);
  221.         }
  222.     }
  223.    
  224.     return 0;
  225. }
Advertisement
Add Comment
Please, Sign In to add comment