Advertisement
ferseg

listas maymenprom

Oct 13th, 2014
20,587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 KB | None | 0 0
  1. /*
  2.     Autor: Joel Fernandez
  3.     Fecha: 11/10/14
  4.     Tema : Listas Enlazadas Simples
  5.     Ejercicio 1 : Crear una lista que almacene los n primeros numeros enteros y calcular el menor, mayor, y el promedio.
  6. */
  7.  
  8.  
  9. #include <iostream>
  10. #include <stdlib.h>
  11.  
  12. using namespace std;
  13.  
  14. struct nodo{
  15.  
  16.        int nro;
  17.  
  18.        struct nodo *sgte;
  19.  
  20. };
  21.  
  22.  
  23.  
  24. typedef struct nodo *Tlista;
  25.  
  26.  
  27. /*-------------------- Insertar siguiente Elemento-------------------------*/
  28. void insertarSgte(Tlista &lista, int valor)
  29.  
  30. {
  31.  
  32.     Tlista t, q = new(struct nodo);
  33.  
  34.     q->nro  = valor;
  35.  
  36.     q->sgte = NULL;
  37.  
  38.     if(lista==NULL)
  39.  
  40.     {
  41.         lista = q;
  42.     }
  43.  
  44.     else
  45.  
  46.     {
  47.         t = lista;
  48.         while(t->sgte!=NULL)
  49.         {
  50.             t = t->sgte;
  51.         }
  52.  
  53.         t->sgte = q;
  54.     }
  55.  
  56. }
  57.  
  58. /*----------------------Mostrar Lista--------------------------------------*/
  59. void reportarLista(Tlista lista)
  60.  
  61. {
  62.  
  63.      int i = 0;
  64.  
  65.      while(lista != NULL)
  66.  
  67.      {
  68.  
  69.           cout <<' '<< i+1 <<") " << lista->nro << endl;
  70.  
  71.           lista = lista->sgte;
  72.  
  73.           i++;
  74.  
  75.      }
  76.  
  77. }
  78.  
  79.  
  80. void calcularMayMenProm(Tlista lista, int mayor, int menor, int promedio, int n){
  81.  
  82.     while(lista!=NULL){
  83.  
  84.      if(mayor<(lista->nro))
  85.  
  86.         mayor=lista->nro;
  87.  
  88.      if(menor>(lista->nro))
  89.  
  90.         menor=lista->nro;
  91.  
  92.      promedio+=lista->nro;
  93.  
  94.      lista=lista->sgte;
  95.      }
  96.  
  97.      promedio=promedio/n;
  98.  
  99.      cout<<endl<<"mayor:"<<mayor<<endl;
  100.  
  101.      cout<<endl<<"menor:"<<menor<<endl;
  102.  
  103.      cout<<endl<<"promedio:"<<promedio<<endl<<endl;
  104.  
  105. }
  106.  
  107. /*------------------------- Funcion Principal ---------------------------*/
  108.  
  109. int main(void)
  110.  
  111. {
  112.  
  113.     Tlista lista = NULL;
  114.  
  115.     system("color 0a");
  116.  
  117.     cout<<"\n\n\t\t[  EJERCICIOS LISTAS SIMPLES ]\n";
  118.  
  119.     cout<<"\t\t-----------------------------\n\n";
  120.  
  121.     cout<<" EJERCICIO 1: Calcular mayor,menor y promedio de una lista"<<endl<<endl;
  122.  
  123.     cout<<"\n Ingrese tamanio de lista: ";
  124.  
  125.     cin>>n;
  126.  
  127.     for(int i=1;i<=n;i++){
  128.  
  129.         insertarSgte(lista,i);
  130.     }
  131.     cout<<endl<<"Elementos de lista"<<endl;
  132.     reportarLista(lista);
  133.  
  134.     mayor=lista->nro;
  135.  
  136.     menor=lista->nro;
  137.  
  138.     promedio=lista->nro;
  139.  
  140.     lista=lista->sgte;
  141.  
  142.     calcularMayMenProm(lista, mayor, menor, promedio, n);
  143.  
  144.  
  145.     system("pause");
  146.  
  147.    return 0;
  148.  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement