Advertisement
Guest User

Lista ED Lulu

a guest
Mar 20th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.41 KB | None | 0 0
  1. ----------------------------------------------- exercício 4 -------------------------------------------------------
  2.  
  3. #include <stdio.h>
  4. #include <windows.h>
  5. #include <conio.h>
  6. #include <string.h>
  7.  
  8.  
  9. struct Estrutura{
  10. int chave;
  11. char nome[45];
  12. };
  13.  
  14. struct LISTA{
  15. Estrutura * estrutura;
  16. LISTA * prox;
  17. };
  18. LISTA * insere_inicio(LISTA * inicio){
  19. LISTA * aux;
  20. LISTA * novo = new LISTA();
  21. novo -> estrutura = new Estrutura();
  22. printf("Informe a Chave ");
  23. scanf("%d", & novo -> estrutura -> chave);
  24. printf("Informe a Nome ");
  25. scanf("%s", & novo -> estrutura -> nome);
  26. //se a lista for vazia � o primeiro no
  27. if(inicio == NULL){
  28. inicio = novo;
  29. inicio -> prox = NULL;
  30. }else{
  31. novo -> prox = inicio;
  32. inicio = novo;
  33. }
  34. return inicio;
  35. }
  36. void imprime_lista(LISTA * inicio){
  37. if(inicio == NULL){
  38. printf("A lista esta vazia ");
  39. }else{
  40. LISTA * aux;
  41. aux = inicio;
  42. // enquanto a lista nao for vazia
  43. while(aux != NULL){
  44. printf("\nChave: %d Nome:%s", aux -> estrutura -> chave, aux -> estrutura -> nome);
  45. aux = aux -> prox;
  46. }
  47. }
  48. }
  49. void busca_lista( LISTA * inicio){
  50. int a,achou=0;
  51. char nome[45];
  52. if(inicio == NULL){
  53. printf("A lista esta vazia");
  54. }else{
  55. LISTA * aux;
  56. aux = inicio;
  57. printf("Informe a Chave: ");
  58. scanf("%d", &a);
  59.  
  60. while(aux != NULL){
  61. if(a == aux -> estrutura -> chave){
  62. printf("Nome: %s", aux->estrutura->nome);
  63.  
  64. achou= 1;
  65. }
  66. aux = aux -> prox;
  67. }
  68. if(achou == 0){
  69. printf("N�o foi encontrado");
  70.  
  71. }
  72. }
  73. }
  74. LISTA * remove (LISTA * inicio){
  75. int b,achou=0;
  76. LISTA * aux;
  77. LISTA * anterior;
  78. if(inicio == NULL){
  79. printf("A lista esta vazia");
  80. }else{
  81. aux = inicio;
  82. anterior = inicio;
  83.  
  84. printf("Informe a Chave: ");
  85. scanf("%d", &b);
  86.  
  87.  
  88. while(aux != NULL){
  89. if(b == aux -> estrutura -> chave){
  90. if(aux == inicio){
  91. inicio = aux -> prox;
  92. delete(aux);
  93. aux = inicio;
  94. }else{
  95. anterior -> prox = aux -> prox;
  96. delete(aux);
  97. aux = anterior -> prox;
  98. }
  99. achou++;
  100. }else{
  101. anterior = aux;
  102. aux = aux -> prox;
  103. }
  104. }
  105. if(achou == 0){
  106. printf("\nnao foi encontrado");
  107. }else{
  108. printf("\nRemovido %d vezes",achou);
  109. }
  110.  
  111. }
  112. return inicio;
  113. }
  114. int main(){
  115. LISTA * inicio = NULL;
  116. int menu;
  117. do{
  118. system("cls");
  119. printf("menu de opcoes");
  120. printf("\n1 - Inserir");
  121. printf("\n2- Consultar");
  122. printf("\n3- Buscar");
  123. printf("\n4- Remover");
  124. printf("\n5- Sair");
  125. printf("\nDigite o opcao desejada ");
  126. scanf("%d", &menu);
  127. switch(menu){
  128. case 1:
  129. inicio = insere_inicio(inicio);
  130. break;
  131. case 2:
  132. imprime_lista(inicio);
  133. break;
  134. case 3:
  135. busca_lista(inicio);
  136. break;
  137. case 4:
  138. inicio = remove(inicio);
  139. break;
  140. }
  141. getch();
  142. }while(menu !=4);
  143. }
  144.  
  145. ----------------------------------------------- exercício 5 -----------------------------------------------------------------
  146.  
  147. #include <stdio.h>
  148. #include <windows.h>
  149. #include <conio.h>
  150. #include <string.h>
  151. #include <stdlib.h>
  152.  
  153. struct Estrutura{
  154. int nota;
  155. char nome[45];
  156. };
  157.  
  158. struct LISTA{
  159. Estrutura * estrutura;
  160. LISTA * prox;
  161. };
  162.  
  163.  
  164. LISTA * insere_nota(LISTA * inicio){
  165. LISTA * aux;
  166. LISTA * novo = new LISTA();
  167. novo -> estrutura = new Estrutura();
  168. printf("Informe a Nome ");
  169. scanf("%s", & novo -> estrutura -> nome);
  170. printf("Informe a nota final: ");
  171. scanf("%d", & novo -> estrutura -> nota);
  172. if(inicio == NULL){
  173. inicio = novo;
  174. inicio -> prox = NULL;
  175. }else{
  176. novo -> prox = inicio;
  177. inicio = novo;
  178. }
  179. return inicio;
  180. }
  181.  
  182.  
  183.  
  184.  
  185.  
  186. void imprime_lista(LISTA * inicio)
  187. {
  188. if (inicio != NULL)
  189. {
  190. if (inicio->estrutura->nota>=7){
  191.  
  192. printf("\nNome: %s ", inicio->estrutura->nome);
  193. printf("Nota: %d ", inicio->estrutura->nota);
  194. }
  195.  
  196. imprime_lista(inicio->prox);
  197. }
  198. }
  199.  
  200.  
  201. int main (){
  202. LISTA * inicio = NULL;
  203. for (int i; i<5; i++){
  204. inicio = insere_nota(inicio);
  205. }
  206. system("cls");
  207. imprime_lista(inicio);
  208.  
  209.  
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement