Advertisement
ferseg

lista de reales ordenados

Oct 13th, 2014
20,144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.76 KB | None | 0 0
  1. /*
  2.     Autor: Joel Fernandez
  3.     Fecha: 11/10/14
  4.     Tema : Listas Enlazadas Simples
  5.     Ejercicio 2 : Almacenar una lista de reales y reportarlos en forma ordenada.
  6.  
  7. */
  8.  
  9. #include <iostream>
  10. #include <stdlib.h>
  11.  
  12. using namespace std;
  13.  
  14. struct nodo{
  15.  
  16.        float nro;
  17.  
  18.        struct nodo *sgte;
  19.  
  20. };
  21.  
  22. typedef struct nodo *Tlista;
  23.  
  24. Tlista fin;
  25.  
  26. /*------------------Menu----------------------------------*/
  27. void menu1()
  28.  
  29. {
  30.  
  31.     cout<<"\n\n\t\t[  EJERCICIOS LISTAS SIMPLES ]\n";
  32.  
  33.     cout<<"\t\t-----------------------------\n\n";
  34.  
  35.     cout<<" EJERCICIO 2: Almacenar una lista de reales y ordenarlos"<<endl<<endl;
  36.  
  37.     cout<<" 1. INSERTAR ELEMENTO                "<<endl;
  38.  
  39.     cout<<" 2. MOSTRAR LISTA ORDENADA           "<<endl;
  40.  
  41.     cout<<" 3. SALIR                            "<<endl;
  42.  
  43.  
  44.  
  45.     cout<<"\n INGRESE OPCION: ";
  46.  
  47. }
  48. /*----------------------------insertar elemento al inicio ---------------------*/
  49.  
  50. void insertarInicio(Tlista &lista, float valor)
  51. {
  52.  
  53.     Tlista q;
  54.     q = new(struct nodo);
  55.  
  56.     q->nro = valor;
  57.  
  58.     q->sgte = lista;
  59.  
  60.     lista  = q;
  61.  
  62. }
  63.  
  64. /*-------------------- Insertar siguiente Elemento-------------------------*/
  65. void insertarFinal(Tlista &lista, float valor)
  66.  
  67. {
  68.  
  69.     Tlista t, q = new(struct nodo);
  70.  
  71.     q->nro  = valor;
  72.  
  73.     q->sgte = NULL;
  74.  
  75.     fin=q;
  76.  
  77.     if(lista==NULL)
  78.  
  79.     {
  80.         lista = q;
  81.     }
  82.  
  83.     else
  84.  
  85.     {
  86.         t = lista;
  87.         while(t->sgte!=NULL)
  88.         {
  89.             t = t->sgte;
  90.         }
  91.  
  92.         t->sgte = q;
  93.     }
  94.  
  95. }
  96.  
  97. /*------------------------- Funcio que inserta el elemento ordenado antes del mayor y despues del menor*/
  98. void insertarElementoEn(Tlista lista, float n){
  99.  
  100.     Tlista t,r,q=new (struct nodo);
  101.  
  102.     q->nro=n;
  103.  
  104.     q->sgte=NULL;
  105.  
  106.     while(lista->sgte!=NULL){
  107.  
  108.             t=lista->sgte;
  109.  
  110.             if((n>=(lista->nro))&&(n<=t->nro)){
  111.  
  112.                 q->sgte=lista->sgte;
  113.  
  114.                 lista->sgte=q;
  115.  
  116.                 return;
  117.  
  118.                 }
  119.  
  120.             lista=lista->sgte;
  121.  
  122.             }
  123.  
  124. }
  125. /*----------------------Mostrar Lista--------------------------------------*/
  126. void reportarLista(Tlista lista)
  127.  
  128. {
  129.  
  130.      int i = 0;
  131.  
  132.      while(lista != NULL)
  133.  
  134.      {
  135.  
  136.           cout <<' '<< i+1 <<") " << lista->nro << endl;
  137.  
  138.           lista = lista->sgte;
  139.  
  140.           i++;
  141.  
  142.      }
  143.  
  144. }
  145.  
  146.  
  147.  
  148. /*------------------------- Funcion Principal ---------------------------*/
  149.  
  150. int main(void)
  151.  
  152. {
  153.     Tlista lista = NULL;
  154.  
  155.     int op;
  156.  
  157.     float n;
  158.  
  159.     system("color 0a");
  160.  
  161.     do
  162.  
  163.     {
  164.  
  165.         menu1();  cin>> op;
  166.  
  167.          switch(op)
  168.  
  169.         {
  170.  
  171.             case 1:
  172.  
  173.                   cout<< "\n NUMERO A INSERTAR: "; cin>> n;
  174.  
  175.                   if(lista==NULL){
  176.  
  177.                     insertarFinal(lista,n);
  178.  
  179.                     }
  180.                     else{
  181.                             if(n<lista->nro)
  182.  
  183.                                 insertarInicio(lista,n);
  184.  
  185.                             else if(n>fin->nro)
  186.  
  187.                                     insertarFinal(lista,n);
  188.  
  189.                                   else{ if((n>lista->nro)&&(n<fin->nro)){
  190.  
  191.                                             insertarElementoEn(lista,n);
  192.  
  193.                                             }
  194.  
  195.                                   }
  196.  
  197.  
  198.                     }
  199.  
  200.                   break;
  201.  
  202.  
  203.             case 2:
  204.  
  205.                   cout<<endl<<"La lista Ordenada es:"<<endl;
  206.  
  207.                   reportarLista(lista);
  208.  
  209.                   break;
  210.  
  211.             case 3: return 0;
  212.  
  213.             default: cout<<"Ingrese una Opcion Valida....!"<<endl;
  214.  
  215.  
  216.                     }
  217.  
  218.  
  219.  
  220.         cout<<endl<<endl;
  221.  
  222.         system("pause");  system("cls");
  223.  
  224.  
  225.  
  226.     }while(op!=3);
  227.  
  228.  
  229.  
  230.     system("pause");
  231.  
  232.    return 0;
  233.  
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement