Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2010
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct cliente
  6. {
  7. char * nome;
  8. int telefone;
  9. int chave;
  10. struct cliente * dir; //right
  11. struct cliente * esq; //left
  12. } Cliente;
  13.  
  14. Cliente * buscar (Cliente * raiz, int k) // fetch the node
  15. {
  16. while (raiz != NULL && raiz->chave != k)
  17. {
  18. if (raiz->chave > k)
  19. {
  20. raiz = raiz->esq;
  21. }
  22. else
  23. {
  24. raiz = raiz->dir;
  25. }
  26. }
  27. return raiz;
  28. }
  29.  
  30. void imprime_in (Cliente * no, FILE * arquivo)
  31. {
  32. int altura;
  33. if(no != NULL)
  34. {
  35. imprime_in (no->esq, arquivo);
  36. fprintf (arquivo, "Altura = %d, Nome: %s, Telefone %d\n", altura, no->nome, no->telefone);
  37. imprime_in (no->dir, arquivo);
  38. }
  39. }
  40.  
  41. void imprime_pre (Cliente * no, FILE * arquivo)
  42. {
  43. int altura;
  44. if(no != NULL)
  45. {
  46. fprintf (arquivo, "Altura = %d, Nome: %s, Telefone %d\n", altura, no->nome, no->telefone);
  47. imprime_pre (no->esq, arquivo);
  48. imprime_pre (no->dir, arquivo);
  49. }
  50. }
  51.  
  52. void imprime_pos (Cliente * no, FILE * arquivo)
  53. {
  54. int altura;
  55. if(no != NULL)
  56. {
  57. imprime_pos (no->esq, arquivo);
  58. imprime_pos (no->dir, arquivo);
  59. fprintf (arquivo, "Altura = %d, Nome: %s, Telefone %d\n", altura, no->nome, no->telefone);
  60. }
  61. }
  62.  
  63. Cliente * remover (Cliente * raiz, int chave) // removes a node
  64. {
  65. Cliente * p,* p2;
  66. if (raiz == NULL)
  67. {
  68. return raiz;
  69. }
  70. if(raiz->chave == chave)
  71. {
  72. if (raiz->esq == raiz->dir)
  73. {
  74. free (raiz);
  75. return NULL;
  76. }
  77. else if (raiz->esq == NULL)
  78. {
  79. p = raiz->dir;
  80. free (raiz);
  81. return p;
  82. }
  83. else if (raiz->dir == NULL)
  84. {
  85. p = raiz->esq;
  86. free (raiz);
  87. return p;
  88. }
  89. else
  90. {
  91. p2 = raiz->dir;
  92. p = raiz->dir;
  93. while (p->esq)
  94. {
  95. p = p->esq;
  96. }
  97. p->esq = raiz->esq;
  98. free (raiz);
  99. return p2;
  100. }
  101. }
  102. if (raiz->chave < chave)
  103. {
  104. raiz->dir = remover (raiz->dir, chave);
  105. }
  106. else
  107. {
  108. raiz->esq = remover (raiz->esq, chave);
  109. }
  110.  
  111. return raiz;
  112. }
  113.  
  114. Cliente * insere (Cliente * r, Cliente * novo) // includes a new node
  115. {
  116. Cliente *f, *p;
  117. if (r == NULL)
  118. {
  119. return novo;
  120. }
  121. f = r;
  122. while (f != NULL)
  123. {
  124. p = f;
  125. if (f->chave > novo->chave)
  126. {
  127. f = f->esq;
  128. }
  129. else
  130. {
  131. f = f->dir;
  132. }
  133. }
  134. if (p->chave > novo->chave)
  135. {
  136. p->esq = novo;
  137. }
  138. else
  139. {
  140. p->dir = novo;
  141. }
  142. return r;
  143. }
  144.  
  145. int main()
  146. {
  147. FILE * fin = fopen ("L1Q3.in", "r");
  148. FILE * fout = fopen ("L1Q3.out", "w");
  149. char * str = (char *) malloc (31 * sizeof (char));
  150. Cliente * aux;
  151. Cliente * arvore;
  152. int para = 0;
  153. int chave;
  154. int i = 1;
  155. int stop;
  156. int teste;
  157. if (fscanf (fin, "%s", str) == EOF)
  158. {
  159. para++;
  160. }
  161. while (para == 0)
  162. {
  163.  
  164. fprintf (fout, "Conjunto #%d\n", i);
  165. arvore = NULL;
  166. stop = 0;
  167. teste = 0;
  168. do {
  169. if (teste != 0)
  170. {
  171. fscanf (fin, "%s", str);
  172. }
  173. if (strcmp (str, "Inserir") == 0)
  174. {
  175. aux = (Cliente *) malloc (1 * sizeof (Cliente));
  176. aux->nome = (char *) malloc (31 * sizeof (char));
  177. aux->esq = NULL;
  178. aux->dir = NULL;
  179. fscanf (fin, "%s", aux->nome);
  180. fscanf (fin, "%d", &aux->telefone);
  181. fscanf (fin, "%d", &aux->chave);
  182. arvore = insere (arvore, aux);
  183. fprintf (fout, "Cliente %s cadastrado\n", aux->nome);
  184. //printf ("Cliente %s cadastrado\n", aux->nome);
  185. //printf ("%s\n", arvore->nome);
  186. free (aux->nome);
  187. free (aux);
  188. aux = NULL;
  189. }
  190. else if (strcmp (str, "Remover") == 0)
  191. {
  192. fscanf (fin, "%d", &chave);
  193. // printf ("O Programa vai remover o no de chave %d\n", chave);
  194. aux = buscar (arvore, chave);
  195. fprintf (fout, "Cliente %s removido\n", aux->nome);
  196. // printf ("Cliente %s removido\n", aux->nome);
  197. arvore = remover (arvore, chave);
  198. free (aux->nome);
  199. free (aux);
  200. }
  201. else if (strcmp (str, "Buscar") == 0)
  202. {
  203. fscanf (fin, "%d", &chave);
  204. aux = buscar (arvore, chave);
  205. if (aux == NULL)
  206. {
  207. fprintf (fout, "Cliente nao encontrado\n");
  208. // printf ("Cliente nao encontrado\n");
  209. }
  210. else
  211. {
  212. fprintf (fout, "Nome: %s, Telefone %d\n", aux->nome, aux->telefone);
  213. // printf ("Nome: %s, Telefone %d\n", aux->nome, aux->telefone);
  214. }
  215. free (aux->nome);
  216. free (aux);
  217. }
  218. else if (strcmp (str, "Imprimir") == 0)
  219. {
  220. fscanf (fin, "%s", str);
  221. if (strcmp (str, "pre") == 0)
  222. {
  223. fprintf (fout, "Visita pre\n");
  224. imprime_pre (arvore, fout);
  225. }
  226. else if (strcmp (str, "in") == 0)
  227. {
  228. fprintf (fout, "Visita in\n");
  229. imprime_in (arvore, fout);
  230. }
  231. else if (strcmp (str, "pos") == 0)
  232. {
  233. fprintf (fout, "Visita pos\n");
  234. imprime_pos (arvore, fout);
  235. }
  236. }
  237. else if (strcmp (str, "Fim") == 0)
  238. {
  239. stop++;
  240. }
  241. teste++;
  242. } while (stop == 0);
  243. if (fscanf (fin, "%s", str) == EOF)
  244. {
  245. para++;
  246. }
  247. free (arvore);
  248. i++;
  249. }
  250.  
  251. return 0;
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement