Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Recursive Search

Name: Anonymous 2007-03-29 21:56 ID:lgeZbKUo

How would one search a C++ linked list with a recursive function?

Name: Anonymous 2007-03-29 22:30 ID:wz08uY4M

#include <list>

int searchList(std::list<int> inList, int searchThing) {
  int result;
  std::list<int>::iterator iter = inList.begin();
  if (iter == inList.end()) {
    result = 9001;
  } else if ((*iter) == searchThing) {
    result = (*iter);
  } else {
    ++iter;
    result = searchList(std::list<int>(iter, inList.end()), searchThing);
  }
  return result;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List