Guest User

Untitled

a guest
Oct 25th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. class Queue
  2. {
  3. public:
  4.  
  5. Queue(); // Constructs a new empty queue.
  6. Queue( const Queue& q );// Copy constructor.
  7. ~Queue();// Destructor.
  8. void print();
  9. void enqueue( int item ); // Enqueues <item>.
  10. int dequeue(); // Dequeues the front item.
  11. int front(); // Returns the front item without dequeuing it.
  12. bool empty(); // Returns true iff the queue contains no items.
  13. int size(); // Returns the current number of items in the queue.
  14. int delay(int item); //
  15. // (Revised to match the description in the instructions.)
  16. // Moves all occurrences of <item> (if there are any) to the
  17. // rear end of the queue; The relative order of all other items
  18. // is unchanged. The return value is the number of occurrences
  19. // of item in the queue.
  20. // If there are no occurrences of <item>, the queue is unchanged.
  21.  
  22. private:
  23. class node // node type for the linked list
  24. {
  25. public:
  26. node(int new_data, node * next_node ){
  27. data = new_data ;
  28. next = next_node ;
  29. }
  30. int data ;
  31. node * next ;
  32. };
  33.  
  34. node * front_p ; // pointer to the (node containing the) next item
  35. // which to be dequeud, or NULL if the queue is empty.
  36.  
  37. node * rear_p ; // pointer to the (node containing the) last item
  38. // which was enqueued, or NULL if the queue is empty.
  39.  
  40. int current_size ; // current number of elements in the queue.
  41. };
  42.  
  43. Queue::Queue() {
  44. front_p = NULL;
  45. rear_p = NULL;
  46. current_size = 0;
  47. }
  48.  
  49. void Queue::enqueue(int item) {
  50. node * n = new node(item, NULL);
  51. current_size++;
  52. if (front_p == NULL) {
  53. front_p = n;
  54. rear_p = n;
  55. }
  56. else {
  57. rear_p->next = n;
  58. rear_p = n;
  59. }
  60. }
  61.  
  62. int Queue::dequeue() {
  63. node * temp = front_p;
  64. // std::cout << "front: " << front_p->next->data << std::endl;
  65. int val = temp->data;
  66.  
  67. if (front_p->next == NULL) {
  68. front_p = NULL;
  69. rear_p = NULL;
  70. }
  71. else {
  72. front_p = front_p->next;
  73. }
  74.  
  75. delete temp;
  76. current_size--;
  77. return val;
  78. }
  79.  
  80. int Queue::delay(int item) {
  81. node * cursor = front_p;
  82. //Keeps track of last node that doesn't match item
  83. node * last;
  84. bool front_not_set = true;
  85. int count = 0;
  86.  
  87. for (int i = 0; i < current_size; i++) {
  88.  
  89. if (cursor->data == item) {
  90. //If cursor node matches item
  91. //set the current rear to point to
  92. //cursor node, then set rear to
  93. //cursor node
  94. rear_p->next = cursor;
  95. rear_p = cursor;
  96.  
  97. cursor = cursor->next;
  98. rear_p->next = NULL;
  99.  
  100. //if last has been set
  101. //set the last->next to
  102. //next node in linked list
  103. if (last != NULL) {
  104. last->next = cursor;
  105. }
  106.  
  107. count++;
  108. }
  109. else {
  110. //set front_p to first node
  111. //that doesn't match item
  112. //only occurs once
  113. if (front_not_set) {
  114. front_not_set = false;
  115. front_p = cursor;
  116. }
  117. //if cursor node doesn't match item
  118. //set last to cursor
  119. last = cursor;
  120. cursor = cursor->next;
  121. }
  122.  
  123. }
  124.  
  125. return count;
  126. }
  127.  
  128. int main () {
  129.  
  130. Queue q1;
  131. q1.enqueue(1);
  132. q1.enqueue(1);
  133. q1.enqueue(1);
  134. q1.enqueue(2);
  135. q1.enqueue(2);
  136. q1.enqueue(2);
  137. q1.enqueue(3);
  138. q1.enqueue(4);
  139. q1.enqueue(5);
  140.  
  141. q1.delay(1);
  142. q1.delay(2);
  143.  
  144. for (int i = 0; i < 9; i++) {
  145. cout << q1.dequeue() << endl;
  146. }
  147.  
  148. return 0;
  149. }
  150.  
  151. 3
  152. 4
  153. 5
  154. 600218504
  155. *** Error in `./test': free(): invalid pointer: 0x00007f2923c69b88 ***
Add Comment
Please, Sign In to add comment