fork download
  1. #include <iostream>
  2. #include <functional>
  3. //using std::cout; using std::endl;
  4. //using std::cerr; using std::function;
  5. struct Node {
  6. int data;
  7. Node* next;
  8. };
  9. void printList(const Node* head) {
  10. for (const Node *wsk = head; wsk != nullptr; wsk = wsk->next){
  11. cout << wsk->data << " ";
  12. }
  13. }
  14. void add(Node*& head, int data) {
  15. Node *toAdd = new Node;
  16. toAdd->data = data;
  17.  
  18. if (!head){
  19. toAdd->next = nullptr;
  20. head = toAdd;
  21. return;
  22. }
  23.  
  24. if (head->data > data){
  25. toAdd->next = head;
  26. head = toAdd;
  27. return;
  28. }
  29.  
  30. Node *wsk;
  31. for (wsk = head; wsk->next != nullptr; wsk = wsk->next){
  32. if (wsk->next->data > data){
  33. Node *tmp = wsk->next;
  34. wsk->next = toAdd;
  35. toAdd->next = tmp;
  36. return;
  37. }
  38. }
  39.  
  40. toAdd->next = nullptr;
  41. wsk->next = toAdd;
  42. }
  43. bool any(const Node* head, function<bool(int)> pred) {
  44. for (const Node *wsk = head; wsk != nullptr; wsk = wsk->next){
  45. if (pred(wsk->data)) return true;
  46. }
  47.  
  48. return false;
  49. }
  50. bool all(const Node* head, function<bool(int)> pred) {
  51. for (const Node *wsk = head; wsk != nullptr; wsk = wsk->next){
  52. if (!pred(wsk->data)) return false;
  53. }
  54.  
  55. return true;
  56. }
  57. void deleteList(Node*& head) {
  58. for (Node *wsk = head; wsk != nullptr;){
  59. Node *tmp = wsk->next;
  60. cout << "Del: " << wsk->data << " ";
  61. delete wsk;
  62. wsk = tmp;
  63. }
  64. head = nullptr;
  65. }
  66. int main() {
  67. Node* head = 0;
  68. add(head, 3);
  69. add(head, 6);
  70. add(head, 2);
  71. add(head, 8);
  72. add(head, 5);
  73. printList(head);
  74. cout << std::boolalpha;
  75. cout << "less than 10 all "
  76. << all(head, [](int i) -> bool {return i< 10; })
  77. << endl;
  78. cout << "is even all "
  79. << all(head, [](int i) -> bool {return i % 2 == 0; })
  80. << endl;
  81. cout << "is even any "
  82. << any(head, [](int i) -> bool {return i % 2 == 0; })
  83. << endl;
  84. deleteList(head);
  85. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'void printList(const Node*)':
prog.cpp:10:45: error: 'nullptr' was not declared in this scope
         for (const Node *wsk = head; wsk != nullptr; wsk = wsk->next){
                                             ^
prog.cpp:11:17: error: 'cout' was not declared in this scope
                 cout << wsk->data << " ";
                 ^
prog.cpp:11:17: note: suggested alternative:
In file included from prog.cpp:1:0:
/usr/include/c++/4.9/iostream:61:18: note:   'std::cout'
   extern ostream cout;  /// Linked to standard output
                  ^
prog.cpp: In function 'void add(Node*&, int)':
prog.cpp:19:31: error: 'nullptr' was not declared in this scope
                 toAdd->next = nullptr;
                               ^
prog.cpp:31:39: error: 'nullptr' was not declared in this scope
         for (wsk = head; wsk->next != nullptr; wsk = wsk->next){
                                       ^
prog.cpp:40:23: error: 'nullptr' was not declared in this scope
         toAdd->next = nullptr;
                       ^
prog.cpp: At global scope:
prog.cpp:43:28: error: 'function' has not been declared
 bool any(const Node* head, function<bool(int)> pred) {
                            ^
prog.cpp:43:36: error: expected ',' or '...' before '<' token
 bool any(const Node* head, function<bool(int)> pred) {
                                    ^
prog.cpp: In function 'bool any(const Node*, int)':
prog.cpp:44:45: error: 'nullptr' was not declared in this scope
         for (const Node *wsk = head; wsk != nullptr; wsk = wsk->next){
                                             ^
prog.cpp:45:35: error: 'pred' was not declared in this scope
                 if (pred(wsk->data)) return true;
                                   ^
prog.cpp: At global scope:
prog.cpp:50:28: error: 'function' has not been declared
 bool all(const Node* head, function<bool(int)> pred) {
                            ^
prog.cpp:50:36: error: expected ',' or '...' before '<' token
 bool all(const Node* head, function<bool(int)> pred) {
                                    ^
prog.cpp: In function 'bool all(const Node*, int)':
prog.cpp:51:45: error: 'nullptr' was not declared in this scope
         for (const Node *wsk = head; wsk != nullptr; wsk = wsk->next){
                                             ^
prog.cpp:52:36: error: 'pred' was not declared in this scope
                 if (!pred(wsk->data)) return false;
                                    ^
prog.cpp: In function 'void deleteList(Node*&)':
prog.cpp:58:39: error: 'nullptr' was not declared in this scope
         for (Node *wsk = head; wsk != nullptr;){
                                       ^
prog.cpp:60:17: error: 'cout' was not declared in this scope
                 cout  << "Del: " << wsk->data << " ";
                 ^
prog.cpp:60:17: note: suggested alternative:
In file included from prog.cpp:1:0:
/usr/include/c++/4.9/iostream:61:18: note:   'std::cout'
   extern ostream cout;  /// Linked to standard output
                  ^
prog.cpp:64:16: error: 'nullptr' was not declared in this scope
         head = nullptr;
                ^
prog.cpp: In function 'int main()':
prog.cpp:74:9: error: 'cout' was not declared in this scope
         cout << std::boolalpha;
         ^
prog.cpp:74:9: note: suggested alternative:
In file included from prog.cpp:1:0:
/usr/include/c++/4.9/iostream:61:18: note:   'std::cout'
   extern ostream cout;  /// Linked to standard output
                  ^
prog.cpp:76:63: warning: lambda expressions only available with -std=c++11 or -std=gnu++11
                 << all(head, [](int i) -> bool {return i< 10; })
                                                               ^
prog.cpp:76:64: error: invalid user-defined conversion from 'main()::<lambda(int)>' to 'int' [-fpermissive]
                 << all(head, [](int i) -> bool {return i< 10; })
                                                                ^
prog.cpp:76:43: note: candidate is: main()::<lambda(int)>::operator bool (*)(int)() const <near match>
                 << all(head, [](int i) -> bool {return i< 10; })
                                           ^
prog.cpp:76:43: note:   no known conversion from 'bool (*)(int)' to 'int'
prog.cpp:77:20: error: 'endl' was not declared in this scope
                 << endl;
                    ^
prog.cpp:77:20: note: suggested alternative:
In file included from /usr/include/c++/4.9/iostream:39:0,
                 from prog.cpp:1:
/usr/include/c++/4.9/ostream:564:5: note:   'std::endl'
     endl(basic_ostream<_CharT, _Traits>& __os)
     ^
prog.cpp:79:68: warning: lambda expressions only available with -std=c++11 or -std=gnu++11
                 << all(head, [](int i) -> bool {return i % 2 == 0; })
                                                                    ^
prog.cpp:79:69: error: invalid user-defined conversion from 'main()::<lambda(int)>' to 'int' [-fpermissive]
                 << all(head, [](int i) -> bool {return i % 2 == 0; })
                                                                     ^
prog.cpp:79:43: note: candidate is: main()::<lambda(int)>::operator bool (*)(int)() const <near match>
                 << all(head, [](int i) -> bool {return i % 2 == 0; })
                                           ^
prog.cpp:79:43: note:   no known conversion from 'bool (*)(int)' to 'int'
prog.cpp:82:68: warning: lambda expressions only available with -std=c++11 or -std=gnu++11
                 << any(head, [](int i) -> bool {return i % 2 == 0; })
                                                                    ^
prog.cpp:82:69: error: invalid user-defined conversion from 'main()::<lambda(int)>' to 'int' [-fpermissive]
                 << any(head, [](int i) -> bool {return i % 2 == 0; })
                                                                     ^
prog.cpp:82:43: note: candidate is: main()::<lambda(int)>::operator bool (*)(int)() const <near match>
                 << any(head, [](int i) -> bool {return i % 2 == 0; })
                                           ^
prog.cpp:82:43: note:   no known conversion from 'bool (*)(int)' to 'int'
stdout
Standard output is empty