Advertisement
thinhckhcmus

queue_linklist_sinh vien(bassic)

May 29th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. struct sinhvien
  5. {
  6. int MSSV;
  7. string HoTen;
  8. string lop;
  9. float DTB;
  10. };
  11. struct Node
  12. {
  13. sinhvien Data;
  14. Node *next;
  15. };
  16. struct Queue// dinh nghia cau truc cua queue
  17. {
  18. Node *dau, *cuoi;// Node dau va node cuoi
  19. int count;// dem so phan tu
  20. };
  21. Node *CreateNode(sinhvien x);
  22. void taoQueue(Queue &q);
  23. bool ktQueueRong(Queue q);
  24. void pushQueue(Queue &q, sinhvien x);
  25. int peakQueue(Queue &q, sinhvien &x);
  26. int popQueue(Queue &q, sinhvien &x);
  27. void input(Queue &q, sinhvien &x, int &n);
  28. void output(Queue q, sinhvien x, int n);
  29. Node *CreateNode(sinhvien x)
  30. {
  31. Node *p = new Node;
  32. if (p == NULL)
  33. {
  34. return NULL;
  35. }
  36. else
  37. {
  38. p->Data = x;
  39. p->next = NULL;
  40. }
  41. return p;
  42. }
  43. void taoQueue(Queue &q)// khoi tao queue
  44. {
  45. q.dau = q.cuoi = NULL;
  46. q.count = 0;
  47. }
  48. bool ktQueueRong(Queue q)// kiem tra rong
  49. {
  50. if (q.count == 0)
  51. return true;
  52. else
  53. return false;
  54. }
  55. void pushQueue(Queue &q, sinhvien x)// them phan tu vao queue
  56. {
  57. Node *p = CreateNode(x);
  58. if (ktQueueRong(q))
  59. {
  60. q.dau = q.cuoi = p;
  61. }
  62. else
  63. {
  64. q.cuoi->next = p;
  65. q.cuoi = p;
  66. }
  67. q.count++;
  68. }
  69. int peakQueue(Queue &q,sinhvien &x)// lay phan tu nhung khong xoa
  70. {
  71. if (ktQueueRong(q))
  72. {
  73. cout << "hang doi rong";
  74. return 0;
  75. }
  76. else
  77. {
  78. sinhvien x = q.dau->Data;
  79. cout << "MSSV la: " << x.MSSV << endl;
  80. cout << "ten sinh vien la: " << x.HoTen<< endl;
  81. cout << "lop sinh vien la: " << x.lop << endl;
  82. cout << "DTB cua sinh vien la: " << x.DTB<< endl;
  83. return 1;
  84. }
  85. }
  86. int popQueue(Queue &q,sinhvien &x)// loai bo phan tu dau khoi hang doi
  87. {
  88. if (ktQueueRong(q))
  89. {
  90. cout << "hang doi rong";
  91. return 0;
  92. }
  93. else
  94. {
  95. sinhvien x = q.dau->Data;
  96. if (q.count == 1)//
  97. taoQueue(q);
  98. else
  99. q.dau = q.dau->next;
  100. q.count--;
  101. return 1;
  102. }
  103. }
  104. void input(Queue &q, sinhvien &x, int &n)
  105. {
  106.  
  107. cout << "nhap so luong sinh vien ma ban muon: ";
  108. cin >> n;
  109. for (int i = 0; i < n; i++)
  110. {
  111. cout << "__________________nhap sinh vien thu " << i + 1 << ": " << endl;
  112. rewind(stdin);
  113. cout << "nhap MSSV: ";
  114. cin >> x.MSSV;
  115. rewind(stdin);
  116. cout << "nhap ten sinh vien: ";
  117. getline(cin, x.HoTen);
  118. rewind(stdin);
  119. cout << "nhap lop sinhvien: ";
  120. getline(cin, x.lop);
  121. rewind(stdin);
  122. cout << "nhap DTB cua sinh vien: ";
  123. cin >> x.DTB;
  124. pushQueue(q, x);
  125. }
  126. }
  127. void output(Queue q,sinhvien x,int n)
  128. {
  129. Node *p = q.dau;
  130. while (p != NULL)
  131. {
  132. cout << "MSSV la: " << p->Data.MSSV << endl;
  133. cout << "ten sinh vien la: " << p->Data.HoTen << endl;
  134. cout << "lop sinh vien la: " << p->Data.lop << endl;
  135. cout << "DTB cua sinh vien la: " << p->Data.DTB << endl;
  136. p = p->next;
  137. }
  138. cout << "\n";
  139. }
  140. //void sapxeptangdan(Queue &q)// chua fix duoc :(((
  141. {
  142. Node *a = q.dau;
  143. Node *b = q.dau->next;
  144. do
  145. {
  146. do
  147. {
  148. if (a->Data.MSSV > b->Data.MSSV)
  149. {
  150. int MSSV;
  151. string HoTen;
  152. string lop;
  153. float DTB;
  154. MSSV = a->Data.MSSV;
  155. b->Data.MSSV = MSSV;
  156. HoTen = a->Data.HoTen;
  157. b->Data.HoTen = HoTen;
  158. MSSV = a->Data.MSSV;
  159. b->Data.MSSV = MSSV;
  160. }
  161. } while (b->next == NULL);
  162. } while (a->next->next == NULL);
  163. }//
  164. int main()
  165. {
  166. sinhvien x, y;
  167. int n;
  168. Queue q;
  169. taoQueue(q);
  170. input(q, x, n);
  171. output(q, x,n);
  172. int lua_chon;
  173. cout << "Moi Ban Chon Phep Toan Voi DS LKD";
  174. cout << "\n1: Kiem Tra Queue Rong";
  175. cout << "\n2: Them Phan Tu Vao Queue";
  176. cout << "\n3: Lay Gia Tri Dau Queue";
  177. cout << "\n4: lay va xoa sinh vien";
  178. cout << "\n5: Xuat Queue";
  179. cout << "\n6: Thoat";
  180. do
  181. {
  182. cout << "\nBan Chon: ";
  183. cin >> lua_chon;
  184. switch (lua_chon)
  185. {
  186. case 1:
  187. {
  188. if (ktQueueRong(q))
  189. cout << "Queue rong";
  190. else
  191. cout << "Queue Khong Rong";
  192. break;
  193. }
  194. case 2:
  195. {
  196. cout << "nhap phan tu can chen vao Queue: ";
  197. rewind(stdin);
  198. cout << "nhap MSSV: ";
  199. cin >> y.MSSV;
  200. rewind(stdin);
  201. cout << "nhap ten sinh vien: ";
  202. getline(cin, y.HoTen);
  203. rewind(stdin);
  204. cout << "nhap lop sinhvien: ";
  205. getline(cin, y.lop);
  206. rewind(stdin);
  207. cout << "nhap DTB cua sinh vien: ";
  208. cin >> y.DTB;
  209. pushQueue(q, y);
  210. break;
  211. }
  212. case 3:
  213. {
  214. peakQueue(q, x);
  215. break;
  216. }
  217. case 4:
  218. {
  219. popQueue(q, x);
  220. break;
  221. }
  222. case 5:
  223. {
  224. output(q, x, n);
  225. break;
  226. }
  227. }
  228. } while (lua_chon != 6);
  229. return 0;
  230. system("pause");
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement