Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template <typename T>
- void DoubleLinkedList<T>::Extract( int index )//deletes a node from the list
- {
- Node<T> * travel = m_head;
- Node<T> * trail = travel;
- if( travel == 0 )
- throw "Empty List\n";
- else
- {
- if( travel->GetNext() == 0 )//If there is only 1 node in list
- {
- m_head = travel->GetNext();//sets m_head to null
- m_tail = travel->GetNext();//sets m_tail to null
- delete travel; //deletes the only node
- }
- else//if there is more than 1 node in the list
- {
- while( travel->GetData() != index && travel->GetNext() != 0 )//searches through until it finds the node
- {
- trail = travel;
- travel = travel->GetNext();
- }
- if( travel->GetData() != index )//if not in the list display so
- {
- cout << "Not in List\n";
- }
- else if( travel == m_head )//if the node is the head
- {
- trail = m_head;
- m_head = m_head->GetNext();
- if( m_head != 0 )
- m_head->SetPrevious(0);
- delete trail;
- }
- else//if the node is found and is not the head
- {
- if( travel->GetNext() != 0 )
- {
- travel->GetNext()->SetPrevious(trail);
- }
- else
- m_tail = trail;
- trail->SetNext( travel->GetNext() );
- delete travel;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement