Advertisement
Guest User

duplamente encadeada simples

a guest
Mar 23rd, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. //EXERCÍCIO 01
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <conio.h>
  5.  
  6. typedef struct agenda {
  7. struct agenda *ant;
  8. char nome[51];
  9. int telefone[9];
  10. char email[51];
  11. struct agenda *prox;
  12. }Tagenda;
  13.  
  14. Tagenda * procurar(Tagenda *L, char nome[]);
  15.  
  16. void adicionar(Tagenda **L, char nome[]) {
  17. Tagenda *novo, *busca;
  18. busca = procurar(*L, nome);
  19. if (busca != NULL){
  20. printf("Impossivel adicionar, contato ja inserido!\n");
  21. return;
  22. }
  23. else{
  24. novo = (Tagenda *)malloc(sizeof(Tagenda));
  25. strcpy(novo->nome, nome);
  26. printf("Informe seu email: ");
  27. gets(novo->email);
  28. fflush(stdin);
  29. printf("Informe seu telefone: ");
  30. gets(novo->telefone);
  31. fflush(stdin);
  32.  
  33. if (*L == NULL){
  34. *L = novo;
  35. novo->prox = NULL;
  36. novo->ant = NULL;
  37. }
  38. else{
  39. novo->prox = *L;
  40. (*L)->ant = novo;
  41. *L = novo;
  42. novo->ant = NULL;
  43. }
  44. }
  45. }
  46.  
  47. Tagenda * procurar(Tagenda *L, char nome[]) {
  48. Tagenda *aux;
  49. if (L == NULL) {
  50. return NULL;
  51. }
  52. else{
  53. aux = L;
  54. while (aux != NULL) {
  55. if (strcmp(aux->nome, nome) == 0) {
  56. return aux;
  57. }
  58. else if (strcmp(aux->nome, nome) > 0){
  59. return NULL;
  60. }
  61. else{
  62. aux = aux->prox;
  63. }
  64. }
  65. }
  66. return NULL;
  67. }
  68.  
  69. void remover(Tagenda **L, char nome[]) {
  70. Tagenda *remover, *auxAnt, *auxPost, *busca, *auxAAnt;
  71. busca = procurar(*L, nome);
  72. if (busca == NULL) {
  73. printf("Esse contato nao consta na agenda!\n");
  74. return;
  75. }
  76. else {
  77. remover = *L;
  78. if (remover->prox == NULL && remover == busca) {
  79. free(remover);
  80. *L = NULL;
  81. printf("Remocao realizada com sucesso!\n");
  82. }
  83. else if (remover->prox != NULL && remover == busca) {
  84. auxAnt = *L;
  85. auxPost = remover->prox;
  86. free(remover);
  87. auxAnt->prox = auxPost;
  88. auxPost->prox = NULL;
  89. auxPost->ant = auxAnt;
  90. printf("Remocao realizada com sucesso!\n");
  91. }
  92. else {
  93. while (remover != busca) {
  94. auxAAnt = auxAnt;
  95. auxAnt = remover;
  96. remover = remover->prox;
  97. auxPost = remover->prox;
  98. }
  99.  
  100. if (remover->prox == NULL) {
  101. auxAnt->prox = NULL;
  102. auxAnt->ant = auxAAnt;
  103. free(remover);
  104. printf("Remocao realizada com sucesso!\n");
  105. }
  106. else {
  107. auxAnt->prox = auxPost;
  108. auxPost->ant = auxAnt;
  109. free(remover);
  110. printf("Remocao realizada com sucesso!\n");
  111. }
  112. }
  113. }
  114. }
  115.  
  116. void listar(Tagenda **L) {
  117. Tagenda *aux;
  118. int contato = 0;
  119. aux = *L;
  120. while (aux != NULL) {
  121. printf("contato %d\n", contato + 1);
  122. puts(aux->nome);
  123. puts(aux->email);
  124. printf("telefone: %s\n", aux->telefone);
  125. aux = aux->prox;
  126. contato++;
  127.  
  128. }
  129. }
  130.  
  131. void exibir(Tagenda **L, char nome[]) {
  132. Tagenda *busca;
  133. busca = procurar(*L, nome);
  134. if (busca == NULL) {
  135. printf("Contato nao cadastrado!\n");
  136. }
  137. else {
  138. printf("Dados do contato\n");
  139. puts(busca->nome);
  140. puts(busca->email);
  141. printf("Telefone: %s\n", busca->telefone);
  142. }
  143.  
  144. }
  145.  
  146. void alterar(Tagenda **L, char nome[]) {
  147. Tagenda *busca;
  148. int op, novoTelefone;
  149. char novoEmail[51];
  150.  
  151. busca = procurar(*L, nome);
  152. if (busca == NULL) {
  153. printf("Esse contato nao esta contido na agenda!\n");
  154. return;
  155. }
  156. else {
  157. do {
  158. printf("1 - alterar email\n2 - alterar telefone\n3 - alterar ambos\nDigite a opcao: ");
  159. scanf("%d", &op);
  160. fflush(stdin);
  161. switch (op){
  162. case 1:
  163. printf("Informe o email: ");
  164. gets(novoEmail);
  165. fflush(stdin);
  166. strcpy(busca->email, novoEmail);
  167. break;
  168. case 2:
  169. printf("Informe o telefone: ");
  170. scanf("%d", &novoTelefone);
  171. fflush(stdin);
  172. strcpy(busca->telefone, novoTelefone);
  173. break;
  174. case 3:
  175. printf("Informe o email: ");
  176. gets(novoEmail);
  177. fflush(stdin);
  178. strcpy(busca->email, novoEmail);
  179. printf("Informe o telefone: ");
  180. scanf("%d", &novoTelefone);
  181. fflush(stdin);
  182. strcpy(busca->telefone, novoTelefone);
  183. break;
  184. default:
  185. break;
  186. }
  187. } while (op < 1 || op > 3);
  188. }
  189. }
  190.  
  191. void removeALL(Tagenda **L) {
  192. Tagenda *remover, *aux;
  193. aux = *L;
  194. if (*L == NULL) {
  195. return;
  196. }
  197. else {
  198. while (aux != NULL) {
  199. remover = aux;
  200. aux = aux->prox;
  201. free(remover);
  202. }
  203. }
  204. }
  205.  
  206.  
  207. int main() {
  208. Tagenda *agenda = NULL;
  209. int op;
  210. char nome[50];
  211. do {
  212. printf("1 - Adicionar contato\n2 - Remover contato\n3 - Listar agenda\n4 - Exibir um contato\n5 - Alterar contato\n6 - Fechar Programa\n Digite a opcao: ");
  213. scanf("%d", &op);
  214. fflush(stdin);
  215. switch (op) {
  216. case 1:
  217. printf("Informe o nome:");
  218. gets(nome);
  219. fflush(stdin);
  220. adicionar(&agenda, nome);
  221. break;
  222. case 2:
  223. printf("Informe o nome:");
  224. gets(nome);
  225. fflush(stdin);
  226. remover(&agenda, nome);
  227. break;
  228. case 3:
  229. listar(&agenda);
  230. break;
  231. case 4:
  232. printf("Informe o nome do contato que deseja exibir: ");
  233. gets(nome);
  234. fflush(stdin);
  235. exibir(&agenda, nome);
  236. break;
  237. case 5:
  238. printf("Informe o nome do contato que deseja alterar: ");
  239. gets(nome);
  240. fflush(stdin);
  241. alterar(&agenda, nome);
  242. break;
  243. case 6:
  244. removeALL(&agenda);
  245. op = 6;
  246. printf("Obrigado por usar o programa!\n");
  247. break;
  248.  
  249. default:
  250. printf("Opcao invalida!\n");
  251. break;
  252. }
  253. } while (op != 6);
  254. return 0;
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement