fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct Book{
  5. string ten;
  6. int sotrang;
  7. };
  8.  
  9. struct Node{
  10. Book data;
  11. Node *next;
  12. Node(string t, int st){
  13. data.ten = t;
  14. data.sotrang = st;
  15. next = nullptr;
  16. }
  17. };
  18.  
  19. void addB(Node *&head, string t, int st){
  20. Node *newNode = new Node(t, st);
  21. if(head == nullptr){
  22. head = newNode;
  23. return;
  24. }
  25. Node *temp = head;
  26. while(temp->next != nullptr){
  27. temp = temp->next;
  28. }
  29. temp->next = newNode;
  30. }
  31.  
  32. void printB(Node *head){
  33. Node *temp = head;
  34. if(head == nullptr){
  35. cout << "Rong!" << endl;
  36. return;
  37. }
  38. while (temp != nullptr){
  39. cout << temp->data.ten << "\t - \t" << temp->data.sotrang << endl;
  40. temp = temp->next;
  41. }
  42. }
  43.  
  44. int countT(Node *head){
  45. Node *temp = head;
  46. int dem = 0;
  47. while(temp != nullptr){
  48. if(temp->data.sotrang > 500){
  49. dem++;
  50. }
  51. temp = temp->next;
  52. }
  53. return dem;
  54. }
  55.  
  56. void deleteB(Node *&head){
  57. Node *temp = head;
  58. int nho = temp->data.sotrang;
  59. while(temp != nullptr){
  60. if(nho > temp->data.sotrang){
  61. nho = temp->data.sotrang;
  62. }
  63. temp = temp->next;
  64. }
  65. while (head != nullptr && head->data.sotrang == nho){
  66. Node *xoa = head;
  67. head = head->next;
  68. delete xoa;
  69. }
  70. Node *tam = head;
  71. while(tam != nullptr && tam->next != nullptr){
  72. if(tam->next->data.sotrang == nho){
  73. Node *xoa = tam->next;
  74. tam->next = tam->next->next;
  75. delete xoa;
  76. } else {
  77. tam = tam->next;
  78. }
  79. }
  80. }
  81.  
  82. int main(){
  83. Node *head = nullptr;
  84. cout << "-----Danh sach sach-----" << endl;
  85. addB(head, "Doraemon", 200);
  86. addB(head, "Toan Roi Rac", 750);
  87. addB(head, "Doraemon", 200);
  88. addB(head, "Giai tich 1", 200);
  89. addB(head, "Toan Logic", 300);
  90. addB(head, "Tieng Viet", 1750);
  91. printB(head);
  92. cout << endl << "So sach lon hon 500 trang: ";
  93. cout << countT(head);
  94. deleteB(head);
  95. cout << endl << "Sau khi xoa so trang nho nhat!" << endl;
  96. printB(head);
  97. return 0;
  98. }
Success #stdin #stdout 0.01s 5272KB
stdin
Standard input is empty
stdout
-----Danh sach sach-----
Doraemon	 - 	200
Toan Roi Rac	 - 	750
Doraemon	 - 	200
Giai tich 1	 - 	200
Toan Logic	 - 	300
Tieng Viet	 - 	1750

So sach lon hon 500 trang: 2
Sau khi xoa so trang nho nhat!
Toan Roi Rac	 - 	750
Toan Logic	 - 	300
Tieng Viet	 - 	1750