home *** CD-ROM | disk | FTP | other *** search
- #include "queue.h"
- #include <assert.h>
-
- charQueue::listNode* charQueue::listNode::kNull = ( listNode*) 0;
-
- charQueue:: charQueue()
- {
- fHead = fTail = listNode::kNull;
- }
- charQueue::~charQueue()
- {
- if (listNode::kNull != fTail)
- {
- listNode* aPointerFromTail = fTail;
- while (listNode::kNull != aPointerFromTail->fNext)
- {
- aPointerFromTail = aPointerFromTail->fNext;
- assert (listNode::kNull != aPointerFromTail->fPrevious);
- delete aPointerFromTail->fPrevious;
- }
- assert (listNode::kNull != aPointerFromTail);
- delete aPointerFromTail;
- fTail = fHead = listNode::kNull;
- }
- }
- void charQueue::add(char aChar)
- {
- if (listNode::kNull == fTail)
- {
- fTail = new listNode();
- fTail->fChar = aChar;
- fHead = fTail;
- }
- else
- {
- listNode* aPointerFromHead = fHead;
- while (listNode::kNull != aPointerFromHead->fPrevious)
- {
- aPointerFromHead = aPointerFromHead->fPrevious;
- }
- aPointerFromHead->fPrevious = new listNode();
- aPointerFromHead->fPrevious->fChar = aChar;
- aPointerFromHead->fPrevious->fNext = aPointerFromHead;
- fTail = aPointerFromHead;
- }
- }
-
- char charQueue::remove()
- {
- char theResult;
- if (listNode::kNull != fHead)
- {
- theResult = fHead->fChar;
- listNode* thePreviousNode = fHead->fPrevious;
- if (listNode::kNull != thePreviousNode)
- thePreviousNode->fNext = listNode::kNull;
- else
- fTail = listNode::kNull;
- delete fHead;
- fHead = thePreviousNode;
- }
- else
- {
- throw QueueEmpty();
- }
- return theResult;
- }
-
- charQueue::listNode::listNode()
- {
- fPrevious = fNext = kNull;
- }
-