user1856799
user1856799

Reputation: 1

Linked List Find Function C++

How would you go about implementing a find function in a non templated C++ linkedList?

The remove function is implemented in the following way:

 bool LinkedList::remove(Node* n)
 {
      Node *temp = front, *prev = 0;

      for(int i = 0;i < size; i++)
      {
           if (temp == n) 
           {
                if (temp == front)
                {
                     front = n->next;
                } 
                else 
                {
                     prev->next = temp->next;
                }

               delete temp;
               size --;
               return true;
          }
          prev = temp;
          temp = temp->next;
     }
     return false;
}

Upvotes: 0

Views: 1754

Answers (1)

NPE
NPE

Reputation: 500267

Here is a hint: the remove() function already does almost everything that find() will need to do. You just need to figure out which bits to keep and which to get rid of. The stuff you'll need to add is going to be minimal, if any. It will depend on find()'s API, which you're not specifying.

Upvotes: 1

Related Questions