Advertisement
Evinreeder

queue.cpp (Update)

Apr 30th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. #include <iostream>
  2. #include "queue.h"
  3. #include "node.h"
  4. using namespace std;
  5.  
  6. //---------------------------------------------------------------------------------------------------
  7. // initQ
  8. //---------------------------------------------------------------------------------------------------
  9. // Given a queue, initializes it to empty
  10. void initQ(queue & q) {
  11. q.head = NULL;
  12. q.tail = NULL;
  13.  
  14. } // initQ()
  15.  
  16. //---------------------------------------------------------------------------------------------------
  17. // printQ
  18. //---------------------------------------------------------------------------------------------------
  19. // Given a Q, prints the word QUEUE followed by the keys of all nodes in the queue. In addition, (re)print
  20. // the key of the node pointed to by the tail pointer.
  21. void printQ(queue & q) {
  22.  
  23. cout << "Queue ";
  24.  
  25. node * p = q.head;
  26. while (p != NULL) {
  27. cout << p->key << " ";
  28. p = p->next;
  29. }
  30.  
  31.  
  32. //re-print the key of the node pointed to by the tail pointer.
  33. if (q.tail == NULL) {
  34. cout << "Tail = NULL" << endl;
  35. }
  36. else {
  37. cout << "Tail=" << q.tail->key << endl;
  38. }
  39.  
  40. } // printQ()
  41.  
  42. //---------------------------------------------------------------------------------------------------
  43. // enQ
  44. //---------------------------------------------------------------------------------------------------
  45. // Given a Q, pops the top, prints the item popped, and prints the stack
  46. //
  47. // Given a Q, and a key for a new node: allocate a new node and populate the key member with the
  48. // argument passed.Insert the new node at the tail of the current Q. Be sure to update the tail pointer… it
  49. // should point to the newly-allocated node.Be sure the code works for “empty Q”, a Q of 1 node, and a Q
  50. // of 2 or more nodes.
  51. void enQ(queue & q, int key) {
  52. // Case one is when the there is no element, case two if there is only one element,
  53. // and case three if there is two or more elements
  54.  
  55. node *last = NULL, *current = NULL;
  56. node* n = new node;
  57. n->next = NULL;
  58. n->key = key;
  59. current = q.head;
  60. if (q.head != NULL) {
  61. while (current->next != NULL) {
  62. last = current;
  63. current = current->next;
  64. }
  65. if (current->next == NULL) {
  66. q.tail = n;
  67. current->next = q.tail;
  68. }
  69. else {
  70. last->next = n;
  71. n->next = current;
  72. }
  73. }
  74. else {
  75. q.head = n;
  76. n->next = NULL;
  77. }
  78.  
  79. } // enQ()
  80.  
  81. //---------------------------------------------------------------------------------------------------
  82. // deQ
  83. //---------------------------------------------------------------------------------------------------
  84. // Given a Q, deQ's the next item and returns it. Leaves no Garbage.
  85. //
  86. // Given a Q, remove the head node(save its key) and deallocate it. Return the key. When the Q is
  87. // empty, return INT_MAX(a constant defined in <iostream>).Be sure this works for an empty Q, a Q of
  88. // length 1, and a queue of length 2 or more.
  89.  
  90. int deQ(queue & q) {
  91. //Case one is when the there is no element, case two if there is only one element,
  92. //and case three if there is two or more elements
  93.  
  94. node* n = q.head; //current is the first
  95. int value;
  96. if (q.head == NULL) { //so if current is empty, do something(return will be needed)
  97. return INT_MAX;
  98. }
  99. else if (q.head->next == NULL) {
  100. value = n->key;
  101. delete n;
  102. q.head = q.tail = NULL;
  103. }
  104. else {
  105. value = n->key;
  106. q.head = n->next;
  107. delete n;
  108. }
  109. return value;
  110.  
  111. } // deQ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement