fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. // ProcessControlBlock
  6. struct PCB
  7. {
  8. int ID;
  9. string name;
  10. int priority;
  11. PCB* next;
  12.  
  13. PCB(int id, string n, int p)
  14. {
  15. ID = id;
  16. name = n;
  17. priority = p;
  18. next = nullptr;
  19. }
  20. };
  21.  
  22. class ProcessList {
  23. private:
  24. PCB* head;
  25.  
  26. public:
  27. ProcessList()
  28. {
  29. head = nullptr;
  30. }
  31.  
  32. // Tao PCB moi va them vao dau danh sach
  33. void addProcess(int ID, const string& name, int priority)
  34. {
  35. PCB* newPCB = new PCB(ID, name, priority);
  36. newPCB->next = head;
  37. head = newPCB;
  38. cout << "Added: ID=" << ID << ", Name=" << name << ", Priority=" << priority << endl;
  39. }
  40.  
  41. // Duyet tim ID tuong ung va loai bo no
  42. void removeProcess(int ID)
  43. {
  44. PCB* current = head;
  45. PCB* prev = nullptr;
  46.  
  47. while (current != nullptr)
  48. {
  49. if (current->ID == ID) {
  50. if (prev == nullptr) {
  51. head = current->next;
  52. } else {
  53. prev->next = current->next;
  54. }
  55. cout << "Removed: ID=" << ID << ", Name=" << current->name << endl;
  56. delete current;
  57. return;
  58. }
  59. prev = current;
  60. current = current->next;
  61. }
  62. cout << "Process with ID=" << ID << " not found." << endl;
  63. }
  64.  
  65. // In thong tin cua cac tien trinh
  66. void displayProcesses()
  67. {
  68. cout << "Process List:" << endl;
  69. PCB* current = head;
  70. while (current != nullptr)
  71. {
  72. cout << "ID=" << current->ID << ", Name=" << current->name << ", Priority=" << current->priority << " -> ";
  73. current = current->next;
  74. }
  75. cout << "nullptr" << endl;
  76. }
  77.  
  78. ~ProcessList()
  79. {
  80. PCB* current = head;
  81. while (current != nullptr) {
  82. PCB* next = current->next;
  83. delete current;
  84. current = next;
  85. }
  86. }
  87. };
  88.  
  89. int main() {
  90. ProcessList readyQueue;
  91.  
  92. readyQueue.addProcess(101, "Editor", 2);
  93. readyQueue.addProcess(102, "Compiler", 1);
  94. readyQueue.addProcess(103, "Browser", 3);
  95.  
  96. readyQueue.displayProcesses();
  97.  
  98. readyQueue.removeProcess(102);
  99. readyQueue.displayProcesses();
  100.  
  101. return 0;
  102. }
Success #stdin #stdout 0.01s 5296KB
stdin
Standard input is empty
stdout
Added: ID=101, Name=Editor, Priority=2
Added: ID=102, Name=Compiler, Priority=1
Added: ID=103, Name=Browser, Priority=3
Process List:
ID=103, Name=Browser, Priority=3 -> ID=102, Name=Compiler, Priority=1 -> ID=101, Name=Editor, Priority=2 -> nullptr
Removed: ID=102, Name=Compiler
Process List:
ID=103, Name=Browser, Priority=3 -> ID=101, Name=Editor, Priority=2 -> nullptr