Lab 5 (100 pts)

Due Tuesday, February 14 at 9:20am on Catalyst

Writing the Queue From Your List Class
Hint: Writing these functions is much easier than you think! Many require just one line of code.
  • For the Queue data structure from Lesson 5, we built a Queue class from scratch.
  • Let's write a second implementation based off of our List class.
  • Use your List class from Lab 4.
  • Create a new project in Eclipse for this Lab, and copy and paste your List.h into this project.
  • Add a second header file named Queue.h
  • Copy and paste the below code into that file.
  • Note that you should not change the below code in any way.



#ifndef Queue_H_
#define Queue_H_

#include "List.h"
#include <iostream>
#include <cstddef>

using namespace std;

class Queue
{
    public:
        /**constructors and destructors*/

        Queue();

        //initializes an empty queue
        //postcondition: an empty queue

        Queue(const Queue &Q);
        //initializes this queue to have same elements as Q
        //postcondition: a copy of Q

        ~Queue();

        //frees memory allocated to the queue
        //postcondition: memory for queue has been freed

        /**manipulation procedures*/

        void dequeue();
        //removes an element from the queue
        //precondition: the queue must not be empty
        //postcondition: an element has been removed from the front of the queue

        void enqueue(double data);
        //adds an element to the front of the queue
        //postcondition: an element added to the end of the queue

        /**accessors*/   


bool operator==(const Queue &Q);
//returns whether this queue is equal to another queue Q

double get_front();
//returns the element at the front of the queue
//precondition: the queue must not be empty

int get_size();
//returns the current size of the queue


/**additional operations*/


       void printQueue();
       //prints the elements in the queue with a blank space in between each element


    private:
        List<double> queue;
 

};


#endif /* Queue_H_ */


  • You are going to be storing the data for your Queue in a linked list called queue.
  • Note that it is the single private field inside your Queue.h file:

private:
        List<double> queue;

  • Unlike our List, there are no other private fields in the Queue header file. The reason: We are using this List named queue to store all of the data pertaining to the Queue. We do not need any other fields but this one, as the List can perform all the operations we need.



Queue Operations and Your Queue.cpp File:
  • Now, write your Queue implementation inside of your Queue.cpp file.
  • Inside each of the Queue functions, you should call the appropriate List functions to update queue.
  • For example:
void Queue::enqueue(double data)
{
    queue.insertLast(data);
}
  • You should not be re-writing any of your List functions here, just calling them!

Queue constructor and destructor
  • The body of these two functions should simply be left blank. Why?
  • The constructor body should be left blank, because there are no new fields to initialize in the constructor
    • The private List name queue is already initialized inside the Queue.h file, with this line of code:
List<double> queue; //calls the List default constructor
  • The body of the destructor also should be left blank. For example, you would NOT wish to write the following:
Queue::~Queue()
{
    queue.~List(); //<--don't do this!!!
    //you never need to explicitly call a destructor in this class!
}
  • Instead, leave the body of this function empty.
  • The reason?
    • Only in the rarest of circumstances would you need to call a destructor explicitly.
    • Instead, the destructor is called automatically once an object goes out of scope.
    • For our Queue, once its private List named queue goes out of scope, the List destructor will be called automatically.
Queue Copy Constructor
  • Here, you will want to call the copy constructor of the List class:
Queue::Queue(const Queue &Q):queue(Q.queue){}
  • How do you test this copy constructor?

//make a new Queue object with the default constructor

Queue q1;

//add data to q1

q1.enqueue(20.5); //can add more data below

...

//make another new Queue object named q2 by calling the copy constructor

Queue q2(q1); // passing in q1 to make a copy of

//Now, print q2 to make sure it contains the same data as q1

q2.print();

  • Don't forget to test all of your functions inside of your main function in a file called QueueTest.cpp
  • Also, don't forget to print out the results of your tests in your QueueTest.cpp in your comment at the bottom of main.


What to submit

  • Submit your List.h, Queue.cpp, and QueueTest.cpp files to Catalyst when you are finished.
  • Note that your QueueTest.cpp should provide a copy of the print out of running your tests inside a comment at the bottom of the file.


How You Will Be Graded

  • You will receive 80 points for providing a working Queue data structure with all functions implemented and which compiles with the provided header file.
    • 20 points will be for adequate testing
    • 15 points for a correct implementation of the == function.
    • 10 points each for all remaining functions
  • You will receive no credit if Queue.cpp and QueueTest.cpp do not compile with the given header file