Advertisement
esquilo10

Untitled

Oct 7th, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5.  
  6. struct no
  7. {
  8. int info;
  9. no *esq, *dir;
  10. };
  11. typedef no* noptr;
  12.  
  13. int criaLista(noptr &lista, int x)
  14. {
  15. if(lista != NULL)
  16. return 1;
  17. lista = new no;
  18. lista->info = x;
  19. lista->esq = NULL;
  20. lista->dir = NULL;
  21. return 0;
  22. }
  23.  
  24. int inseredir(noptr &p, int x)
  25. {
  26. noptr q, d;
  27. if (p == NULL) // se p ´e NULL, n~ao d´a pra inserir `a direita
  28. return 1;
  29. else // inserindo n´o `a direita de p
  30. {
  31. q = new no;
  32. q->info = x;
  33. d = p->dir;
  34. d->esq = q;
  35. q->dir = d;
  36. q->esq = p;
  37. p->dir = q;
  38. return 0;
  39. }
  40. }
  41.  
  42. int contanos(noptr &lista)
  43. {
  44. noptr t = NULL;
  45. int a = 0;
  46. for(t = lista; t != NULL; t = t->dir)
  47. {
  48. a++;
  49. }
  50. return a;
  51. }
  52.  
  53. int somanos(noptr &lista)
  54. {
  55. noptr t = NULL;
  56. int soma = 0;
  57. for(t = lista; t != NULL; t = t->dir)
  58. {
  59. soma = soma + t->info;
  60. }
  61. return soma;
  62. }
  63.  
  64. int main()
  65. {
  66. noptr lista = NULL;
  67. int val;
  68.  
  69. cin >> val;
  70. while(val != 0)
  71. {
  72. if(lista == NULL)
  73. {
  74. criaLista(lista, val);
  75. }
  76. else
  77. {
  78. inseredir(lista, val);
  79. }
  80. cin >> val;
  81. }
  82.  
  83. cout << "NUMERO DE ELEENTOS INSERIDOS: " << contanos(lista);
  84. cout << "SOMA: " << somanos(lista);
  85. cout << "MEDIA: " << somanos(lista)/(contanos(lista)*1.0);
  86.  
  87. return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement