Advertisement
NovaViper

My Queue

Mar 18th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. /*
  2. Author: W. Douglas Lafield
  3. Editor By: Ilise Leary
  4. Queue.h
  5. */
  6. #ifndef _QUEUE_GUARD
  7. #define _QUEUE_GUARD 1
  8. #define QUEUE_MAX_NUM_OF_NODES 1000
  9.  
  10. using namespace std;
  11.  
  12. template <class T>
  13. class Queue
  14. {
  15.  
  16. private:
  17. /* number of items currently in the queue */
  18. int length;
  19.  
  20. /* You put the rest of the private variables here */
  21. /* Node structure */
  22. struct Node
  23. {
  24. T value;
  25. struct Node *nextNode;
  26. };
  27.  
  28. Node *headNode, *tailNode, *tempNode; //Pointers to the first and last nodes in the list, and a temporary node for saving purposes
  29.  
  30. public:
  31. /* constructer */
  32. Queue() {
  33. length = 0; //Set length to 0
  34. headNode = tailNode = tempNode = nullptr; //Set the headNode and tailNode to null
  35. }
  36.  
  37. /* destructer */
  38. ~Queue() {
  39. while(headNode != nullptr){ //While the headNode is NOT null
  40. tempNode = headNode; //Save headNode into tempNode
  41. headNode = headNode->nextNode; //Make headNode point to the nextNode
  42. tempNode = nullptr;
  43. delete tempNode; //Delete the temporary node
  44. }
  45. }
  46.  
  47. int getLength() { return length; }
  48.  
  49. bool dequeue();
  50. /* remove item from the front of the queue and return the item */
  51.  
  52. bool enqueue(T item);
  53. /* add new item to the end of the queue */
  54.  
  55. T *front();
  56. /* return item at front of the queue without dequeueing */
  57.  
  58. };
  59.  
  60. /***************************************************/
  61. //**Stack with Linked List**//
  62. template <class T>
  63. bool Queue<T>::dequeue()
  64. { /* remove item from the front of the queue and return the item */
  65. if(getLength() == 0) { //If the length is 0
  66. cout << "Deqeue Failed" << endl;
  67. return false;//Make it fail because it's empty!
  68. }
  69.  
  70. cout << "Decrementing" << endl;
  71. length--;//Decrement the length
  72.  
  73. if(getLength() == 0) { //if the length becomes 0
  74. cout << "Removing" << endl;
  75. headNode = tailNode = nullptr; //Make the headNode and tailNode point to null
  76. delete headNode; //Delete headNode
  77. }else{//Otherwise,
  78. tempNode = headNode; //Save the headNode in tempNode
  79. headNode = tempNode->nextNode; //Make the headNode point to the nextNode
  80. tempNode = nullptr;
  81. delete tempNode; //Delete the tempNode
  82. }
  83. return true;
  84. } /* dequeue */
  85.  
  86. /***************************************************/
  87.  
  88. template <class T>
  89. bool Queue<T>::enqueue(T item)
  90. { /* add new item to the end of the queue */
  91. Node *newNode; //Create a blank node called newNode for item
  92. newNode->nextNode = nullptr; //Make the node next to newNode point to null
  93. if(getLength() == 0) { //If the length is 0
  94. headNode = tailNode = newNode; //Make the headNode and tailNode point to newNode
  95. }else{//Otherwise, then
  96. tailNode->nextNode = newNode; //Make the node next to the tailNode point to the newNode
  97. tailNode = newNode; //Make the tailNode point to newNode
  98. }
  99. length++; //Increment the length
  100. return true; //Done
  101. } /* enqueue */
  102.  
  103. /***************************************************/
  104.  
  105. template <class T>
  106. T *Queue<T>::front()
  107. { /* return item at front of the queue without dequeueing */
  108. return &headNode->value;//Return the node next to headNode
  109. } /* front */
  110.  
  111. /***************************************************/
  112. /***************************************************/
  113. /***************************************************/
  114. /***************************************************/
  115. /***************************************************/
  116. /***************************************************/
  117. /***************************************************/
  118. /***************************************************/
  119.  
  120. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement