Hi Everyone,
Kindly, I need some help with this code. This question is about I have to convert single linked list to doubly linked list. so, I tried to solve this but I have some errors.

This is a single linked list before converting

 ``1234567891011121314151617`` `````` template< typename NODETYPE > void List< NODETYPE >::insertAtFront( const NODETYPE &value ) { ListNode< NODETYPE > *newPtr = getNewNode( value ); // new node if ( isEmpty() ) // List is empty { firstPtr = lastPtr = newPtr; } // new list has only one node else // List is not empty { newPtr->nextPtr = firstPtr; // point new node to previous 1st node firstPtr = newPtr; // aim firstPtr at new node } // end else } // end function insertAtFront ``````

This is after I change in the first condition.

 ``1234567891011121314151617181920212223`` ``````template< typename NODETYPE > void List< NODETYPE >::insertAtFront( const NODETYPE &value ) { ListNode< NODETYPE > *newPtr = getNewNode( value ); // new node ListNode< NODETYPE > *temp; if ( isEmpty() ) // List is empty { newPtr = new Node; firstPtr = newPtr; lastPtr = newPtr; newPtr ->nextPtr = NULL; newPtr ->prevPtr = NULL; newPtr ->data = value; } // new list has only one node else // List is not empty { newPtr->nextPtr = firstPtr; // point new node to previous 1st node firstPtr = newPtr; // aim firstPtr at new node } // end else } // end function insertAtFront ``````

I have an error in Line 9 the
 syntax error : identifier 'Node'
There is no any sense in the statement

newPtr = new Node;

You already initialized newPtr in the previous statement

ListNode< NODETYPE > *newPtr = getNewNode( value ); // new node

Also you should check whether function getNewNode( value ); has already within its body statements similar to

newPtr ->nextPtr = NULL;
newPtr ->prevPtr = NULL;

Last edited on
 ``123`` ``````ListNode< NODETYPE > *newPtr = getNewNode( value ); // new node ... newPtr = new Node;``````
Memory leak?

 ``123456`` ``````else // List is not empty { newPtr->nextPtr = firstPtr; // point new node to previous 1st node firstPtr->prevPtr = newPtr; firstPtr = newPtr; // aim firstPtr at new node } // end else ``````

Last edited on
ohh.. it is obvious.
sorry about that I didn't notice this. I delete that line and it works.

Thank you so much
Topic archived. No new replies allowed.