Advertisement
Falmung

DoublyList.h

Oct 26th, 2011
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.92 KB | None | 0 0
  1. // DoublyList.h
  2. #pragma once
  3. #include "DoublyListNode.h"
  4. #include <iostream>
  5. using namespace::std;
  6.  
  7. template< class T >
  8. class DoublyList
  9. {
  10. private:
  11.     DoublyListNode< T > *head;
  12.     int size;
  13.     DoublyListNode< T > *find( int index ) const;
  14.  
  15. public:
  16.     DoublyList(); // constructor
  17.     DoublyList(const DoublyList< T > &aList); // Insert constructor
  18.     ~DoublyList(); // destructor
  19.     int getLength() const;
  20.     void insert( int index, T tempData);
  21.     void remove( int index );
  22.     void retrieve ( int index, T &tempData );
  23.     bool isEmpty() const;
  24.     void print() const;
  25. }; // end Double List class
  26.  
  27. template < class T >
  28. bool DoublyList< T > :: isEmpty() const
  29. {
  30.     if( size == 0 ) return true;
  31.     else return false;
  32. } // end isEmpty
  33.  
  34. template< class T >
  35. int DoublyList< T > :: getLength() const
  36. {
  37.     return size;
  38. } // end getLength function
  39.  
  40. template< class T >
  41. DoublyList< T > :: DoublyList(const DoublyList< T > &aList) : head(NULL), size( 0 )
  42. {
  43.     // empty body
  44. } // end List
  45.  
  46. template< class T >
  47. DoublyListNode< T > * DoublyList< T > :: find(int index) const
  48. {
  49.     if ( ( index < 1) || ( index > getLength() ) )
  50.         return NULL;
  51.     else
  52.     {
  53.         DoublyListNode< T > *currentPtr = head;
  54.         for(int i = 1; i < index; i++)
  55.         {
  56.             currentPtr = currentPtr->nextPtr;
  57.         } // end for
  58.  
  59.         return currentPtr;
  60.     } // end else
  61. } // end find
  62.  
  63. template< class T >
  64. void DoublyList< T > :: insert( int index, T tempData )
  65. {
  66.     int newLength = getLength()+1;
  67.  
  68.     if( ( index < 1 ) || ( index > newLength ) )
  69.     {
  70.         cout << "Insert index out of range\n";
  71.     } // end if
  72.     else
  73.     {
  74.         DoublyListNode < T > *newPtr = new DoublyListNode< T >(tempData);
  75.         newPtr->nextPtr = newPtr->prePtr = NULL;
  76.        
  77.         if(newPtr == NULL)
  78.         {
  79.             cout << "Insert cannot allocate memory\n";
  80.         } //end if
  81.  
  82.         else {
  83.             size = newLength; // Insert at the beginning of the list
  84.             if( index == 1)
  85.             {
  86.                 if( head != NULL) { // insert first node
  87.                     newPtr->nextPtr = head;
  88.                     head->prePtr = newPtr;
  89.                 } // end if
  90.                 head = newPtr;
  91.             }// end if
  92.             else {
  93.                 DoublyListNode< T > *current = find(index - 1);
  94.                 if( current->nextPtr == NULL )
  95.                 {
  96.                     current->nextPtr = newPtr; // Add a node to the end of the list
  97.                     newPtr->prePtr = current;
  98.                 } // end else
  99.                 else {
  100.                     newPtr->nextPtr = current->nextPtr;
  101.                     newPtr->nextPtr->prePtr = newPtr; // Add a node in any position
  102.                     current->nextPtr = newPtr;
  103.                     newPtr->prePtr = current;
  104.                 } // end else
  105.             } // end else
  106.         } // end else
  107.     } // end else
  108. } // end insert
  109.  
  110. template < class T >
  111. void DoublyList< T > :: remove(int index)
  112. {
  113.     DoublyListNode< T > *currentPtr;
  114.     if( ( index < 1) || ( index > getLength() ) )
  115.     {
  116.         cout << "Remove index out of range\n";
  117.     } // end if
  118.    
  119.     else {
  120.         if( index == 1) // Remove from the beginning of list
  121.         {
  122.             if( head->nextPtr != NULL )
  123.             {
  124.                 currentPtr = head;
  125.                 head = head->nextPtr;
  126.                 head->prePtr = NULL;
  127.             }
  128.             else head = NULL;
  129.         } // end if;
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement