I just need some help to finish this program : exercise 1: The code for the dequ
ID: 3622114 • Letter: I
Question
I just need some help to finish this program :
exercise 1: The code for the dequeue is missing from the implementation section of the queue. Complete the dequeue function.
exercise 2:The code in main13.cc generates 100 random real numbers and enqueues some of them while using the others to request a dequeue. Add code to the program so you can determine the maximum size (length) the queue becomes in the process of generating the 100 random numbers. Display the maximum length. Turn in a listing of the modified source file, a compile and a run.
exercise 3:Modify main13.cc to "generate 100 random real numbers ....." 25 times. Display the maximum length for each of the 25 runs, and calculate and print the average of the maximum lengths for the 25 times. If you were implementing the queue as an array, what size array would you feel comfortable using? Turn in a script file showing the new code and a run.
here is the program:
//This file contains the implementation of a Queue class using a linked list.
#include
#include
#include "inlab13.h"
using namespace std;
// This is the default constructor.
Queue::Queue()
{
frontPtr = NULL;
rearPtr = NULL;
} // end default constructor
//qEmpty tests for an empty queue.
int Queue::qEmpty() const
{
return frontPtr == NULL;
} // end qEmpty
// This function takes the front element off of the queue. It assumes
// that the queue is not empty. Call 'qEmpty()' before calling this
// function to ensure that this is the case.
DataType Queue::dequeue()
{
// This is exercise 1 **************************************
// End exercise 1 ****************************************
} // end enqueue
// This function adds an element to the rear of the queue.
void Queue::enqueue(const DataType& item)
{
NodePtr newPtr = new QueueNode;
newPtr->data = item;
newPtr->next = NULL;
if (qEmpty()) //Was the queue empty?
{
frontPtr = newPtr;
rearPtr = newPtr;
}
else
{
rearPtr->next = newPtr;
rearPtr = newPtr;
}
} // end enqueue
////////////////////////////////////////////////////////////////
//This file contains the definition of the queue class implemented using
//an anchored singly linked list and two list pointers.
#ifndef QUEUE_CLASS_H
#define QUEUE_CLASS_H
typedef float DataType; //the data type is float
struct QueueNode //a node in the Queue has this structure
{
DataType data;
QueueNode* next;
};
typedef QueueNode* NodePtr; //pointer to a node in the Queue
class Queue
{
public:
// default constructor
Queue();
//queue test method
int qEmpty() const;
// queue access methods
DataType dequeue();
void enqueue(const DataType& elt);
private:
// a linked list object to hold the queue items
NodePtr frontPtr, rearPtr;
};
#endif
///////////////////////////////////////////////////////////////////////////
//main
#include
#include // *** new, replaces next line
// #include *** UNIX specific
#include
#include "inlab13.h"
using namespace std;
//Function: floatRand()
// This function generates a random floating point number. It calls
// the standard library function 'rand()' to get an int between 0 and
// RAND_MAX and divides it by RAND_MAX to get a float between 0 and 1.
inline
float floatRand()
{
return ( ((float)rand()) / ((float)RAND_MAX) );
} // end floatRand
int main(void)
{
float item;
Queue q;
int qLength, maxLength; // added for exercise 2
int total; // added for exercise 3
// Initialize the random number generator.
srand((unsigned)time(NULL)); // srand(0) would make results easier to grade
// srand((int) getpid()); // *** replaced by above non-UNIX-specific line
// loop and total for exercise 3
total = 0;
for (int testRun = 0; testRun < 25; testRun++)
{
// Generate 100 random floats between 0 and 1. If the number
// is less than 0.5, it is put into the queue. If the number
// is 0.5 or greater, a number is removed from the queue.
qLength = 0;
maxLength = 0;
for (int i=0; i<100; ++i)
{
item = floatRand();
if (item < 0.5)
{
q.enqueue(item);
// need to add two lines for exercise 2
}
else
{
if (!q.qEmpty())
{
item = q.dequeue();
// need to add one line for exercise 2
}
}
}
// need to add one line for exercise 2 for printing
// need to add one line for exercise 3 for calculating total
while(!q.qEmpty())item = q.dequeue(); // clear out queue
}
// add one line for exercise 3 so as to print average maxiumum length
return 0;
} // end main