Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. /* Receba 10 numeros , remova somente os numeros pares e imprimir de trás para frente " */
  2.  
  3.  
  4. #include <iostream>
  5. using namespace std;
  6. #define tam 10
  7.  
  8. struct no
  9. {
  10. int info;
  11. struct no*prox;
  12. };
  13.  
  14. typedef struct no*noPtr;
  15.  
  16. // funcoes
  17. bool listaVazia(noPtr p);
  18. int inserir(noPtr *start, int x );
  19. void listar(noPtr start);
  20. void retirar(noPtr *start, int x);
  21. void listarPares(noPtr *start);
  22. int menu();
  23. // fim do espaco
  24.  
  25.  
  26. int main(int argc, char** argv)
  27. {
  28. int op;
  29. noPtr inicio = NULL;
  30. int x;
  31. int qtde = 0;
  32.  
  33. do
  34. {
  35. op = menu();
  36. switch(op)
  37. {
  38. case 1:
  39. cout << "Digite um numero para inserir " << endl;
  40. cin >> x;
  41. qtde += inserir(&inicio, x);
  42. case 2:
  43. cout << "Digite um elemento que queira remover " << endl;
  44. cin >> x;
  45. retirar(&inicio, x);
  46. cout << "Elemento removido " << endl;
  47. break;
  48. case 3:
  49. listar(inicio);
  50. break;
  51. case 4:
  52. listarPares(&inicio);
  53. case 0:
  54. break;
  55. }
  56. }
  57. while(op != 0);
  58.  
  59. return 0;
  60. }
  61.  
  62.  
  63. bool listaVazia(noPtr p)
  64. {
  65. if(p == NULL)
  66. return true;
  67. else
  68. {
  69. return false;
  70. }
  71. }
  72.  
  73.  
  74. int inserir(noPtr *start, int x )
  75. {
  76. noPtr p = new no;
  77.  
  78. p->info = x;
  79.  
  80. if(listaVazia(*start))
  81. {
  82. *start = p;
  83. p->prox = NULL;
  84. }
  85. else
  86. {
  87. noPtr aux = *start;
  88. *start = p;
  89. p->prox = aux;
  90. }
  91. cout << "Elemento adicionado " << endl;
  92.  
  93. return 1;
  94.  
  95. }
  96.  
  97. void listar(noPtr start)
  98. {
  99. noPtr p = start;
  100. while(p != NULL)
  101. {
  102. cout << p->info << endl;
  103. p = p->prox;
  104. }
  105. }
  106.  
  107. void listarPares(noPtr *start)
  108. {
  109. int contador = 0;
  110.  
  111. noPtr p = *start;
  112.  
  113. while(p != NULL)
  114. {
  115. if((p->info % 2) == 0)
  116. {
  117. contador += 1;
  118. }
  119. p = p->prox;
  120. } // Quantos elementos pares tem
  121.  
  122. int vetor[contador];
  123.  
  124. for(int i = 0; i < tam ; i++)
  125. {
  126.  
  127. if(p->info % 2 == 0)
  128. {
  129. vetor[i] = p->info;
  130. }
  131. p = p->prox;
  132. } // Adicionar os valores pares a array
  133.  
  134. for(int i = 0; i < tam ; i++)
  135. {
  136. if(p->info % 2 == 0)
  137. {
  138. retirar(start, vetor[i]);
  139. }
  140. p = p->prox;
  141. } // remover os elementos pares da lista encadeada
  142.  
  143. }
  144.  
  145.  
  146.  
  147. void retirar(noPtr *start, int x)
  148. {
  149. noPtr p = *start;
  150.  
  151. if(listaVazia(*start))
  152. {
  153. cout << "Lista Vazia " << endl;
  154. }
  155. else
  156. {
  157. while(p != NULL)
  158. {
  159. if( p->info == x)
  160. {
  161. *start = p->prox;
  162. delete(p);
  163. }
  164. p = p->prox;
  165. }
  166. }
  167. }
  168.  
  169.  
  170. int menu()
  171. {
  172. int op;
  173. cout << "1 INSERIR" << endl;
  174. cout << "2 REMOVER" << endl;
  175. cout << "3 LISTAR" << endl;
  176. cout << "4 LISTAR PARES " << endl;
  177. cout << "0 SAIR" << endl;
  178. cin >> op;
  179. return op; // 1, 2 , 3 , 0
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement