Advertisement
thienlang

LinkedListStudent

Mar 5th, 2014
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. struct Student
  7. {
  8.     char _hoTen[30];
  9.     int _tuoi;
  10. };
  11.  
  12. // hàm nhập thông tin sinh viên
  13. //trả về thông tin sinh viên
  14. Student EnterStudent()
  15. {
  16.     Student sv;
  17.  
  18.     fflush(stdin);
  19.     cout << "Nhap ten sinh vien: ";
  20.     gets_s(sv._hoTen);
  21.  
  22.     fflush(stdin);
  23.  
  24.     cout << "Nhap tuoi: ";
  25.     cin >> sv._tuoi;
  26.  
  27.     return sv;
  28. }
  29.  
  30. //cấu trúc 1 node là 1 sinh viên
  31. //và đường dẫn tới sinh viên tiếp theo
  32. struct NODE
  33. {
  34.     Student _sinhVien;
  35.     NODE* pNext;
  36.  
  37.     //hàm khởi tạo ban đầu cho NODE
  38.     //khi vừa khai báo NODE thì pNext = NULL;
  39.     NODE()
  40.     {
  41.         pNext = NULL;
  42.     }
  43. };
  44.  
  45. //Hàm tạo 1 NDOE có dữ liệu là thông tin 1 sv.
  46. NODE* CreateNode( Student sv )
  47. {
  48.     NODE * Data = new NODE;
  49.     if  ( Data == NULL )
  50.         return NULL;
  51.     Data->_sinhVien = sv;
  52.     Data ->pNext = NULL;
  53.  
  54.     return Data;
  55. }
  56.  
  57. //Cấu trúc lưu trữ danh sách sinh viên.
  58. struct ListStudent
  59. {
  60.     NODE* _sinhVienDau;
  61.     NODE* _sinhVienCuoi;
  62.  
  63.     //đây là hàm khởi tạo ban đầu cho list
  64.     //tức là khi vừa khai báo list thì có svdau,svcuoi = NULL
  65.     ListStudent()
  66.     {
  67.         _sinhVienDau = NULL;
  68.         _sinhVienCuoi = NULL;
  69.     }
  70. };
  71.  
  72. //Hàm thêm 1 sinh viên vào ListStudent;
  73. bool AddTailList (ListStudent & List, Student sv )
  74. {
  75.     NODE* Data = CreateNode(sv);
  76.     if ( Data == NULL )
  77.         return false; //hết bộ nhớ thêm vào thất bại
  78.  
  79.     //list chưa có sinh viên nào
  80.     //thêm NODE Data có thông tin của sv vừa tạo vào
  81.     // và nó chính là thằng đầu cũng là thằng cuối
  82.     if ( List._sinhVienCuoi == NULL)
  83.     {
  84.         List._sinhVienDau = Data;
  85.         List._sinhVienCuoi = Data;
  86.         return true; //thêm vào thành công
  87.     }
  88.  
  89.     List._sinhVienCuoi->pNext = Data;//thêm nó vào làm thằng cuối cùng
  90.     List._sinhVienCuoi = Data;
  91.     return true; //thêm thành công
  92. }
  93.  
  94.  
  95. void PrintList(ListStudent List)
  96. {
  97.     NODE *pNode = List._sinhVienDau;
  98.     cout << endl;
  99.     int i = 1;
  100.     while (pNode != NULL)
  101.     {
  102.         cout << "Sinh Vien thu " << i++ << endl;
  103.         cout << "\tTen: " << pNode->_sinhVien._hoTen << endl;
  104.         cout << "\tTuoi: " << pNode->_sinhVien._tuoi << endl;
  105.  
  106.         pNode = pNode->pNext;//di chuyển tới sv tiếp theo
  107.  
  108.     }
  109. }
  110.  
  111. void main()
  112. {
  113.     Student sv;
  114.     ListStudent DanhSachSinhVien;
  115.     for ( int i = 0; i < 3 ; i++)
  116.     {
  117.         sv = EnterStudent();//khai báo và bắt nhập thông tin sinh viên
  118.  
  119.         //thêm sinh viên vào danh sách
  120.         if (!AddTailList(DanhSachSinhVien,sv))
  121.         {
  122.             cout <<endl<< "Thieu Bo Nho";
  123.             break;
  124.         }
  125.     }
  126.  
  127.     PrintList(DanhSachSinhVien);//xuất danh sách
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement