fork download
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. struct tree{
  5. int item;
  6. struct tree *next;
  7. };
  8. tree *creat_tree(int );
  9. void insert_top(tree *&,int);
  10. void show(tree *);
  11. tree *creat_tree(int x){
  12. tree *p;
  13. p=new tree;
  14. p->item=x;
  15. p->next=NULL;
  16. return p;
  17. }
  18. void insert_top(tree *&p,int x){
  19. tree *q;
  20. q=creat_tree(x);
  21. q->next=p;
  22. p=q;
  23. return;
  24. }
  25. void insert_bottom(tree *p,int x){
  26. tree *q,*r;
  27. q=creat_tree(x);
  28. r=p;
  29. while(r->next!=NULL){
  30. r=r->next;
  31. }
  32. r->next=q; //show(q);
  33. }
  34. void show(tree *p){
  35. tree *q;
  36. //q=new tree;
  37. q=p;
  38. while(q){
  39. cout<<q->item<<" - ";
  40. q=q->next;
  41. }
  42. }
  43. main(){
  44. tree *p;
  45. p=creat_tree(0);
  46. insert_top(p,1);
  47. insert_top(p,2);
  48. insert_bottom(p,3);
  49. show(p);
  50. }
  51.  
Success #stdin #stdout 0s 3272KB
stdin
Standard input is empty
stdout
2 - 1 - 0 - 3 -