/******************************************************************** * SINGLY Template List class definition * Project #2 - Kyu Bae * *********************************************************************/ #ifndef SLIST_H #define SLIST_H #include #include #include "slistnd.h" /******************************************************************** * template class _NODETYPE * class list declaration * ********************************************************************/ template< class _NODETYPE > class sList { public: sList(); // constructor ~sList(); // destructor void _insertAtFront( const _NODETYPE & ); void _insertAtBack( const _NODETYPE & ); void _changeCurrent(); void _printCurrent (); bool _removeCurrent(_NODETYPE & ); bool _isEmpty() const; void _print() const; sListNode< _NODETYPE > *_firstPtr; // pointer to first node sListNode< _NODETYPE > *_lastPtr; // pointer to last node sListNode< _NODETYPE > *_currentPtr; //pointer to current node private: // Utility function to allocate a new node sListNode< _NODETYPE > *_getNewNode( const _NODETYPE & ); }; /******************************************************************** * class list costructor * initializing the pointers values * ********************************************************************/ // Default constructor template< class _NODETYPE > sList< _NODETYPE >::sList() : _firstPtr( 0 ), _lastPtr( 0 ) , _currentPtr( 0 ) { } /******************************************************************** * class desctructor * delete all allocated space and _data * ********************************************************************/ // Destructor template< class _NODETYPE > sList< _NODETYPE >::~sList() { if ( !_isEmpty() ) { // List is not empty //cout << "Destroying nodes ...\n"; _currentPtr=_firstPtr; sListNode< _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"; } /******************************************************************** * insertatfront() * add node infront of the current node * returns nothing ********************************************************************/ // Insert a node at the front of the list template< class _NODETYPE > void sList< _NODETYPE >::_insertAtFront( const _NODETYPE &value ) { sListNode< _NODETYPE > *_newPtr = _getNewNode( value ); if ( _isEmpty() ) // List is empty _firstPtr = _lastPtr = _currentPtr = _newPtr; else { // List is not empty if (_currentPtr==_firstPtr) { _newPtr->_nextPtr = _firstPtr; _firstPtr = _newPtr; _currentPtr=_newPtr; } else { sListNode< _NODETYPE > *_tempPrev = _firstPtr; while ( _tempPrev->_nextPtr != _currentPtr ) { _tempPrev=_tempPrev->_nextPtr; } _newPtr->_nextPtr=_tempPrev->_nextPtr; _tempPrev->_nextPtr=_newPtr; _currentPtr=_newPtr; } } } /******************************************************************** * insertatback() * add node insertafter of the current node * returns nothing ********************************************************************/ // Insert a node at the back of the list template< class _NODETYPE > void sList< _NODETYPE >::_insertAtBack( const _NODETYPE &value ) { sListNode< _NODETYPE > *_newPtr = _getNewNode( value ); if ( _isEmpty() ) // List is empty _firstPtr = _lastPtr = _currentPtr = _newPtr; else { if (_currentPtr==_lastPtr) { // List is not empty _lastPtr->_nextPtr = _newPtr; _lastPtr = _newPtr; _currentPtr=_newPtr; } else if (_currentPtr==_firstPtr) { _newPtr->_nextPtr=_currentPtr->_nextPtr; _currentPtr->_nextPtr=_newPtr; _currentPtr=_newPtr; } else { sListNode< _NODETYPE > * _tempPrev = _firstPtr; //_tempPrev = _firstPtr; while ( _tempPrev->_nextPtr != _currentPtr ) { _tempPrev = _tempPrev->_nextPtr; } _tempPrev = _currentPtr; _newPtr->_nextPtr = _tempPrev->_nextPtr; _tempPrev->_nextPtr = _newPtr; _currentPtr= _newPtr; } } } /******************************************************************** * List<_NODETYPE> class: changecurrent Function * Asks for the node num and change its current position * return nothing ********************************************************************/ //Change the current node template void sList< _NODETYPE >::_changeCurrent( ) { int _nodenum=0; if ( _isEmpty() ) cout <<"List is empty and can't change the current node!!\n"; else { cout <<"What NODE do you want it to be the current NODE?\n"; cin >> _nodenum; _currentPtr = _firstPtr; for (int i=1;i<_nodenum; ++i) { _currentPtr = _currentPtr->_nextPtr; } // cout <<"The current Node is at NODE # " << i << endl; // cout <<"The current value is " << _currentPtr->_data << endl; } } /******************************************************************** * PrintCurrent Function * Print current node position and its _data * returns nothing ********************************************************************/ //PRINT the current node template void sList< _NODETYPE >::_printCurrent( ) { int c; char junk; int _nodenum=0; if ( _isEmpty() ) cout <<" List is empty and can't change the current node!!\n"; else { if (_currentPtr == _firstPtr) { c=1; } else { sListNode< _NODETYPE > *_tempPtr = _firstPtr; c=2; while(_tempPtr->_nextPtr!=_currentPtr) { c++; _tempPtr = _tempPtr->_nextPtr; } } cout <<"\n\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 |ios::left); cout <_data << endl; // cout <<(_currentPtr->_data).Surname; ???ask about accessingsurname and other private members cout <<" Hit Enter to continure =>\n"; cin.ignore(); junk=cin.get(); } } /******************************************************************** * RemoveCurrent Function * Delete current node and its _data * return boolean when it is true ********************************************************************/ // Delete a node from the current node template< class _NODETYPE > bool sList< _NODETYPE >::_removeCurrent( _NODETYPE &value ) { if ( _isEmpty() ) // List is empty return false; // delete unsuccessful else { if ( _firstPtr == _lastPtr ) { _firstPtr = _lastPtr = 0; value=_currentPtr->_data; //call overload op delete _currentPtr; _currentPtr=0; } else if (_currentPtr==_firstPtr) { _firstPtr=_firstPtr->_nextPtr; value = _currentPtr->_data; //call overload op delete _currentPtr; _currentPtr=_firstPtr; } else if (_currentPtr==_lastPtr) { sListNode<_NODETYPE> * _prevPtr=_firstPtr; while (_prevPtr->_nextPtr!= _currentPtr) _prevPtr=_prevPtr->_nextPtr; _prevPtr->_nextPtr=0; value=_currentPtr->_data; delete _currentPtr; _currentPtr=_lastPtr=_prevPtr; } else { sListNode<_NODETYPE> * _prevPtr=_firstPtr; while (_prevPtr->_nextPtr!= _currentPtr) _prevPtr= _prevPtr->_nextPtr; _prevPtr->_nextPtr = _currentPtr->_nextPtr; value = _currentPtr->_data; // _data being removed delete _currentPtr; _currentPtr = _prevPtr->_nextPtr; } return true; // delete successful } } /******************************************************************** * _isEmpty Function * Check if the list is empty or not and when is empty * Returns boolean and set _firstPtr as zero. ********************************************************************/ // Is the List empty? template< class _NODETYPE > bool sList< _NODETYPE >::_isEmpty() const { return _firstPtr == 0; } /******************************************************************** * GetnewNode() function * Makes new node and set value to zero. * Returns ListNode pointer ********************************************************************/ // Return a pointer to a newly allocated node template< class _NODETYPE > sListNode< _NODETYPE > *sList< _NODETYPE >::_getNewNode( const _NODETYPE &value ) { sListNode< _NODETYPE > *_ptr = new sListNode< _NODETYPE >( value ); assert( _ptr != 0 ); return _ptr; } /******************************************************************** * Print() function * Print its node and its _data * returns nothing ********************************************************************/ // Display the contents of the List template< class _NODETYPE > void sList< _NODETYPE >::_print() const { int c=0; // char junk; if ( _isEmpty() ) { cout << "The list is empty\n\n"; return; } sListNode< _NODETYPE > *_headPtr = _firstPtr; cout <<"\n********************************************************\n"; cout <<"The List Of Titles Containing that Word is: \n"<< endl; int i=1;/* cout <<"_____________________________________________________________________________" << endl; cout.precision(2); cout.setf(ios::showpoint | ios::fixed | ios::left); cout <_data << endl; _headPtr = _headPtr->_nextPtr; i++; /* if (i==16) { cout <<"\nHit Enter Key for the Next Page =>\n"; junk=cin.get(); cout <<"_____________________________________________________________________________" << endl; cout.precision(2); cout.setf(ios::showpoint | ios::fixed |ios::left); cout <\n"; //cin.ignore(); //junk=cin.get(); cout << "\n\n"; } #endif /******************************************************************** * The End of Program * * ********************************************************************/