Implementation file CListP.cpp Circular linked list - Pointer

Document Sample
Implementation file CListP.cpp Circular linked list - Pointer Powered By Docstoc
					//   Implementation file CListP.cpp
//   Circular linked list - Pointer-based implementation
//   Written by: Pranava K. Jha
//   Date: Sep. 15, 2008
//   See CList.h for documentation.

#include <iostream>
using namespace std;

#include "CList.h"     // header file
// definitions of methods follow:
CList::CList()   // Default constructor
{
      rear = NULL; // pointer to the last node
      size = 0;    // number of elements
} // end of default constructor

CList::CList(const CList& aList): size(aList.size) // Copy constructor
// aList is the original list, a copy of which is being created.
{
      ListNode *p, *stop;
      if(aList.rear == NULL)
            rear = NULL;
      else
      {
            stop = aList.rear; // pointer to the last node of aList
            // First copy the last node
            rear = new ListNode;
            rear->item = aList.rear->item;
            p = rear; // pointer to the list being created
            stop = stop->next; // at this point, stop points to
                               // the first node of aList.

            // copy rest of the list
            while(stop != aList.rear)
            {
                  // stop is one node ahead of p
                  p->next = new ListNode;
                  p = p->next;
                  p->item = stop->item;
                  stop = stop->next;
            } // end of while
            p->next = rear; // complete the cycle
      } // end of if
} // end of copy constructor

CList::~CList() // Destructor
{
      while(rear != NULL)
            remove(1);
} // end of destructor

// List operations:
bool CList::isEmpty() const
{
      return rear == NULL;
} // end of isEmpty
int CList::getLength() const
{
      return size;
} // end of getLength

int CList::search(CListItemType target) const
{
      if (rear == NULL) // list is empty.
            return 0;

      ListNode* first;
      ListNode* here;
      int i; // index
      first = rear->next; // pointer to the first node
      here = first;
      i = 1;
      do
      {
            if(here->item == target)
                  return i;

            here = here->next;
            i++;
      } while(here != first);
      return 0;
} // end of search;

CListItemType CList::retrieve(int k) const
{
      if (k < 1 || k > size)
      {
            cout << "Function retrieve: Index out of bound" << endl;
            exit(1);
      } // end of if

      // In what follows, 1 <= k <= size
      ListNode* here = rear->next; // pointer to the first node
      int i = 1;
      while(i < k)
      {
            here = here->next;
            i++;
      }
      return here->item;
} // end of retrieve

void CList::insert(CListItemType& newItem)
{
      ListNode *first, *prev, *curr, *here;
      if(rear == NULL) // the list is presently empty.
      {
            rear = new ListNode;
            rear->item = newItem;
            rear->next = rear;
            size = 1;
            return;
      }
      //In what follows, the existing list is non-empty.
      prev = rear;
      first = rear->next; // pointer to the first node
      curr = first;
      do // find prev and curr to appropriately position the item
      {
            if(newItem <= curr->item)
                  break; // location of insertion has been found.
            prev = curr;
            curr = curr->next;
      } while (curr != first);

      here = new   ListNode;
      here->item   = newItem;
      here->next   = curr;
      prev->next   = here; // adjust pointers

      if(newItem > rear->item) // adjust pointer if adding at the end
            rear = here;
      size++; // increment the number of elements
} // end of insert

void CList::remove(int k)
// Note: count starts from the "first" node.
{
      if (k < 1 || k > size)
      {
            cout << "Function remove: index out of bound" << endl;
            exit(1);
      } // end of if

      if(size == 1) // if the list is a singleton, then
      {                   // k must be equal to 1.
            rear->next = NULL;
            delete rear;
            rear = NULL;
            size = 0;
            return;
      } // end of if

      // in what follows, size >= 2.
      int j = 1;
      bool done = false;
      ListNode *prev, *curr;
      prev = rear;
      curr = rear->next;
      // at this point, curr points to the "first" node.

      while(j < k && !done)
      {
            prev = curr;
            curr = curr->next;
            done = (curr == rear->next); // check for a completely
                                         // traversed list
            j++;
      } // end of while
      if(!done) // curr points to the node to be deleted
      {
            if(curr == rear)
                  rear = prev; // "last" node is to be deleted,
                               // so move rear to the new "last" node.
            prev->next = curr->next;
            curr->next = NULL;
            delete curr;
      } // end of if
      size--; // decrement the number of elements
} // end of remove

void CList::print()
// Pre: The pointer variable rear points to the last node.
// Systematically output the data contained in the nodes of
// the list starting from the first node
{
      if(rear == NULL)
            return;

      ListNode *first, *stop;
      first = rear->next; // first points to the first item
      stop = first;
      do
      {
            cout << first->item << " ";
            first = first->next;
      } while(first != stop);
} // end of print