SOURCE FILE: deque.cpp
// *********************************************************
// Implementation file deque.cpp for the ADT deque.
// Pointer-based implementation.
// *********************************************************
#include <iostream>
#include <iomanip>
#include "deque.h" // header file
using namespace std ;
// The deque is implemented as a circular doubly linked list
// with one external pointer to the back of the deque.
struct dequeNode
{
dequeItemType Item;
ptrType Next;
ptrType Prev;
}; // end struct
dequeClass::dequeClass() : BackPtr(NULL), Size(0)
{
} // end default constructor
dequeClass::~dequeClass()
{
/* FILL THIS IN */
cout << "... performing destruction ..." << endl ; // stub action
} // end destructor
bool dequeClass::IsEmpty() const
{
/* FILL THIS IN */
cout << "... performing empty test ..." << endl ; // stub action
return true ; // stub action
} // end IsEmpty
int dequeClass::Length() const
{
/* FILL THIS IN */
cout << "... figuring length ..." << endl ; // stub action
return 0 ; // stub action
} // end Length
/*
AddInRear is given here in completed form
as a ' helpful hint' for the student. However, note that
the correct operation of AddInRear depends on the use of
function IsEmpty().
*/
void dequeClass::AddInRear(dequeItemType NewItem, bool& Success)
{
// create a new node
ptrType NewPtr = new dequeNode;
// cout << "... Inserting in REAR ..." << endl ; // stub action
Success = bool(NewPtr != NULL); // check allocation
if (Success)
{ // allocation successful; set data portion of new node
NewPtr->Item = NewItem;
// insert the new node
if (IsEmpty())
{ // insertion into empty deque
NewPtr->Next = NewPtr;
NewPtr->Prev = NewPtr;
}
else
{ // insertion into nonempty deque
NewPtr->Next = BackPtr->Next;
NewPtr->Prev = BackPtr ;
BackPtr->Next->Prev = NewPtr ;
BackPtr->Next = NewPtr;
/* Note: This works if we are adding the second element */
} // end if
BackPtr = NewPtr; // new node is at back
Size++ ;
} // end if
} // end AddInRear
void dequeClass::AddInFront(dequeItemType NewItem, bool& Success)
{
/* FILL THIS IN */
cout << "... Inserting in FRONT ..." << endl ; // stub action
Success = true ; // stub action
} // end AddInFront
void dequeClass::TakeOutFront(dequeItemType& DequeFront, bool& Success)
{
/* FILL THIS IN */
cout << "... Removing from FRONT ..." << endl ; // stub action
Success = true ; // stub action
DequeFront = 0 ; // stub action
} // end TakeOutFront
void dequeClass::TakeOutRear(dequeItemType& DequeRear, bool& Success)
{
/* FILL THIS IN */
cout << "... Removing from REAR ..." << endl ; // stub action
Success = true ; // stub action
DequeRear = 0 ; // stub action
} // end TakeOutRear
void dequeClass::Print() const
{
/* FILL THIS IN */
cout << "... Printing the deque ..." << endl ; // stub action
}
// End of implementation file.