Advertisement
esquilo10

Untitled

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