PhieuLang

DSLK Don

Apr 20th, 2012
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.40 KB | None | 0 0
  1. // LinkList.cpp
  2. #include<iostream>
  3. #include<cstdlib>
  4. #include<string>
  5. using namespace std;
  6.  
  7. /*Cau truc thong tin chung ve sinh vien*/
  8. typedef struct
  9. {
  10.    string hoten;
  11.    int msv;
  12. }sinhvien;
  13.  
  14. /*Cau truc cua mot node trong danh sach lien ket don*/
  15. typedef struct tagNode
  16. {
  17.     sinhvien info;
  18.     struct tagNode* pNext;
  19. }Node;
  20.  
  21. /*================================= */
  22. typedef struct tagList
  23. {
  24.     Node *pHead;
  25.     Node *pTail;
  26. }LIST;
  27.  
  28. /*===========Check data=======*/
  29. int checkdata(sinhvien a,sinhvien b)
  30. {
  31.     if((a.hoten==b.hoten)&&(a.msv==b.msv)) return 1;
  32.     return 0;
  33. }
  34. /*==================Khoi dong danh sach lien ket don=============== */
  35. void CreateList(LIST &l)
  36. {
  37.     l.pHead=NULL;
  38.     l.pTail=NULL;
  39. }
  40.  
  41. /*============Cap phat mien nho cho mot node cua danh sach lien ket don===========*/
  42. Node* CreateNode(sinhvien x)
  43. {
  44.     Node *p;
  45.     p=new Node;
  46.     if(p==NULL) return NULL;
  47.     p->info=x;
  48.     p->pNext=NULL;
  49.     return p;
  50. }
  51. /*==================Them mot node vao dau danh sach lien ket don============*/
  52. void AddHead(LIST &l,Node *p)
  53. {
  54.     if(l.pHead==NULL)
  55.     {
  56.         l.pHead=p;
  57.         l.pTail=l.pHead;
  58.     }
  59.     else
  60.     {
  61.         p->pNext=l.pHead;
  62.         l.pHead=p;
  63.     }
  64. }
  65. /*==================Them mot node vao cuoi danh sach lien ket don============*/
  66. void AddTail(LIST &l,Node *p)
  67. {
  68.     if(l.pHead==NULL)
  69.     {
  70.         l.pHead=p;
  71.         l.pTail=l.pHead;
  72.     }
  73.     else
  74.     {
  75.         l.pTail->pNext=p;
  76.         l.pTail=p;
  77.     }
  78. }
  79. /*==================Them mot node vao sau mot node trong danh sach lien ket don============*/
  80. void AddAfterQ(LIST &l,Node *p,Node *q)
  81. {
  82.     if(q!=NULL)
  83.     {
  84.         q->pNext=p->pNext;
  85.         q->pNext=p;
  86.         if(l.pTail==q)
  87.             l.pTail=p;
  88.     }
  89.     else
  90.     {
  91.         AddHead(l,p);
  92.     }
  93. }
  94. /*==================Huy node dau tien trong DSLK============*/
  95. int RemoveHead(LIST &l,sinhvien &x)
  96. {
  97.     Node *p;
  98.     if(l.pHead!=NULL)
  99.     {
  100.         p=l.pHead;
  101.         x=p->info;
  102.         l.pHead=l.pHead->pNext;
  103.         delete p;
  104.         if(l.pHead==NULL)
  105.             l.pTail=NULL;
  106.         return 1;
  107.     }
  108.     return 0;
  109. }
  110. /*==================Huy node sau node q trong DSLK============*/
  111. int RemoveAfterQ(LIST &l,Node *q, sinhvien &x)
  112. {
  113.     Node *p;
  114.     if(q!=NULL)
  115.     {
  116.         p=q->pNext;
  117.         if(p!=NULL)
  118.         {
  119.             if(p==l.pTail)
  120.                 l.pTail=q;
  121.             q->pNext=p->pNext;
  122.             x=p->info;
  123.             delete p;
  124.         }
  125.         return 1;
  126.     }
  127.     else
  128.         return 0;
  129. }
  130. /*==================Huy node co khoa X trong DSLK============*/
  131. int RemoveByKey(LIST &l,sinhvien key)
  132. {
  133.     Node *p,*q=NULL;
  134.     p=l.pHead;
  135.     while((p!=NULL)&&(!checkdata(p->info,key)))
  136.     {
  137.         q=p;
  138.         p=p->pNext;
  139.     }
  140.     if(p==NULL)
  141.     {
  142.         return 0;
  143.     }
  144.     if(q!=NULL)
  145.     {
  146.         RemoveAfterQ(l,q,key);
  147.     }
  148.     else
  149.     {
  150.         RemoveHead(l,key);
  151.     }
  152.             return 1;
  153. }
  154. /*============Tim kiem node chua Key============*/
  155. Node* Search(LIST &l,sinhvien key)
  156. {
  157.     Node *p;
  158.     p=l.pHead;
  159.     while((p!=NULL)&&(!checkdata(p->info,key)))
  160.     {
  161.         p=p->pNext;
  162.     }
  163.     return p;
  164. }
  165. /*===========Huy danh sach==============*/
  166. void RemoveList(LIST &l)
  167. {
  168.     Node *p;
  169.     while(l.pHead!=NULL)
  170.     {
  171.         p=l.pHead;
  172.         l.pHead=l.pHead->pNext;
  173.         delete p;
  174.     }
  175. }
  176. /*============Duyet danh sach=========*/
  177. int PrintList(LIST l)
  178. {
  179.     Node *p;
  180.     if(l.pHead==NULL) return 0;
  181.     int index=1;
  182.     p=l.pHead;
  183.     while(p!=NULL)
  184.     {
  185.         cout<<"\t\tSinh vien thu "<<index++<<":"<<endl;
  186.         cout<<"Ho va ten: "<<p->info.hoten<<endl;
  187.         cout<<"Ma sinh vien: "<<p->info.msv<<endl<<endl;
  188.         p=p->pNext;
  189.     }
  190.     return 1;
  191. }
  192.     /*===============Sap xep danh sach=========*/
  193. /*====Theo ten====*/
  194. void SortByName(LIST &l)
  195. {
  196.     Node *p,*q,*min;
  197.     string temp;
  198.     int tmp;
  199.     p=l.pHead;
  200.     while(p!=l.pTail)
  201.     {
  202.         min=p;
  203.         q=p->pNext;
  204.         while(q!=NULL)
  205.         {
  206.             if(q->info.hoten<min->info.hoten)
  207.                 min=q;
  208.             q=q->pNext;
  209.         }
  210.         tmp = min->info.msv;
  211.         temp = min->info.hoten;
  212.         min->info.msv = p->info.msv;
  213.         min->info.hoten = p->info.hoten;
  214.         p->info.msv = tmp;
  215.         p->info.hoten = temp;
  216.         p=p->pNext;
  217.     }
  218. }
  219. /*====Theo ma sinh vien====*/
  220. void SortByMSV(LIST &l)
  221. {
  222.     int tmp;
  223.     string temp;
  224.     Node *p,*q,*min;
  225.     p=l.pHead;
  226.     while(p!=l.pTail)
  227.     {
  228.         min=p;
  229.         q=p->pNext;
  230.         while(q!=NULL)
  231.         {
  232.             if(q->info.msv<min->info.msv)
  233.                 min=q;
  234.             q=q->pNext;
  235.         }
  236.         tmp = min->info.msv;
  237.         temp = min->info.hoten;
  238.         min->info.msv = p->info.msv;
  239.         min->info.hoten = p->info.hoten;
  240.         p->info.msv = tmp;
  241.         p->info.hoten = temp;
  242.         p=p->pNext;
  243.     }
  244. }
  245. /*====Theo ten====*/
  246. int QuickSortByName(LIST &l)
  247. {
  248.     LIST l1,l2;
  249.     Node *p,*x;
  250.     if(l.pHead==l.pTail) return 0;
  251.     CreateList(l1);
  252.     CreateList(l2);
  253.     x=l.pHead;
  254.     l.pHead=l.pHead->pNext;
  255.     while(l.pHead!=NULL)
  256.     {
  257.         p=l.pHead;
  258.         l.pHead=p->pNext;
  259.         p->pNext=NULL;
  260.         if(p->info.hoten<=x->info.hoten)
  261.             AddTail(l1,p);
  262.         else
  263.             AddTail(l2,p);
  264.     }
  265.     QuickSortByName(l1);
  266.     QuickSortByName(l2);
  267.     if(l1.pHead!=NULL)
  268.     {
  269.         l.pHead=l1.pHead;
  270.         l1.pTail->pNext=x;
  271.     }
  272.     else
  273.         l.pHead=x;
  274.     x->pNext=l2.pHead;
  275.     if(l2.pHead!=NULL)
  276.         l.pTail=l2.pTail;
  277.     else
  278.         l.pTail=x;
  279. }
  280. /*====Theo ma sinh vien====*/
  281. int QuickSortByMSV(LIST &l)
  282. {
  283.     LIST l1,l2;
  284.     Node *p,*x;
  285.     if(l.pHead==l.pTail) return 0;
  286.     CreateList(l1);
  287.     CreateList(l2);
  288.     x=l.pHead;
  289.     l.pHead=l.pHead->pNext;
  290.     while(l.pHead!=NULL)
  291.     {
  292.         p=l.pHead;
  293.         l.pHead=p->pNext;
  294.         p->pNext=NULL;
  295.         if(p->info.msv<=x->info.msv)
  296.             AddTail(l1,p);
  297.         else
  298.             AddTail(l2,p);
  299.     }
  300.     QuickSortByName(l1);
  301.     QuickSortByName(l2);
  302.     if(l1.pHead!=NULL)
  303.     {
  304.         l.pHead=l1.pHead;
  305.         l1.pTail->pNext=x;
  306.     }
  307.     else
  308.         l.pHead=x;
  309.     x->pNext=l2.pHead;
  310.     if(l2.pHead!=NULL)
  311.         l.pTail=l2.pTail;
  312.     else
  313.         l.pTail=x;
  314. }
  315.  
  316. int main()
  317. {
  318.     LIST l;
  319.     sinhvien sv;
  320.     Node *q;
  321.     int select;
  322.     CreateList(l);
  323.     do
  324.     {
  325.         cout<<"\n\t THAO TAC VOI DANH SACH SINH VIEN ";
  326.         cout<<"\n\t------------------------------------";
  327.         cout<<"\n\t 1.Them sinh vien vao dau danh sach ";
  328.         cout<<"\n\t 2.Them sinh vien vao cuoi danh sach ";
  329.         cout<<"\n\t 3.Huy sinh vien dau danh sach ";
  330.         cout<<"\n\t 4.Huy sinh vien trong danh sach ";
  331.         cout<<"\n\t 5.Tim sinh vien ";
  332.         cout<<"\n\t 6.Duyet danh sach";
  333.         cout<<"\n\t 7.Sap xep danh sach theo ho ten";
  334.         cout<<"\n\t 8.Sap xep danh sach theo ma sinh vien";
  335.         cout<<"\n\t 9.QuickSort theo ho ten";
  336.         cout<<"\n\t 10.QuickSort theo ma sinh vien";
  337.         cout<<"\n\t 11.Xoa danh sach";
  338.         cout<<"\n\t 0.Thoat ";
  339.         cout<<"\n\n\t #chon (1->n or 0 de thoat): ";
  340.         cin>>select;
  341.         cin.ignore();
  342.         switch(select)
  343.         {
  344.             case 1:
  345.                 cout<<"\nHo ten sinh vien: ";
  346.                 getline(cin,sv.hoten);
  347.                 cout<<"Ma sinh vien: ";
  348.                 cin>>sv.msv;
  349.                 q=CreateNode(sv);
  350.                 if(q!=NULL) AddHead(l,q);
  351.                 break;
  352.             case 2:
  353.                 cout<<"\nHo ten sinh vien: ";
  354.                 getline(cin,sv.hoten);
  355.                 cout<<"Ma sinh vien: ";
  356.                 cin>>sv.msv;
  357.                 q=CreateNode(sv);
  358.                 if(q!=NULL) AddTail(l,q);
  359.                 break;
  360.             case 3:
  361.                 if(RemoveHead(l,sv))
  362.                 {
  363.                     cout<<"\nHo ten sinh vien da huy: "<<sv.hoten<<endl;
  364.                     cout<<"Ma sinh vien da huy: "<<sv.msv<<endl;
  365.                 }
  366.                 else
  367.                     cout<<"Danh sach rong"<<endl;
  368.                 break;
  369.             case 4:
  370.                 cout<<"\nHo ten sinh vien: ";
  371.                 getline(cin,sv.hoten);
  372.                 cout<<"Ma sinh vien: ";
  373.                 cin>>sv.msv;
  374.                 if(!RemoveByKey(l,sv))   cout<<"Khong tim thay sinh vien nay"<<endl;
  375.                 else    cout<<"Xoa thanh cong"<<endl;
  376.                 break;
  377.             case 5:
  378.                 cout<<"\nHo ten sinh vien: ";
  379.                 getline(cin,sv.hoten);
  380.                 cout<<"Ma sinh vien: ";
  381.                 cin>>sv.msv;
  382.                 if(Search(l,sv)==NULL)   cout<<"\nKet qua: Khong tim thay sinh vien nay"<<endl;
  383.                 else    cout<<"\nKet qua: Tim thay sinh vien trong danh sach"<<endl;
  384.                 break;
  385.             case 6:
  386.                 if(!PrintList(l)) cout<<"Danh sach rong"<<endl;
  387.                 break;
  388.             case 7:
  389.                 SortByName(l);
  390.                 break;
  391.             case 8:
  392.                 SortByMSV(l);
  393.                 break;
  394.             case 9:
  395.                 QuickSortByName(l);
  396.                 break;
  397.             case 10:
  398.                 QuickSortByMSV(l);
  399.                 break;
  400.             case 11:
  401.                 RemoveList(l);
  402.                 break;
  403.         }
  404.    // system("pause");
  405.     }
  406.     while(select!=0);
  407. }
Advertisement
Add Comment
Please, Sign In to add comment