Advertisement
Loesome

Untitled

Apr 7th, 2014
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. #ifndef QUEUE_H
  2. #define QUEUE_H
  3. #include"Link.h"
  4. #include<iostream>
  5.  
  6. using namespace std;
  7.  
  8. template <typename E> class Queue{
  9. private:
  10. Link<E>* head;
  11. Link<E>* tail;
  12. int size;
  13. public:
  14. Queue() {
  15. head = tail = new Link<E>();
  16. size = 0;
  17. }
  18. ~Queue() {
  19. clear();
  20. delete head;
  21. }
  22.  
  23. void clear() {
  24. while(head->next != 0) {
  25. dequeue();
  26. }
  27. tail = head;
  28. size = 0;
  29. }
  30. void enqueue(const E& it) {
  31. tail->next = new Link<E>(it, 0);
  32. tail = tail->next;
  33. size++;
  34. }
  35. E dequeue() {
  36. if(size != 0) {
  37. E it = head->next->element;
  38.  
  39. Link<E>* aux = head->next;
  40. head->next = aux->next;
  41. if (tail == aux){
  42. tail = head;
  43. }
  44.  
  45. delete aux;
  46. size --;
  47. return it;
  48. }
  49. else{
  50. return 0;
  51. }
  52. }
  53. const E& headValue() const {
  54. if(size != 0) {
  55. return head->next->element;
  56. }
  57. else{
  58. return 0;
  59. }
  60. }
  61. int length() const {
  62. return size;
  63. }
  64. };
  65.  
  66.  
  67. #endif // QUEUE_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement