Contains the source code from the course work throughout my undergraduate Computer Engineering degree at Brigham Young University. There is a mixture of Go, Python, C, C++, Java, VHDL, Verilog, Matlab, Bash, Assembly, etc..
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PriorityQueue.c 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "yakk.h"
  2. #include "PriorityQueue.h"
  3. #include "clib.h"
  4. void initializePriorityQueue(PriorityQueue* queue) {
  5. queue->size = 0;
  6. queue->head = null;
  7. queue->tail = null;
  8. }
  9. void insertPriorityQueue(PriorityQueue* queue, TCB* tcb) {
  10. TCB* temp;
  11. if(tcb == null) return;
  12. //list is empty
  13. YKEnterMutex();
  14. if (queue->size == 0) {
  15. queue->head = tcb;
  16. queue->tail = tcb;
  17. tcb->next = null;
  18. tcb->prev = null;
  19. queue->size = 1;
  20. YKExitMutex();
  21. return;
  22. }
  23. //size of the list = 1
  24. if (queue->size == 1) {
  25. if (queue->head->priority < tcb->priority) {
  26. queue->head->next = tcb;
  27. tcb->prev = queue->head;
  28. tcb->next = null;
  29. queue->tail = tcb;
  30. queue->size++;
  31. YKExitMutex();
  32. return;
  33. } else {
  34. tcb->next = queue->head;
  35. tcb->prev = null;
  36. queue->tail->prev = tcb;
  37. queue->head = tcb;
  38. queue->size++;
  39. YKExitMutex();
  40. return;
  41. }
  42. }
  43. //size of the priorityQueue > 1
  44. temp = queue->head;
  45. while (temp != null) {
  46. if (temp->priority > tcb->priority) {
  47. tcb->next = temp;
  48. tcb->prev = temp->prev;
  49. if (temp == queue->head) {
  50. queue->head = tcb;
  51. } else {
  52. temp->prev->next = tcb;
  53. }
  54. temp->prev = tcb;
  55. queue->size++;
  56. YKExitMutex();
  57. return;
  58. }
  59. temp = temp->next;
  60. }
  61. //tcb has lowest priority
  62. queue->tail->next = tcb;
  63. tcb->prev = queue->tail;
  64. queue->tail = tcb;
  65. tcb->next = null;
  66. queue->size++;
  67. YKExitMutex();
  68. return;
  69. }
  70. TCB* peekPriorityQueue(PriorityQueue* queue) {
  71. return queue->head;
  72. }
  73. TCB* removePriorityQueue(PriorityQueue* queue) {
  74. TCB* retValue;
  75. //Size of the list = 0
  76. YKEnterMutex();
  77. if (queue->size == 0) {
  78. YKExitMutex();
  79. return null;
  80. }
  81. //size of the list = 1
  82. if (queue->size == 1) {
  83. retValue = queue->head;
  84. retValue->next = null;
  85. retValue->prev = null;
  86. queue->size--;
  87. queue->head = null;
  88. queue->tail = null;
  89. YKExitMutex();
  90. return retValue;
  91. }
  92. //size of the list > 1
  93. retValue = queue->head;
  94. queue->head = queue->head->next;
  95. queue->head->prev = null;
  96. queue->size--;
  97. retValue->next = null;
  98. retValue->prev = null;
  99. YKExitMutex();
  100. return retValue;
  101. }
  102. void printPriorityQueue(PriorityQueue* queue) {
  103. TCB* current;
  104. while (current != null) {
  105. printInt(current->priority);
  106. printNewLine();
  107. current = current->next;
  108. }
  109. }