Iterate through multiple linked lists at the same time in c++ +linked list


I am doing a task that requires to calculate a metric from a linked list that contains multiple linked lists of char (each row is a single linked list as shown in the graph). So I will need to iterate through every node that contains a space from the second row, to check how many spaces are surrounding by four other spaces (top, bottom, left, right). For instance, referring to the graph below, the second space in the third row is surrounding by four spaces, so count++. (the "H" just simply means non-space character, sorry that I don't have enough reputation to post a real picture).

I am allowed to used the STL linked list library. I was trying to use three iterators to iterate through three rows at the same time. However, the code gets really messy and does not even work correctly as each row has different lengths. I have been thinking the solution for two days, but as I've been practicing C++ for only two months, so what I could think of is pretty limited. So I am wondering if anyone could give me a hint or a smarter solution, please.

space | space | --H -- | --H -- | -- H -- | NULL| NULL

--- H --| --H ---| space | space | --- H- | -- H -- | NULL

--- H --| space | space | space | -- H-- | space| NULL

space | --H -- | space | space | -- H -- | NULL | NULL


Following may help: (in C++11): <a href="https://ideone.com/1ha7tx" rel="nofollow">Live example</a>.

std::size_t countSpaceSurroundBySpace(const std::list<std::list<char>>& l) { if (l.size() < 3u) { return 0u; } auto it1 = l.begin(); auto it2 = std::next(it1); auto it3 = std::next(it2); std::size_t count = 0u; for (; it3 != l.end(); ++it1, ++it2, ++it3) { // pointers on the 5 characters std::list<char>::const_iterator its[5] = { it1->begin(), it2->begin(), it2->begin(), it2->begin(), it3->begin() }; if (its[0] == it1->end()) { continue; } ++its[0]; if (its[2] == it2->end()) { continue; } ++its[2]; ++its[3]; if (its[3] == it2->end()) { continue; } ++its[3]; if (its[4] == it3->end()) { continue; } ++its[4]; for (; its[0] != it1->end() && its[3] != it2->end() && its[4] != it3->end();) { if (std::all_of(std::begin(its), std::end(its), [](std::list<char>::const_iterator it) { return *it == ' '; })) { ++count; } for (auto& it : its) { ++it; } } } return count; }


You can use a std::vector of std::list::iterators. You'd have an inner loop that just does whatever computation you want at each step, if the iterator isn't already at the corresponding list's end(). This will be a lot easier if your incoming lists are in turn provided as an array or a vector.


  • Display counts for missing dates in mysql query
  • How to differ between Windows Mobile 6.5.3 and previous versions during runtime?
  • How do I make an EditText raise along with the keyboard when in focus?
  • Why is this jQuery reference '$(“”)' instead of '$(“”)'?
  • What do internal compiler error messages mean, and what can I do?
  • Why does an array get passed by value some times and not others?
  • Which devices/recommended sizes should I target with mediaqueries?
  • How to get the sql state from libpq?
  • Using HTML/CSS for UI in XNA?
  • C function strchr - How to calculate the position of the character?
  • Yii2: Finding file and getting path in a directory tree
  • Trying to get the char code of ENTER key
  • Using Sax parsing to edit and write XML in VB6
  • Breaking out column by groups in Pandas
  • WPF ICommand CanExecute(): RaiseCanExecuteChanged() or automatic handling via DispatchTimer?
  • How solve “Qt: Untested Windows version 10.0 detected!”
  • Time complexity of a program which involves multiple variables
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • Listbox within Listbox and scrolling trouble in Windows Phone 7 Silverlight
  • Disable Enter in editText android
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Django: Count of Group Elements
  • Highlight one bar in a series in highcharts?
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Javascript Callbacks with Object constructor
  • How to check if every primary key value is being referenced as foreign key in another table
  • Sending data from AppleScript to FileMaker records
  • MySQL WHERE-condition in procedure ignored
  • How can I use Kendo UI with Razor?
  • When should I choose bucket sort over other sorting algorithms?
  • How do you troubleshoot character encoding problems?
  • Calling of Constructors in a Java
  • PHP: When would you need the self:: keyword?
  • python regex in pyparsing
  • Free memory of cv::Mat loaded using FileStorage API
  • Understanding cpu registers
  • Memory offsets in inline assembly
  • Turn off referential integrity in Derby? is it possible?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize