Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <stdexcept> // exception
  4.  
  5. using std::cout;
  6. using std::endl;
  7. using std::logic_error;
  8. using std::string;
  9.  
  10. class QueueException : public logic_error
  11. {
  12. public:
  13. QueueException(const string& message = "")
  14. : logic_error(message.c_str())
  15. {}
  16. };
  17.  
  18. template <typename T>
  19. class MyQueue // Naive Linear
  20. {
  21. private:
  22. static const int MAX_SIZE = 8;
  23. T data[MAX_SIZE];
  24. int head; // index of next item to be removed
  25. int tail; // index of the first free slot
  26.  
  27. public:
  28. MyQueue() // constructor
  29. {
  30. head = 0;
  31. tail = 0;
  32. }
  33.  
  34. ~MyQueue() // destructor
  35. {
  36. clear();
  37. }
  38.  
  39. T front() const throw(QueueException)
  40. {
  41. if (empty())
  42. throw QueueException("Queue is Empty");
  43. return data[head];
  44. }
  45.  
  46. T back() const throw(QueueException)
  47. {
  48. if (empty())
  49. throw QueueException("Queue is Empty");
  50. return data[tail-1];
  51. }
  52.  
  53. void push(T item) throw(QueueException)
  54. {
  55. if(size() == MAX_SIZE || tail == MAX_SIZE)
  56. throw QueueException("Queue Overflow!");
  57. data[tail++] = item;
  58. }
  59.  
  60. void pop() throw(QueueException)
  61. {
  62. if(empty())
  63. throw QueueException("Queue is Empty");
  64. ++head;
  65. }
  66.  
  67. bool empty() const
  68. {
  69. return (head == tail);
  70. }
  71.  
  72. int size() const
  73. {
  74. return (tail - head);
  75. }
  76.  
  77. void clear()
  78. {
  79. head = tail = 0;
  80. }
  81. };
  82.  
  83.  
  84. int main(){
  85. MyQueue<char> school;
  86. cout << school.empty() << endl; // prints 1 (true)
  87. //cout << school.front() << endl; // Queue Empty Exception
  88. //cout << school.back() << endl; // Queue Empty Exception
  89. //school.pop(); // what(): Queue Underflow
  90. school.push('M');
  91. school.push('-');
  92. school.push('U');
  93. school.push('S');
  94. school.push('L');
  95. school.push('D');
  96. cout << school.empty() << endl; // prints 0 (false)
  97. cout << school.front() << endl; // prints 'M'
  98. cout << school.back() << endl; // prints 'D'
  99. cout << school.size() << endl; // prints 6
  100.  
  101. school.pop();
  102. school.pop();
  103.  
  104. school.push('X');
  105. cout << school.back() << endl; // prints 'X'
  106. school.push('Y');
  107. cout << school.back() << endl; // prints 'Y'
  108. school.push('Z'); // Queue Overflow
  109.  
  110.  
  111. while( !school.empty() )
  112. {
  113. cout << school.front();
  114. school.pop();
  115. } // prints "M-USLD"
  116. cout << endl;
  117. cout << school.size() << endl; // prints 0
  118. return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement