Question
you have to implement an unfair queue from the scratch. A skeleton code is provided to guide you through the process. Unfair queue has all
you have to implement an unfair queue from the scratch. A skeleton code is provided to guide you through the process. Unfair queue has all the properties of a queue, however, it can do more. you have to implement an unfair queue from the scratch. A skeleton code is provided to guide you through the process. Unfair queue has all the properties of a queue, however, it can do more.
======================================================================
#include
#include "queue.h"
int main() {
std::cout << "create a queue with default constructor:"< ubcse::queue std::cout << "q1 is created with default constructor"<< std::endl; if (q1.empty() ) { std::cout << "Queue is empty "; } else{ std::cout << "Queue is not empty "; } // pushes some values into the queue: std::cout << "Pushing three values: 10, 20, 30"<< std::endl; q1.push(10); q1.push(20); q1.push(30); std::cout << "Printing q1: "; q1.print(); std::cout << "Size of q1: " << q1.size() << std::endl; std::cout << "poping an item from q1 ..."<< std::endl; q1.pop(); std::cout << "printing q1 after pop operation:"<< std::endl; q1.print(); std::cout << "poping rest of the items ..."<< std::endl; q1.pop(); q1.pop(); std::cout << "Now printing q1 again ..."<< std::endl; q1.print(); std::cout << "Creating a new queue q2 with default constructor"<< std::endl; ubcse::queue q2.push(100); q2.push(200); q2.push(300); std::cout << "printing q2:"<< std::endl; q2.print(); std::cout << "Perform swap operation."<< std::endl; q2.swap(q1); std::cout<< "After swap between q2 and q1:"< std::cout << "q1 : "; q1.print(); std::cout << "q2 : "; q2.print(); std::cout << "Creating another queue with copy constructor:"<< std::endl; std::cout << "copying q1 to q3"<< std::endl; ubcse::queue std::cout << "printing q3:"<< std::endl; q3.print(); std::cout << "size of q3:"; std::cout << q3.size() << std::endl; std::cout << "Printing q1:"<< std::endl; q1.print(); std::cout << "size of q1:"; std::cout << q1.size() << std::endl; std::cout << "Deleting everything from q3:"<< std::endl; q3.delete_all(); std::cout << "Printing q3:"<< std::endl; q3.print(); std::cout << "Printing q1:"<< std::endl; q1.print(); std::cout << "Pushing values into q1: 400, 500, 600, 700, 800."<< std::endl; q1.push(400); q1.push(500); q1.push(600); q1.push(700); q1.push(800); std::cout << "printing q1:"<< std::endl; q1.print(); std::cout << "Printing q3:"<< std::endl; q3.print(); std::cout << "Now removing the 3rd item:"<< std::endl; q1.delete_from_the_middle(2); std::cout << "Printing q1 after removing 3rd item :"<< std::endl; q1.print(); std::cout << "inserting 10000 in the 4th position of q1:"<< std::endl; q1.insert_in_the_middle(10000, 3); std::cout << "Printing q1:"<< std::endl; q1.print(); } //end of main =============================================================== #ifndef NODE_H_ #define NODE_H_ template struct Node { // Data Fields /** The data */ ntype data; /** The pointer to the next node. */ Node* next; // Constructor /** Creates a new Node that points to another Node. @param data_item The data stored @param next_ptr Pointer to the Node that is pointed to by the new Node */ Node(const ntype& data_item, Node* next_ptr = nullptr) : data(data_item), next(next_ptr) {} }; #endif =============================================== #ifndef QUEUE_CPP_ #define QUEUE_CPP_ /* ************** GETTER FUNCTIONS *************************/ /** * TO DO: implement get_head method. * This method returns the head pointer. */ template Node //write your code here... } // end of get_head function. /** * TO DO: implement get_tail method. * This method returns the tail pointer. */ template Node //write your code here ... } // end of get_tail function. /** * TO DO: implement size method. * This function returns the size of the queue. * return type is 'size_t'. */ template size_t queue //write your code here... } /* end of size method */ /** * TO DO : implement empty method. * This function returns whether the queue is empty * or not. if empty it returns true, otherwise returns * false. */ template bool queue // write your code here .... } /************ SETTER FUNCTIONS ***********************/ /** * TO DO : implement set_head method. * This function updates the head pointer * with given pointer. */ template void queue //write your code here ... } /** * TO DO : implement set_tail method. * This function updates the tail pointer * with given pointer. */ template void queue //write your code here ... } /** * TO DO: implement set_size. * This method can update the num_items with the given * input. */ template void queue //write your code here ... } /************ CORE QUEUE OPERATIONS **********************/ /** TO DO: * This method addes the given item onto the back * of the queue. Update the tail pointer accordingly. * and also increase 'num_items' by 1. */ template void queue //write your code here ... } // end of push function /** * TO DO: implement pop method. * This function removes the item from the front of the queue. * it returns nothing. * if queue is empty it will print a message "queue is empty" * and return. * Don't forget to reduce the 'num_items' by 1. */ template void queue //write your code here ... } /* end of pop method */ /** * TO DO: implement front method. * This function returns the front of the queue. * if the queue is empty is prints a message "queue is empty" * throw an error. */ template const dtype& queue //write your code here ... } /** * TO DO: implement front method. * This function returns the front of the queue. * if the queue is empty is prints a message "queue is empty" * throw an error. */ template dtype& queue //write your code here ... } /************ FOR PORTABILITY **********************************/ /** * TO DO : implement swap method. * Input argument: * @param other - reference to a queue. * exchange contents of this queue by those of 'other'. * also swap the 'num_items'. */ template void queue //write your code here ... } /* end of swap method */ /******* UNFAIR QUEUE OPERATIONS **********************************/ /** * TO DO: implement inset_in_the_middle * This method enables the queue to be unfair. With this function * you can push something in the middle of the queue. As argument * you need two arguments - the value that you want to push and * the position where you want to push. position argument is similar * to index, it starts from 0. That means, the front of the queue has * position 0. */ template void queue //write your code here ... } /** TO DO: * Implement delete_from_the_middle * This method also enables the queue to be unfair. You can remove something * from the middle of the queue. As input argument you need the position that * you want to delete. position argument is similar * to index, it starts from 0. That means, the front of the queue has * position 0. */ template void queue //write your code here ... } /** * This method deletes all the elements in the queue * without memory leak. */ template void queue //write your code here ... } /* end of delete_all */ /******************** PRINT *************************************/ /** * This method prints the queue. * if the queue is empty, it prints error message */ template void queue if (num_items == 0 ){ std::cout << "Queue is empty"<< std::endl; } else{ Node //std::cout<<"here "<< head->next->data< while( iter != nullptr ){ std::cout << iter->data << " "; iter = iter->next; } std::cout << std::endl; } } /* end of print method */ #endif =============================================== #ifndef QUEUE_H_ #define QUEUE_H_ #include namespace ubcse{ #include "node.h" template class queue { private: Node Node size_t num_items; public: /** TO DO: implement default constructor. * default constructor: * this constructor initializes the following: * head to nullptr, * tail to nullptr, * num_item to zero. */ queue // write your code here }; /** TO DO: implement copy constructor. * copy constructor: * This constructor performs deep copy of the queue. * Input argument for this constructor is a constant * reference of another queue. You cannot change the * given queue. * Copy all the data members from the given queue to * this queue. That means both num_items and queue. Both 'head' * 'tail' pointer should point properly in this queue. * Don't forget to copy num_itmes as well. */ queue num_items = other.size(); if (num_items == 0){ head = nullptr; tail = nullptr; } else{ // write your code here. } }; /** TO DO: implement destructor. * Write a destructor to release memory. */ // write your code here. /******* getter function declarations *******************/ /* returns the head pointer */ Node /* returns the tail pointer. */ Node /* returns the size of the queue. */ size_t size() const; /* checks whether queue is empty or not. */ bool empty() const; /******** setter/ modifier function declarations *************/ /* assign the given pointer val to head pointer */ void set_head(Node /* assign the given pointer val to tail pointer */ void set_tail(Node /* update the current number of items in the queue */ void set_size(size_t n_items); /********************* Core queue operations **********/ /* pushes an item onto the tail. */ void push(const dtype& val); /* removes the front item from the queue */ void pop(); /* return the front element, however, it is not going to delete the front element. */ dtype& front(); const dtype& front() const; /******** For portability **********************/ /* performs swap between two queues */ void swap(queue /********* operations for unfair queue **************/ /* deletes the whole queue without memory leak */ void delete_all(); /* delete data from the middle */ void delete_from_the_middle(size_t position); /* insert data in the middle */ void insert_in_the_middle(const dtype& val, const size_t position); /******* printing *******************************/ /* prints the whole queue */ void print(); }; // end of Stack class #include "queue.cpp" } // end of namespace ubcse #endif
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started