/******************************************************************** * doulbly list.h * PROJECT #2- KyuBae * Template List class definition * double linked list ********************************************************************/ #ifndef DLIST_H #define DLIST_H #include #include #include "dlistnd.h" /******************************************************************** * template List class * declaration of list class * * ********************************************************************/ template< class NODETYPE > class List { public: List(); // constructor ~List(); // destructor void insertAtFront( const NODETYPE & ); void insertAtBack( const NODETYPE & ); void changeCurrent(); void printCurrent(); bool removeCurrent(NODETYPE & ); void print() const; bool isEmpty() const; private: ListNode< NODETYPE > *firstPtr; // pointer to first node ListNode< NODETYPE > *lastPtr; // pointer to last node ListNode< NODETYPE > *currentPtr; //pointer to current node // Utility function to allocate a new node ListNode< NODETYPE > *getNewNode( const NODETYPE & ); }; /******************************************************************** * List class constructor List() * initialize pointers to zeros * * ********************************************************************/ // Default constructor template< class NODETYPE > List< NODETYPE >::List() : firstPtr( 0 ), lastPtr( 0 ) , currentPtr( 0 ) { } /******************************************************************** * List class destructor List() * delete all allocated pointer and data * * ********************************************************************/ // Destructor template< class NODETYPE > List< NODETYPE >::~List() { if ( !isEmpty() ) { // List is not empty //cout << "Destroying nodes ...\n"; char junk=cin.get(); currentPtr=firstPtr; ListNode< NODETYPE > *tempPtr; while ( currentPtr != 0 ) { // delete remaining nodes tempPtr = currentPtr; // cout << tempPtr->data << '\n'; currentPtr = currentPtr->nextPtr; delete tempPtr; } } //cout << "All nodes destroyed\n\n"; } /******************************************************************** * List class insertAtFront() * add node infront of current node * returns nothing * ********************************************************************/ // Insert a node at the front of the list template< class NODETYPE > void List< NODETYPE >::insertAtFront( const NODETYPE &value ) { ListNode< NODETYPE > *newPtr = getNewNode( value ); if ( isEmpty() ) // List is empty firstPtr = lastPtr = currentPtr = newPtr; else { // List is not empty if (currentPtr == firstPtr) { newPtr->nextPtr = firstPtr; currentPtr->prevPtr = newPtr; firstPtr = newPtr; currentPtr=newPtr; } else { newPtr->nextPtr = currentPtr; newPtr->prevPtr = currentPtr->prevPtr; currentPtr->prevPtr->nextPtr = newPtr; currentPtr->prevPtr = newPtr; currentPtr = newPtr; } } } /******************************************************************** * List class insertAtBack () * add node after the current node and data * returns nothing * ********************************************************************/ // Insert a node at the back of the list template< class NODETYPE > void List< NODETYPE >::insertAtBack( const NODETYPE &value ) { ListNode< NODETYPE > *newPtr = getNewNode( value ); if ( isEmpty() ) // List is empty firstPtr = lastPtr = currentPtr = newPtr; else { if (currentPtr == lastPtr) { // currentPtr = lastPtr;// List is not empty newPtr->prevPtr = currentPtr; lastPtr = newPtr; currentPtr->nextPtr = newPtr; currentPtr=newPtr; } else { newPtr->nextPtr = currentPtr->nextPtr; newPtr->prevPtr = currentPtr; newPtr->nextPtr->prevPtr = newPtr; currentPtr->nextPtr= newPtr; currentPtr=newPtr; } } } /******************************************************************** * class List * change the current node to user's choice * returns nothing * ********************************************************************/ //Change the current node template void List< NODETYPE >::changeCurrent( ) { int nodenum=0; if ( isEmpty() ) cout <<"\n\nList is empty and can't change the current node!!\n"; else { cout <<"\n\nWhat NODE do you want it to be the current NODE? "; cin >> nodenum; currentPtr = firstPtr; for (int i=1;inextPtr; } } } /******************************************************************** * Printcurrent function * print the current node * return nothing * ********************************************************************/ //Print the current node template void List< NODETYPE >::printCurrent() { int c=0; if ( isEmpty() ) cout <<"\n List is empty and can't change the current node!!\n"; else { if (currentPtr == firstPtr) { c=1; } else { ListNode< NODETYPE > *tempPtr = firstPtr; c=2; while(tempPtr->nextPtr!=currentPtr) { c++; tempPtr = tempPtr->nextPtr; } } cout <<"\nThe current Node is at NODE # " << c << endl; cout <<"The current Node is =>\n"; cout <<"_____________________________________________________________________________" << endl; cout.precision(2); cout.setf(ios::showpoint | ios::fixed); cout <data << endl; // cout <<(currentPtr->data).Surname; ??? printing artist and title ask dan } } /******************************************************************** * removeCurrent function * delete the current node * * ********************************************************************/ // Delete a node from the current node template< class NODETYPE > bool List< NODETYPE >::removeCurrent( NODETYPE &value ) { if ( isEmpty() ) // List is empty return false; // delete unsuccessful else { if ( firstPtr == lastPtr ) //deleteOnly-one node { firstPtr = lastPtr = 0; value=currentPtr->data; delete currentPtr; currentPtr=0; } else if (currentPtr == firstPtr) //deleteHead { firstPtr=currentPtr->nextPtr; currentPtr->nextPtr->prevPtr=0; value=currentPtr->data; delete currentPtr; currentPtr=firstPtr; } else if (currentPtr == lastPtr) //delete Tail { lastPtr = currentPtr->prevPtr; currentPtr->prevPtr->nextPtr=0; value = currentPtr->data; delete currentPtr; currentPtr = lastPtr; } else //delete Middle node { ListNode * temp; currentPtr->nextPtr->prevPtr = currentPtr->prevPtr; currentPtr->prevPtr->nextPtr = currentPtr->nextPtr; temp = currentPtr; value=currentPtr->data; currentPtr= currentPtr->nextPtr; delete temp; } return true; // delete successful } } /******************************************************************** * Checking the list and if it is , firstPtr as 0. * class List IsEmpty() * returns first pointer as zero * ********************************************************************/ // Is the List empty? template< class NODETYPE > bool List< NODETYPE >::isEmpty() const { return firstPtr == 0; } /******************************************************************** * class List getNewnode() * make and allocate new pointer and initialize as zero * returns pointer * ********************************************************************/ // Return a pointer to a newly allocated node template< class NODETYPE > ListNode< NODETYPE > *List< NODETYPE >::getNewNode( const NODETYPE &value ) { ListNode< NODETYPE > *ptr = new ListNode< NODETYPE >( value ); assert( ptr != 0 ); return ptr; } /******************************************************************** * print ()- class List * print all informations * returns nothing * Print the records ********************************************************************/ // Display the contents of the List template< class NODETYPE > void List< NODETYPE >::print() const { char junk; if ( isEmpty() ) { cout << " The list is empty\n\n"; return; } ListNode< NODETYPE > *headPtr = firstPtr; cout << " The list is: \n"<< endl; int i=1; cout <<"_____________________________________________________________________________" << endl; cout.precision(2); cout.setf(ios::showpoint | ios::fixed ); cout <data ; headPtr = headPtr->nextPtr; i++; if (i==13) { cout <<"\n Hit Enter Key for the Next Page =>\n"; junk=cin.get(); cout <<"_____________________________________________________________________________" << endl; cout.precision(2); cout.setf(ios::showpoint | ios::fixed); cout <\n"; junk=cin.get(); cout << "\n\n"; } #endif /******************************************************************** * * * * ********************************************************************/