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?
views:
171answers:
2
+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
2010-05-10 02:52:34
In which header? I tried #include <slist>
powerboy
2010-05-10 02:57:21
@powerboy: Edited to show header.
Stephen
2010-05-10 03:04:46
I prefer this answer because I prefer not to importing another library, though Greg Hewgill's answer is also correct. Thx guys!
powerboy
2010-05-10 03:10:12
+5
A:
There is the slist
class from Boost that is a singly linked list implementation.
Greg Hewgill
2010-05-10 02:53:41
It appears that there is an `slist` in some vendor implementations of the STL, but not in the C++ Standard Library.
Greg Hewgill
2010-05-10 02:58:56