Advertisement
Guest User

Untitled

a guest
May 20th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. void err()
  5. {
  6. printf("Error\n");
  7. }
  8.  
  9. struct Node
  10. {
  11. char * number;
  12. char * name;
  13. struct Node * l;
  14. struct Node * r;
  15. struct Node * p;
  16. };
  17.  
  18. struct Node * root;
  19. int size;
  20.  
  21. void inic(char * name, char * number)
  22. {
  23. root = malloc(sizeof(struct Node));
  24. root->name = name;
  25. root->number = number;
  26. root->l = NULL;
  27. root->r = NULL;
  28. root->p = NULL;
  29. size = 1;
  30. }
  31. void rec_add(struct Node * cur_vertex, char * name, char * number)
  32. {
  33. int cmp = strcmp(name, cur_vertex->name);
  34. if(cmp==-1)
  35. {
  36. if(cur_vertex->l == NULL)
  37. {
  38. struct Node * new_vertex = malloc(sizeof(struct Node));
  39. new_vertex->name = name;
  40. new_vertex->number = number;
  41. new_vertex->p = cur_vertex;
  42. cur_vertex->l = new_vertex;
  43. new_vertex->l = NULL;
  44. new_vertex->r = NULL;
  45. size++;
  46. }
  47. else
  48. {
  49. rec_add(cur_vertex->l, name, number);
  50. return;
  51. }
  52. }
  53. else
  54. {
  55. if(cur_vertex->r == NULL)
  56. {
  57. struct Node * new_vertex = malloc(sizeof(struct Node));
  58. new_vertex->name = name;
  59. new_vertex->number = number;
  60. new_vertex->p = cur_vertex;
  61. cur_vertex->r = new_vertex;
  62. new_vertex->l = NULL;
  63. new_vertex->r = NULL;
  64. size++;
  65. }
  66. else
  67. {
  68. rec_add(cur_vertex->r, name, number);
  69. return;
  70. }
  71. }
  72. }
  73. void add(char * name, char * number)
  74. {
  75. if(size == 0)
  76. {
  77. inic(name, number);
  78. }
  79. else
  80. {
  81. rec_add(root, name, number);
  82. }
  83. }
  84.  
  85. void rec_del(struct Node * cur_vertex, char * name)
  86. {
  87. int cmp = strcmp(cur_vertex->name, name);
  88. if(cmp==1)
  89. {
  90. if(cur_vertex->l == NULL)
  91. {
  92. err();
  93. return;
  94. }
  95. else
  96. {
  97. rec_del(cur_vertex->l, name);
  98. return;
  99. }
  100. }
  101. else if(cmp==-1)
  102. {
  103. if(cur_vertex->r == NULL)
  104. {
  105. err();
  106. return;
  107. }
  108. else
  109. {
  110. rec_del(cur_vertex->r, name);
  111. return;
  112. }
  113. }
  114.  
  115. if(cur_vertex->l==NULL&&cur_vertex->r==NULL)
  116. {
  117. if(cur_vertex->p != NULL)
  118. {
  119. if(cur_vertex->p->l==cur_vertex)
  120. {
  121. cur_vertex->p->l = NULL;
  122. }
  123. else
  124. {
  125. cur_vertex->p->r = NULL;
  126. }
  127. }
  128. else
  129. {
  130. free(root);
  131. }
  132. free(cur_vertex);
  133. size--;
  134. return;
  135. }
  136.  
  137. if(cur_vertex->l==NULL)
  138. {
  139. struct Node * d = cur_vertex->r;
  140. cur_vertex->name = cur_vertex->r->name;
  141. cur_vertex->number = cur_vertex->r->number;
  142. cur_vertex->l = d->l;
  143. cur_vertex->r = d->r;
  144. if(cur_vertex->r!=NULL)
  145. cur_vertex->r->p = cur_vertex;
  146. if(cur_vertex->l!=NULL)
  147. cur_vertex->l->p = cur_vertex;
  148. free(d);
  149. size--;
  150. return;
  151. }
  152.  
  153. if(cur_vertex->r==NULL)
  154. {
  155. struct Node * d = cur_vertex->l;
  156. cur_vertex->name = cur_vertex->l->name;
  157. cur_vertex->number = cur_vertex->l->number;
  158. cur_vertex->l = d->l;
  159. cur_vertex->r = d->r;
  160. if(cur_vertex->r!=NULL)
  161. cur_vertex->r->p = cur_vertex;
  162. if(cur_vertex->l!=NULL)
  163. cur_vertex->l->p = cur_vertex;
  164. free(d);
  165. size--;
  166. return;
  167. }
  168.  
  169. struct Node * K = cur_vertex->l;
  170. while(K->r!=NULL)
  171. K = K->r;
  172. cur_vertex->name = K->name;
  173. cur_vertex->number = K->number;
  174.  
  175. if(K->p == cur_vertex)
  176. {
  177. cur_vertex->l = K->p->l;
  178. K->p->l->p = cur_vertex;
  179. free(K);
  180. size--;
  181. }
  182. else
  183. {
  184. K->p->r = K->l;
  185. K->l->p = K->p;
  186. free(K);
  187. size--;
  188. }
  189. }
  190.  
  191. void erase(char * name)
  192. {
  193. if(size>0)
  194. {
  195. rec_del(root, name);
  196. }
  197. else
  198. {
  199. err();
  200. return;
  201. }
  202. }
  203.  
  204. void rec_find(struct Node * cur,char * name)
  205. {
  206. int cmp = strcmp(cur->name, name);
  207. if(cmp == 0)
  208. {
  209. printf("Number of %s is %s\n", name, cur->number);
  210. return;
  211. }
  212. if(cmp==1)
  213. {
  214. if(cur->l==NULL)
  215. {
  216. err();
  217. return;
  218. }
  219. else
  220. {
  221. rec_find(cur->l, name);
  222. }
  223. }
  224. else
  225. {
  226. if(cur->r==NULL)
  227. {
  228. err();
  229. return;
  230. }
  231. else
  232. {
  233. rec_find(cur->r, name);
  234. }
  235. }
  236. }
  237.  
  238. void find(char * name)
  239. {
  240. if(size==0)
  241. {
  242. err();
  243. return;
  244. }
  245. else
  246. {
  247. rec_find(root, name);
  248. }
  249. }
  250.  
  251. void rec_bypass(struct Node * cur_vertex)
  252. {
  253. if(cur_vertex->l !=NULL)
  254. {
  255. rec_bypass(cur_vertex->l);
  256. }
  257.  
  258. printf("%s %s\n", cur_vertex->name, cur_vertex->number);
  259.  
  260. if(cur_vertex->r!=NULL)
  261. {
  262. rec_bypass(cur_vertex->r);
  263. }
  264.  
  265.  
  266. }
  267.  
  268. void bypass()
  269. {
  270. if(size>0)
  271. {
  272. rec_bypass(root);
  273. }
  274. else
  275. {
  276. err();
  277. return;
  278. }
  279. }
  280.  
  281. char * readString()
  282. {
  283. char * a = malloc(sizeof(char)*25);
  284. for (int i=0; i<25; i++)
  285. {
  286. a[i]=' ';
  287. }
  288. char ch = getchar();
  289. for (int i=0; i<25; i++)
  290. {
  291. if(ch!=' '&&ch!='\n'&&ch!='\0'&&ch!=EOF)
  292. {
  293. a[i]=ch;
  294. ch = getchar();
  295. }
  296. }
  297. return a;
  298. }
  299. void print(char * a)
  300. {
  301. for (int i=0; i<25; i++)
  302. {
  303. putchar(a[i]);
  304. }
  305. printf("\n");
  306. }
  307. int main()
  308. {
  309. size = 0;
  310. // add (name + number)
  311. // erase(name)
  312. // find (name)
  313. // bypass()
  314. return 0;
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement