Facebook
From Kamis, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 241
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct node{
  5.         int k;
  6.         struct node* prev;
  7.         struct node* ls;
  8.         struct node* ps;
  9. };
  10. struct node *dodaj_element(struct node *root, struct node* nowy);
  11. int main(){
  12. struct node *root=NULL; struct node *nowy=NULL;
  13. root=dodaj_element(root,1);
  14. root=dodaj_element(root,2);
  15. root=dodaj_element(root,3);
  16.  
  17.  
  18. }
  19.  
  20.  
  21.  
  22. struct node *dodaj_element(struct node *root, int n)
  23. {if(root==NULL)
  24. {root=(struct node*)malloc(sizeof(struct node));
  25. root->k=n;
  26. root->prev=NULL;
  27. root->ls=NULL;
  28. root->ps=NULL;
  29. }
  30. else if(n< (root->k))
  31. {if(root->ls!=NULL)
  32.         dodaj_element(n,root->ls);
  33. }
  34. else{
  35.           struct node *nowy = NULL;
  36.           nowy=(struct node*)malloc(sizeof(struct node));
  37.       nowy->k = n;
  38.       nowy->ls= NULL;
  39.       nowy->ps = NULL;
  40.       nowy->prev = root;
  41.       root->ls=nowy;
  42. }
  43. }
  44. else{
  45.          if(root->ps!=NULL)
  46.       {
  47.        dodawanie(n,root->ps);
  48.       }
  49.          else{
  50.            node *nowy = (node*)malloc(sizeof *node);
  51.        nowy->k = n;
  52.        nowy->ls = NULL;
  53.        nowy->ps = NULL;
  54.        nowy->prev = start;
  55.        root->ps=nowy;
  56.          }
  57.  
  58. }
  59. return root;
  60. }
  61.  
  62. void in_order(struct node *root)
  63.  
  64. {
  65.  
  66. if(root->ls != NULL)
  67.    
  68. in_order(root->ls);  
  69. printf("%dn", root->k);
  70.  
  71.  
  72. if(root->ps!= NULL)    
  73. in_order(root->ps);
  74.  
  75. }