Advertisement
Guest User

queue

a guest
Apr 30th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. #include <iostream>
  2. #include "queue.h"
  3. using namespace std;
  4.  
  5. //---------------------------------------------------------------------------------------------------
  6. // initQ
  7. //---------------------------------------------------------------------------------------------------
  8. // Given a queue, initializes it to empty
  9. void initQ(queue & q) {
  10. q.head = NULL;
  11. q.tail = NULL;
  12. } // initQ()
  13.  
  14. //---------------------------------------------------------------------------------------------------
  15. // printQ
  16. //---------------------------------------------------------------------------------------------------
  17. void printQ(queue & q) {
  18. node * p = q.head;
  19. node * z = q.tail;
  20. cout << "QUEUE: ";
  21. if (p == NULL) {
  22. cout << "Empty" << endl;
  23. }
  24. else {
  25. while (z != NULL) {
  26. cout << z->key << " ";
  27. z = z->next;
  28. }
  29. }
  30. if (p != NULL) {
  31. cout << "TAIL= " << p->key << " ";
  32. }
  33. else {
  34. cout << "TAIL= EMPTY";
  35. }
  36. } // printQ()
  37.  
  38. //---------------------------------------------------------------------------------------------------
  39. // enQ
  40. //---------------------------------------------------------------------------------------------------
  41. // Given a Q, pops the top, prints the item popped, and prints the stack
  42. void enQ(queue & q, int key) {
  43.  
  44. node * n = new node;
  45. node * d = new node;
  46. n->key = key;
  47. n->next = NULL;
  48. if (q.head == NULL) {
  49. q.head = n;
  50. q.tail = n;
  51. }
  52. else {
  53. q.head->next = n;
  54. q.head = n;
  55. }
  56.  
  57. }
  58.  
  59. // enQ()
  60.  
  61. //---------------------------------------------------------------------------------------------------
  62. // deQ
  63. //---------------------------------------------------------------------------------------------------
  64. // Given a Q, deQ's the next item and returns it. Leaves no Garbage.
  65. int deQ(queue & q) {
  66. if (q.head != NULL) {
  67. node * c = new node;
  68. c = q.tail;
  69. q.tail = q.tail->next;
  70. return c->key;
  71. }
  72. else {
  73. return 0;
  74. }
  75. } // deQ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement