[Please do not e-mail me off-list. I've been off e-mail for a while
trying to catch up on a major project.]
Sami Jani wrote:
> Hi thomas, i c u r active in c++, im doing some c++ and now training on
> linked lists. Ive a qestion, how to write a function remove(in n) so that the
> function remove a given n = list node? I know how to remove from the start of
> the list and the end of the list but to remove any given node i declare it in
> this way: but i think there should be a better way, thank u inadvance for all
> help. i specially dont like when i define it the last row:
> firstPtr->nextPtr->nextPtr->nextPtr->nextPtr =tempPtr1; i tryed to itrate
> insteak fo firstPtr->nextPtr->----- but all my attempts failed. I there any
> other way to fix it?
>
> here is my fuction remove(): I also attache all the program for u. Thanx fo
> all help.
>
> template< typename NODETYPE >
> bool List< NODETYPE >::remove(int n)
> { List< int > integerList;
>
> if ( isEmpty() ) // List is empty
> return false; // delete unsuccessful
> else
> {
> ListNode< NODETYPE > *tempPtr; // hold tempPtr to delete
> ListNode< NODETYPE > *tempPtr1;
> ListNode< NODETYPE > *tempPtr2;
> ListNode< NODETYPE > *tempPtr3;
>
> if ( firstPtr == lastPtr ) // List has one element
> firstPtr = lastPtr = 0; // no nodes remain after removal
> else
> {
> ListNode< NODETYPE > *currentPtr = firstPtr;
>
> // locate second-to-last element
> while ( currentPtr->nextPtr != lastPtr)
> { currentPtr = currentPtr->nextPtr;
> if(currentPtr->getData()==n)
> {tempPtr2=currentPtr;
> if(currentPtr->nextPtr != lastPtr)
> //cout<<currentPtr->getData()<<endl;
> tempPtr1 = currentPtr->nextPtr;
>
> }
> //cout<<"sami"<<endl;
> }
> } // end else
> //firstPtr->nextPtr=tempPtr1;
>
> firstPtr->nextPtr->nextPtr->nextPtr->nextPtr =tempPtr1;
> return true; // delete successful
> } // end else
--
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197
*NEW* MyTaskFocus 1.1
Get on task. Stay on task.
http://www.CubicleSoft.com/MyTaskFocus/