Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. // A Queue class based on a DLL implementation.
  2.  
  3. // Work in this implementation is delegated to a DLList object.
  4.  
  5. // A queue can be implemented as a linked list by adding to one end and deleting from
  6. // the other end. For example, AddToTail + DeleteHead or AddToHead + DeleteTail.
  7. // DeleteTail should be avoided with singly linked lists.
  8.  
  9. #include "Header.h"
  10.  
  11. template <class T>
  12. class QueueDLL
  13. {
  14. public:
  15. QueueDLL();
  16. ~QueueDLL();
  17.  
  18. void Enqueue(T& val); // Adds an element to the queue.
  19. T Dequeue(void); // Returns and removes from the queue the first
  20. // element added to the queue.
  21.  
  22. T GetFirst(void); // Returns the first element added to the queue.
  23. T GetLast(void); // Returns the last element added to the queue.
  24. bool IsEmpty(void);
  25. void Clear(void);
  26.  
  27. private:
  28. DLList<T> list;
  29. };
  30.  
  31.  
  32. // Constructor.
  33. // Automatically calls the constructor of the DLList.
  34. template <class T>
  35. QueueDLL<T>::QueueDLL(void)
  36. {}
  37.  
  38. // Destructor.
  39. // Automatically calls the destructor of the DLList.
  40. template <class T>
  41. QueueDLL<T>::~QueueDLL(void)
  42. {}
  43.  
  44. template <class T>
  45. void QueueDLL<T>::Enqueue(T& el)
  46. {
  47. list.AddToTail(el);
  48. }
  49.  
  50. template <class T>
  51. T QueueDLL<T>::Dequeue()
  52. {
  53. T el = list.GetHead();
  54. list.DeleteFromHead();
  55. return el;
  56. }
  57.  
  58. template <class T>
  59. T QueueDLL<T>::GetFirst()
  60. {
  61. return list.GetHead();
  62. }
  63.  
  64. template <class T>
  65. T QueueDLL<T>::GetLast()
  66. {
  67. return list.GetTail();
  68. }
  69.  
  70. template <class T>
  71. bool QueueDLL<T>::IsEmpty(void)
  72. {
  73. return list.IsEmpty();
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement