fork(1) download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. struct node {
  6. int value;
  7. node * next;
  8. node * previous;
  9. node(int v, node * n = NULL) {
  10. value = v;
  11. previous = next;
  12. next = n;
  13. }
  14. };
  15.  
  16. struct chain {
  17. node * head;
  18. chain(node * h = NULL) {
  19. head = h;
  20. }
  21. ~chain() {
  22. clear();
  23. }
  24. int push(int v) {
  25. head = new node(v, head);
  26. return v;
  27. }
  28. int pop() {
  29. int v = head->value;
  30. node * n = head;
  31. head = head->previous;
  32. delete n;
  33. return v;
  34. }
  35. int back() {
  36. return head->value;
  37. }
  38. void clear() {
  39. while(head != NULL) {
  40. node * n = head;
  41. head = head->next;
  42. delete n;
  43. }
  44. }
  45. int size() {
  46. int size = 0;
  47. for(node * i = head; i != NULL; i = i->next) size++;
  48. return size;
  49. }
  50. bool empty() {return head == NULL;}
  51. } stack;
  52.  
  53. int main() {
  54. string s;
  55. while (cin >> s) {
  56. if (s == "exit") {cout << "bye\n" << head->previous; return 0;}
  57. if (s == "push") {int x; cin >> x; stack.push(x); cout << "ok\n";}
  58. if (s == "pop")
  59. if (stack.empty()) cout << "error" << endl;
  60. else cout << stack.pop() << endl;
  61. if (s == "back")
  62. if (stack.empty()) cout << "error" << endl;
  63. else cout << stack.back() << endl;
  64. if (s == "clear") {stack.clear(); cout << "ok\n";}
  65. if (s == "size") {cout << stack.size() << endl;}
  66. }
  67. }
Compilation error #stdin compilation error #stdout 0s 5420KB
stdin
push 1
push 2
push 3
pop
front
exit
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:56:40: error: ‘head’ was not declared in this scope
   if (s == "exit") {cout << "bye\n" << head->previous; return 0;}
                                        ^~~~
prog.cpp:56:40: note: suggested alternative: ‘fread’
   if (s == "exit") {cout << "bye\n" << head->previous; return 0;}
                                        ^~~~
                                        fread
stdout
Standard output is empty