(C++ Intro to Programming 2). If someone would be able to help me with this it w
ID: 3689036 • Letter: #
Question
(C++ Intro to Programming 2). If someone would be able to help me with this it would be greatly appreciated! Thank you!
//--------------------------------------------------------------------
// Laboratory 11 queuelnk.cpp
// SOLUTION: Linked list implementation of the Queue ADT
//--------------------------------------------------------------------
#pragma warning( disable : 4290 ) //get rid of exception warnings
#include <stdexcept>
#include <new>
#include "queuelnk.h"
using namespace std;
//--------------------------------------------------------------------
template < class DT >
QueueNode<DT>::QueueNode(const DT &nodeDataItem,
QueueNode<DT> *nextPtr)
{
dataItem = nodeDataItem;
next = nextPtr;
}
//--------------------------------------------------------------------
template < class DT >
Queue<DT>::Queue(int ignored)
{
front = 0;
rear = 0;
}
//--------------------------------------------------------------------
template < class DT >
Queue<DT>:: ~Queue()
{
clear();
}
//--------------------------------------------------------------------
template < class DT >
void Queue<DT>::enqueue(const DT &newDataItem)
throw (logic_error)
// Inserts newElement at the rear of a queue.
{
QueueNode<DT> *p; // Pointer to enqueued data item
//make p a new QueueNode that has newDataItem and link of zero
//If there are no nodes, set the front to be this new node
//Otherwise, add the new node to the end
//Reassign the rear to p
}
//--------------------------------------------------------------------
template < class DT >
DT Queue<DT>::dequeue() throw (logic_error)
// Removes the least recently (front) element from a queue and
// returns it.
{
QueueNode<DT> *p; // Pointer to dequeued node
DT temp; // Temporarily stores dequeued element
// Requires that the queue is not empty
if (front == 0)
throw logic_error("list is empty");
temp = front->dataItem;
p = front;
front = front->next;
if (front == 0)
rear = 0;
delete p;
return temp;
}
//--------------------------------------------------------------------
template < class DT >
void Queue<DT>::clear()
// Removes all the elements from a queue.
{
QueueNode<DT> *p, // Points to successive nodes
*nextP; // Stores pointer to next node
p = front;
while (p != 0)
{
nextP = p->next;
delete p;
p = nextP;
}
front = 0;
rear = 0;
}
//--------------------------------------------------------------------
template < class DT >
bool Queue<DT>::isEmpty() const
// Returns true if a queue is empty. Otherwise, returns false.
{
return (front == 0);
}
//--------------------------------------------------------------------
template < class DT >
bool Queue<DT>::isFull() const
// Returns true if a queue is full. Otherwise, returns false.
{
// If a node can be successfully allocated than the list is not
// full. If the allocation fails it is implied that there is no
// more free memory therefore the list is full.
QueueNode<DT> *p;
DT junk;
try
{
p = new QueueNode<DT>(junk, 0);
}
catch (bad_alloc &e)
{
return true;
}
delete p;
return false;
}
//--------------------------------------------------------------------
template < class DT >
void Queue<DT>::showStructure() const
// Linked list implementation. Outputs the elements in a queue. If
// the queue is empty, outputs "Empty queue". This operation is
// intended for testing and debugging purposes only.
{
QueueNode<DT> *p; // Iterates through the queue
if (front == 0)
cout << "Empty queue" << endl;
else
{
cout << "front ";
for (p = front; p != 0; p = p->next)
cout << p->dataItem << " ";
cout << "rear" << endl;
}
}
Explanation / Answer
queuelnk.h was not provided. So i couldn't test.
Anyway i have completed the part which i was supposed to complete. Hope this will help you.
//--------------------------------------------------------------------
// Laboratory 11 queuelnk.cpp
// SOLUTION: Linked list implementation of the Queue ADT
//--------------------------------------------------------------------
#pragma warning( disable : 4290 ) //get rid of exception warnings
#include <stdexcept>
#include <new>
#include "queuelnk.h"
using namespace std;
//--------------------------------------------------------------------
template < class DT >
QueueNode<DT>::QueueNode(const DT &nodeDataItem,
QueueNode<DT> *nextPtr)
{
dataItem = nodeDataItem;
next = nextPtr;
}
//--------------------------------------------------------------------
template < class DT >
Queue<DT>::Queue(int ignored)
{
front = 0;
rear = 0;
}
//--------------------------------------------------------------------
template < class DT >
Queue<DT>:: ~Queue()
{
clear();
}
//--------------------------------------------------------------------
template < class DT >
void Queue<DT>::enqueue(const DT &newDataItem)
throw (logic_error)
// Inserts newElement at the rear of a queue.
{
QueueNode<DT> *p; // Pointer to enqueued data item
p = new QueueNode(&nodeDataItem,0) //make p a new QueueNode that has newDataItem and link of zero
if(p==null){
front =p; //If there are no nodes, set the front to be this new node
} else {
rear->next = p;//Otherwise, add the new node to the end
rear = rear->next;/Reassign the rear to p
}
/
}
//--------------------------------------------------------------------
template < class DT >
DT Queue<DT>::dequeue() throw (logic_error)
// Removes the least recently (front) element from a queue and
// returns it.
{
QueueNode<DT> *p; // Pointer to dequeued node
DT temp; // Temporarily stores dequeued element
// Requires that the queue is not empty
if (front == 0)
throw logic_error("list is empty");
temp = front->dataItem;
p = front;
front = front->next;
if (front == 0)
rear = 0;
delete p;
return temp;
}
//--------------------------------------------------------------------
template < class DT >
void Queue<DT>::clear()
// Removes all the elements from a queue.
{
QueueNode<DT> *p, // Points to successive nodes
*nextP; // Stores pointer to next node
p = front;
while (p != 0)
{
nextP = p->next;
delete p;
p = nextP;
}
front = 0;
rear = 0;
}
//--------------------------------------------------------------------
template < class DT >
bool Queue<DT>::isEmpty() const
// Returns true if a queue is empty. Otherwise, returns false.
{
return (front == 0);
}
//--------------------------------------------------------------------
template < class DT >
bool Queue<DT>::isFull() const
// Returns true if a queue is full. Otherwise, returns false.
{
// If a node can be successfully allocated than the list is not
// full. If the allocation fails it is implied that there is no
// more free memory therefore the list is full.
QueueNode<DT> *p;
DT junk;
try
{
p = new QueueNode<DT>(junk, 0);
}
catch (bad_alloc &e)
{
return true;
}
delete p;
return false;
}
//--------------------------------------------------------------------
template < class DT >
void Queue<DT>::showStructure() const
// Linked list implementation. Outputs the elements in a queue. If
// the queue is empty, outputs "Empty queue". This operation is
// intended for testing and debugging purposes only.
{
QueueNode<DT> *p; // Iterates through the queue
if (front == 0)
cout << "Empty queue" << endl;
else
{
cout << "front ";
for (p = front; p != 0; p = p->next)
cout << p->dataItem << " ";
cout << "rear" << endl;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.