fork(2) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct list { int data; list* next; };
  5.  
  6. list* cons(int n, list* l) { return new list({n,l}); }
  7.  
  8. struct List {
  9. list* begin;
  10. list* end;
  11. void push_front(int n) {
  12. begin = cons(n,begin);
  13. }
  14. void push_back(int n) {
  15. list* newend = cons(0,NULL); // 새로운 end 를 만든다
  16. end->data = n;
  17. end->next = newend;
  18. end = newend;
  19. }
  20. };
  21.  
  22. void printList(List l) {
  23. for(list* p=l.begin; p!=l.end; p=p->next) {
  24. cout <<p->data <<" ";
  25. }
  26. cout <<endl;
  27. }
  28.  
  29.  
  30. int main(void) {
  31. list* lend = cons(0,NULL);
  32. list* lbegin = cons(1,cons(2,cons(3,lend)));
  33.  
  34. List lis = {lbegin, lend};
  35. lis.push_front(9);
  36. lis.push_back(4);
  37.  
  38. printList(lis);
  39. return 0;
  40. }
  41.  
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
9 1 2 3 4