fork download
  1. #include <limits.h>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. struct list { int data; list* next; };
  6. list* cons(int n, list* p) {
  7. return new list({n,p});
  8. }
  9.  
  10. struct Stack {
  11. list* l;
  12. int empty() { return l==NULL; } // 스택이 비었는지 검사
  13. void push(int n) { l = cons(n,l); }
  14. int pop() {
  15. if ( empty() ) {
  16. cout <<"stack empty" <<endl;
  17. return INT_MIN;
  18. }
  19. int n = l->data;
  20. l = l->next;
  21. return n;
  22. }
  23. };
  24.  
  25.  
  26. int main() {
  27. // your code goes here
  28. Stack s = { NULL };
  29. s.pop();
  30. s.push(1);
  31. s.push(2);
  32. s.push(3);
  33. s.push(4);
  34. int n1 = s.pop();
  35. int n2 = s.pop();
  36. cout <<n1 <<", " <<n2 <<endl;
  37.  
  38. return 0;
  39. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
stack empty
4, 3