Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct node{
  6.  
  7. int x;
  8.  
  9. node *ptr;
  10.  
  11. };
  12.  
  13. void insert(node *root , int val){
  14.  
  15. node *ant = root;
  16.  
  17. node *at = root;
  18.  
  19. node *b;
  20.  
  21. while( at != NULL ){
  22.  
  23. if(val <= at -> x){
  24.  
  25. ant -> ptr = (node*)malloc(sizeof(node));
  26.  
  27. b = ant -> ptr;
  28.  
  29. b -> x = val;
  30.  
  31. b -> ptr = at;
  32.  
  33. return;}
  34.  
  35. if(val > at -> x){
  36.  
  37. if(at -> ptr == NULL){
  38.  
  39. at -> ptr = (node*)malloc(sizeof(node));
  40.  
  41. b = at -> ptr;
  42.  
  43. b -> x = val;
  44.  
  45. b -> ptr = NULL;
  46.  
  47. return;}
  48.  
  49. ant = at;
  50.  
  51. at = at -> ptr;}}
  52.  
  53. return;
  54.  
  55. }
  56.  
  57. void ptr(node *root){
  58.  
  59. node *at = root;
  60.  
  61. while(at != NULL){
  62.  
  63. if(at -> x != 0)printf("%d ",at -> x);
  64.  
  65. at = at -> ptr;}
  66.  
  67. return;
  68.  
  69. }
  70.  
  71. int main(){
  72.  
  73. node *root;
  74.  
  75. int n , val , i;
  76.  
  77. root = (node*)malloc(sizeof(node));
  78.  
  79. root -> x = 0;
  80.  
  81. root -> ptr = NULL;
  82.  
  83. scanf("%d",&n);
  84.  
  85. for(i=0;i<n;i++){
  86.  
  87. scanf("%d" , &val);
  88.  
  89. insert(root,val);}
  90.  
  91. ptr(root);
  92.  
  93. return 0;
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement