views:

171

answers:

2

Seems that there is only doubly linked list (but no singly linked list) in the C++ standard library, right? Is there any widely-used C++ libraries with singly linked list?

+1  A: 

There is slist, which is an SGI extension (__gnu_cxx::slist)

#include <iostream>
#include <iterator>
#include <ext/slist>

int main(int argc, char** argv) {
  __gnu_cxx::slist<int> sl;
  sl.push_front(1);
  sl.push_front(2);
  sl.push_front(0);
  std::copy(sl.begin(), sl.end(),  // The output is 0 2 1
            std::ostream_iterator<int>(std::cout, " "));
  std::cout << std::endl;
  return 0;
}
Stephen
In which header? I tried #include <slist>
powerboy
@powerboy: Edited to show header.
Stephen
I prefer this answer because I prefer not to importing another library, though Greg Hewgill's answer is also correct. Thx guys!
powerboy
+5  A: 

There is the slist class from Boost that is a singly linked list implementation.

Greg Hewgill
slist is in Boost, not STL, right?
powerboy
It appears that there is an `slist` in some vendor implementations of the STL, but not in the C++ Standard Library.
Greg Hewgill