queue.cpp 783 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include "queue.h"
  2. int data[SIZE];
  3. queue::queue() {
  4. count = 0;
  5. front = 0;
  6. rear = 0;
  7. for (int i = 0; i < SIZE; ++i)
  8. data[i] = 0.0;
  9. }
  10. bool queue::isEmpty() {
  11. bool result;
  12. if (count == 0)
  13. result = true;
  14. else
  15. result = false;
  16. return result;
  17. }
  18. void queue::enqueue(const int item) {
  19. if (count < SIZE) {
  20. data[rear] = item;
  21. rear = (rear + 1) % SIZE;
  22. ++count;
  23. } else
  24. std::cerr << "Error: Attempt to enqueue to full queue\n";
  25. }
  26. int queue::dequeue() {
  27. int result = 0;
  28. if (!isEmpty()) {
  29. result = data[front];
  30. data[front] = NULL;
  31. front = (front + 1) % SIZE;
  32. --count;
  33. } else
  34. std::cerr << "Error: Attempt to dequeue from an empty queue\n";
  35. return result;
  36. }
  37. int queue::first() {
  38. return data[front];
  39. }
  40. int queue::size() {
  41. return count;
  42. }