Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // DoublyList.h
- #pragma once
- #include "DoublyListNode.h"
- #include <iostream>
- using namespace::std;
- template< class T >
- class DoublyList
- {
- private:
- DoublyListNode< T > *head;
- int size;
- DoublyListNode< T > *find( int index ) const;
- public:
- DoublyList(); // constructor
- DoublyList(const DoublyList< T > &aList); // Insert constructor
- ~DoublyList(); // destructor
- int getLength() const;
- void insert( int index, T tempData);
- void remove( int index );
- void retrieve ( int index, T &tempData );
- bool isEmpty() const;
- void print() const;
- }; // end Double List class
- template < class T >
- bool DoublyList< T > :: isEmpty() const
- {
- if( size == 0 ) return true;
- else return false;
- } // end isEmpty
- template< class T >
- int DoublyList< T > :: getLength() const
- {
- return size;
- } // end getLength function
- template< class T >
- DoublyList< T > :: DoublyList(const DoublyList< T > &aList) : head(NULL), size( 0 )
- {
- // empty body
- } // end List
- template< class T >
- DoublyListNode< T > * DoublyList< T > :: find(int index) const
- {
- if ( ( index < 1) || ( index > getLength() ) )
- return NULL;
- else
- {
- DoublyListNode< T > *currentPtr = head;
- for(int i = 1; i < index; i++)
- {
- currentPtr = currentPtr->nextPtr;
- } // end for
- return currentPtr;
- } // end else
- } // end find
- template< class T >
- void DoublyList< T > :: insert( int index, T tempData )
- {
- int newLength = getLength()+1;
- if( ( index < 1 ) || ( index > newLength ) )
- {
- cout << "Insert index out of range\n";
- } // end if
- else
- {
- DoublyListNode < T > *newPtr = new DoublyListNode< T >(tempData);
- newPtr->nextPtr = newPtr->prePtr = NULL;
- if(newPtr == NULL)
- {
- cout << "Insert cannot allocate memory\n";
- } //end if
- else {
- size = newLength; // Insert at the beginning of the list
- if( index == 1)
- {
- if( head != NULL) { // insert first node
- newPtr->nextPtr = head;
- head->prePtr = newPtr;
- } // end if
- head = newPtr;
- }// end if
- else {
- DoublyListNode< T > *current = find(index - 1);
- if( current->nextPtr == NULL )
- {
- current->nextPtr = newPtr; // Add a node to the end of the list
- newPtr->prePtr = current;
- } // end else
- else {
- newPtr->nextPtr = current->nextPtr;
- newPtr->nextPtr->prePtr = newPtr; // Add a node in any position
- current->nextPtr = newPtr;
- newPtr->prePtr = current;
- } // end else
- } // end else
- } // end else
- } // end else
- } // end insert
- template < class T >
- void DoublyList< T > :: remove(int index)
- {
- DoublyListNode< T > *currentPtr;
- if( ( index < 1) || ( index > getLength() ) )
- {
- cout << "Remove index out of range\n";
- } // end if
- else {
- if( index == 1) // Remove from the beginning of list
- {
- if( head->nextPtr != NULL )
- {
- currentPtr = head;
- head = head->nextPtr;
- head->prePtr = NULL;
- }
- else head = NULL;
- } // end if;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement