Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Queue
- {
- public:
- Queue(); // Constructs a new empty queue.
- Queue( const Queue& q );// Copy constructor.
- ~Queue();// Destructor.
- void print();
- void enqueue( int item ); // Enqueues <item>.
- int dequeue(); // Dequeues the front item.
- int front(); // Returns the front item without dequeuing it.
- bool empty(); // Returns true iff the queue contains no items.
- int size(); // Returns the current number of items in the queue.
- int delay(int item); //
- // (Revised to match the description in the instructions.)
- // Moves all occurrences of <item> (if there are any) to the
- // rear end of the queue; The relative order of all other items
- // is unchanged. The return value is the number of occurrences
- // of item in the queue.
- // If there are no occurrences of <item>, the queue is unchanged.
- private:
- class node // node type for the linked list
- {
- public:
- node(int new_data, node * next_node ){
- data = new_data ;
- next = next_node ;
- }
- int data ;
- node * next ;
- };
- node * front_p ; // pointer to the (node containing the) next item
- // which to be dequeud, or NULL if the queue is empty.
- node * rear_p ; // pointer to the (node containing the) last item
- // which was enqueued, or NULL if the queue is empty.
- int current_size ; // current number of elements in the queue.
- };
- Queue::Queue() {
- front_p = NULL;
- rear_p = NULL;
- current_size = 0;
- }
- void Queue::enqueue(int item) {
- node * n = new node(item, NULL);
- current_size++;
- if (front_p == NULL) {
- front_p = n;
- rear_p = n;
- }
- else {
- rear_p->next = n;
- rear_p = n;
- }
- }
- int Queue::dequeue() {
- node * temp = front_p;
- // std::cout << "front: " << front_p->next->data << std::endl;
- int val = temp->data;
- if (front_p->next == NULL) {
- front_p = NULL;
- rear_p = NULL;
- }
- else {
- front_p = front_p->next;
- }
- delete temp;
- current_size--;
- return val;
- }
- int Queue::delay(int item) {
- node * cursor = front_p;
- //Keeps track of last node that doesn't match item
- node * last;
- bool front_not_set = true;
- int count = 0;
- for (int i = 0; i < current_size; i++) {
- if (cursor->data == item) {
- //If cursor node matches item
- //set the current rear to point to
- //cursor node, then set rear to
- //cursor node
- rear_p->next = cursor;
- rear_p = cursor;
- cursor = cursor->next;
- rear_p->next = NULL;
- //if last has been set
- //set the last->next to
- //next node in linked list
- if (last != NULL) {
- last->next = cursor;
- }
- count++;
- }
- else {
- //set front_p to first node
- //that doesn't match item
- //only occurs once
- if (front_not_set) {
- front_not_set = false;
- front_p = cursor;
- }
- //if cursor node doesn't match item
- //set last to cursor
- last = cursor;
- cursor = cursor->next;
- }
- }
- return count;
- }
- int main () {
- Queue q1;
- q1.enqueue(1);
- q1.enqueue(1);
- q1.enqueue(1);
- q1.enqueue(2);
- q1.enqueue(2);
- q1.enqueue(2);
- q1.enqueue(3);
- q1.enqueue(4);
- q1.enqueue(5);
- q1.delay(1);
- q1.delay(2);
- for (int i = 0; i < 9; i++) {
- cout << q1.dequeue() << endl;
- }
- return 0;
- }
- 3
- 4
- 5
- 600218504
- *** Error in `./test': free(): invalid pointer: 0x00007f2923c69b88 ***
Add Comment
Please, Sign In to add comment