fork(1) download
  1. #include <iostream>
  2. #include <list>
  3. using namespace std;
  4.  
  5. class D {
  6. typedef list<int> L;
  7. L* result;
  8. L::iterator ptr;
  9. public:
  10. D() : result(0) {}
  11. ~D() { delete result; }
  12.  
  13. void make_result () {
  14. result = new L;
  15. for (int i=0; i < 5; ++ i)
  16. result->push_back (i);
  17. ptr = result->begin();
  18. }
  19.  
  20. void prev () {
  21. if (result == 0) return;
  22. if (ptr == result->begin()) ptr = result->end ();
  23. --ptr;
  24. }
  25.  
  26. void next () {
  27. if (result == 0) return;
  28. if (++ptr == result->end()) ptr = result->begin ();
  29. }
  30. void display () {
  31. if (result == 0) return;
  32. cout << *ptr << ' ';
  33. }
  34. };
  35.  
  36. int main () {
  37. D d;
  38. d.display (); // output nothing
  39. cout << endl;
  40.  
  41. d.make_result();
  42. for (int i=0; i < 10; ++i) {
  43. d.next();
  44. d.display();
  45. }
  46. cout << endl;
  47.  
  48. for (int i=0; i < 10; ++i) {
  49. d.prev();
  50. d.display();
  51. }
  52. cout << endl;
  53. }
Success #stdin #stdout 0s 3228KB
stdin
Standard input is empty
stdout
1 2 3 4 0 1 2 3 4 0 
4 3 2 1 0 4 3 2 1 0