9
3

queue.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef QUEUE_H
  17. #define QUEUE_H
  18. template<class T>
  19. class MyQueue;
  20. template<class T>
  21. class MyQueueNode
  22. {
  23. public:
  24. MyQueueNode(T* data)
  25. {
  26. next = 0;
  27. this->data = data;
  28. }
  29. friend class MyQueue<T>;
  30. private:
  31. T* data;
  32. MyQueueNode<T>* next;
  33. };
  34. template<class T>
  35. class MyQueue
  36. {
  37. public:
  38. MyQueue()
  39. {
  40. head = tail = 0;
  41. }
  42. ~MyQueue() {
  43. clear();
  44. }
  45. void push(T* data)
  46. {
  47. if (head == 0)
  48. {
  49. tail = head = new MyQueueNode<T>(data);
  50. }
  51. else
  52. {
  53. tail->next = new MyQueueNode<T>(data);
  54. tail = tail->next;
  55. }
  56. }
  57. T* pop()
  58. {
  59. if (head == 0)
  60. {
  61. return 0;
  62. }
  63. T* data = head->data;
  64. MyQueueNode<T>* next_node = head->next;
  65. delete head;
  66. head = next_node;
  67. return data;
  68. }
  69. T* top()
  70. {
  71. if (head == 0)
  72. {
  73. return 0;
  74. }
  75. return head->data;
  76. }
  77. bool empty()
  78. {
  79. if (head == 0)
  80. {
  81. return true;
  82. }
  83. return false;
  84. }
  85. void clear()
  86. {
  87. T* d = 0;
  88. while((d = pop())) {
  89. delete d;
  90. }
  91. return;
  92. }
  93. int count()
  94. {
  95. int count = 0;
  96. MyQueueNode<T>* d = head;
  97. while(d != 0) {
  98. count++;
  99. d = d->next;
  100. }
  101. return(count);
  102. }
  103. private:
  104. MyQueueNode<T>* head;
  105. MyQueueNode<T>* tail;
  106. };
  107. #endif