Assignment Description: You will implement a doubly-linked circular queue of int
ID: 3680005 • Letter: A
Question
Assignment Description:
You will implement a doubly-linked circular queue of integers.
Consider the following class declarations when implementing BQUEUE. As always, you must comment your declaration and implementation files, "BQUEUE.h" and "BQUEUE.cpp", respectively.
class bqnode
{
public:
int time;
bqnode *prev, *next;
};
class BQUEUE
{
public:
BQUEUE( );
~BQUEUE( );
BQUEUE(const BQUEUE &);
void Enqueue(int);
void Dequeue( );
void Print( );
private:
bqnode *front;//use ONLY one pointer
};
Use the following driver called "BQUEUE_driver.cpp" to test your code:
#inlcude <iostream>
#include "BQUEUE.h"
using namespace std;
int main( )
{
BQUEUE k;
k.Enqueue(60);
k.Print();
k.Enqueue(20);
k.Enqueue(30);
k.Print();
k.Enqueue(10);
k.Print();
k.Enqueue(50);
k.Enqueue(40);
k.Print();
BQUEUE j = k;
j.Dequeue();
j.Print();
j.Dequeue();
j.Dequeue();
j.Dequeue();
j.Print();
j.Dequeue();
j.Dequeue();
j.Print();
j.Dequeue();
j.Dequeue();
return 0;
}
Explanation / Answer
Please find below the programs for this assignment. These all are compiled and tested ok. I tried to comment where ever possible for your better understanding. GOOD LUCK !!
/**BQUEUE_driver.cpp**/
#include <iostream>
#include "BQUEUE.h"
using namespace std;
int main( )
{
BQUEUE k;
k.Enqueue(60);
k.Print();
k.Enqueue(20);
k.Enqueue(30);
k.Print();
k.Enqueue(10);
k.Print();
k.Enqueue(50);
k.Enqueue(40);
k.Print();
BQUEUE j = k;
j.Dequeue();
j.Print();
j.Dequeue();
j.Dequeue();
j.Dequeue();
j.Print();
j.Dequeue();
j.Dequeue();
j.Print();
j.Dequeue();
j.Dequeue();
return 0;
}
/**BQUEUE.h**/
#ifndef ASSIGNMENT1_BQUEUE_H
#define ASSIGNMENT1_BQUEUE_H
#include <iostream>
using namespace std;
typedef int QueueElement;
class bqnode
{
public:
//int time;
int data;
bqnode *prev, *next;
};
class BQUEUE
{
public:
BQUEUE(); //Complete
~BQUEUE(); // Complete
BQUEUE(const BQUEUE &); // NEED TO BE COMPLETED
void Enqueue(int); // Complete
void Dequeue(); // Complete
void Print(); // Complete
bool Empty()const {return front == 0;} // Complete
private:
bqnode *front, *back;
};
#endif //ASSIGNMENT1_BQUEUE_H
/**BQUEUE.cpp**/
#include "BQUEUE.h"
using namespace std;
/****************************************************************************************************
Function Name: Default Constructor
Preconditon: Object has not been initialized
Postcondition: Sets front and back to NULL
Description: The front and back of the que are set to NULL
*******************************************************************************************************/
BQUEUE::BQUEUE(): front(0), back(0)
{ }
/****************************************************************************************************
Function Name: Copy Constructor
Preconditon: K has not been copied
Postcondition: Makes a Copy of K into J
Description: Creates a Copy of the current QUE
*******************************************************************************************************/
BQUEUE::BQUEUE(const BQUEUE & item)
{
front= back=0;
(*this) = item;
}
/****************************************************************************************************
Function Name: DeQueue
Preconditon: Vales are to be removed from que
Postcondition: Removes the value form the que
Description: Empties the que by removing all values.
*******************************************************************************************************/
void BQUEUE::Dequeue( )
{
bqnode *temp = new bqnode;
if(front == NULL)
{
cout <<"Queue is empty, nothing to Display ";
}else{
temp = front;
front = front->next;
delete temp;
}
}
/****************************************************************************************************
Function Name: Display
Preconditon: Value in the que are to be Display
Postcondition: Displays the values currently in the que
Description: Displsy the values in the que
*******************************************************************************************************/
void BQUEUE::Print() // Does not display the right information,
{
bqnode *p = front;
if(front ==0)
{
//do nothing
}
else
{
while (p != back) {
cout << p->data << " ";
p = p->next;
}
}
}
/****************************************************************************************************
Function Name: Destructor
Preconditon: Memory occupied by obejct is to be deallocated
Postcondition: Deallocated the memmory that was allocated by Object
Description: Clear memory
*******************************************************************************************************/
BQUEUE::~BQUEUE()
{
cout << "~QUEUE has been called ";
while(!Empty())
Dequeue();
}
/****************************************************************************************************
Function Name: EnQueue
Preconditon: Values are to be added into the que
Postcondition: Value are added to the que
Description: Adds the values into the que
*******************************************************************************************************/
void BQUEUE::Enqueue(int item)
{
if(Empty())
{
front = back = new bqnode;
front->data = item;
}
else
{
back->next = new bqnode;
back->next->prev = back;
back = back ->next;
back->data = item;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.