Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. template <typename T>
  2. typename BidiLinkedList<T>::Node**
  3. BidiLinkedList<T>::findAll(Node* startFrom, const T& val, int& size)
  4. {
  5.     if (!startFrom)
  6.         return nullptr;
  7.  
  8.     // try not to use any standard containers. create an array only when found a first occurence
  9.     Node** res = nullptr;
  10.     size = 0;
  11.     Node *current = startFrom;
  12.  
  13.     // figuring out the size of the container
  14.     while (current)
  15.     {
  16.         if (current->getValue() == val)
  17.             size++;
  18.  
  19.         current = current->getNext();
  20.     }
  21.  
  22.     current = startFrom;
  23.  
  24.     // if there are no suitable elements, return null
  25.     if (size != 0)
  26.         res = new Node*[size];
  27.     else return res;
  28.  
  29.     // going through the list for the second time
  30.     // this time filling in the array
  31.     int i = 0;
  32.     while (current)
  33.     {
  34.         if (current->getValue() == val)
  35.             res[i++] = current;
  36.  
  37.         current = current->getNext();
  38.     }
  39.  
  40.     return res;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement