listas

LISTAS EN C++
  1. #include<conio.h>
    #include<stdio.h>
    #include<stdlib.h>
    #include<iostream.h>

    struct nodo_lista
        {
          int dato;
          nodo_lista *sgte;
        };

    typedef nodo_lista *nodo;

    nodo ReservarMemoria();
    nodo CrearNodo(int);
    nodo InsertarInicio(nodo, int);
    nodo InsertarPosicion(nodo, int,int);
    nodo InsertarFinal(nodo, int);
    nodo Insertar(nodo);
    int Longitud(nodo);
    nodo Inicializar(nodo);
    void Mostrar(nodo);
    int Existe(nodo,int);
    nodo Eliminar(nodo,int);
    nodo Formalizar(nodo);
    void SumaElementos(nodo);
    nodo Invertir(nodo);
    void MostrarInversa(nodo);
    nodo EliminarNumero(nodo,int);
    void MostrarElemento(nodo,int);
    nodo IntercambiarNodo(nodo,int);
    nodo ParRepetidos(nodo,int);
    Menu(int);
    menu(int);

    void main()
    {
     int opcion,num=0,opcion1,elem;
     nodo lista=NULL;
     do
     {
     opcion=Menu(num);
     switch(opcion)
     {

        case 1:lista=Insertar(lista);
            break;

        case 2:Mostrar(lista);
            getch();
            break;

        case 3:printf("\nLa Longitud es :%d",Longitud(lista));
            getch();
            break;

        case 4:lista=Inicializar(lista);
            break;

        case 5:SumaElementos(lista);
            break;
           case 6:MostrarInversa(lista);
            getch();
            break;
           case 7:printf("Que numero deseas Eleminar?");scanf("%d",&elem);
            lista=EliminarNumero(lista,elem);
             getch();
              break;
          case 8:printf("Ingrese numero para Buscar");scanf("%d",&elem);
             MostrarElemento(lista,elem);
             getch();
               break;
          case 9:printf("Ingrese el numero ");scanf("%d",&elem);
             lista=IntercambiarNodo(lista,elem);
             getch();
               break;
        case 10:printf("Ingrese el numero ");scanf("%d",&elem);
             lista=ParRepetidos(lista,elem);
             getch();
               break;

       case 11:lista=Inicializar(lista);
              free(lista);
             printf ("F I N");
             getch();
             break;
         }
         }while (opcion!=11);
     }

    Menu (int num)
    {
         clrscr();
         gotoxy (29, 7);
           textcolor(3);
         cprintf (" M E N U");
         gotoxy (11,8);
      printf(" ÉÍ[þ]ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍfffffÍÍÍ»\n\
           º   [1]   Insertar Datos                       º\n\
           º   [2]   Mostrar Lista                        º\n\
           º   [3]   Longitud Lista                       º\n\
           º   [4]   Inicializar Lista                    º\n\
           º   [5]   Suma Elementos                       º\n\
           º   [6]  Mostrar Lista Inversa                 º\n\
           º   [7]  Eliminar Numero                       º\n\
           º   [8]  Buscar Elemento                       º\n\
           º   [9]  Intercambiar Nodos                    º\n\
           º   [10] Eliminar ParRepetidos   ÉÄÄÄÄÄÄÄÄ»    º\n\
           º   [11] Salir                   ³  ³    º\n\
           ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÛ±±±°°²°°±±±ÛÍͼ\n\n");

        printf ("Que opcion quieres:");
        scanf ("%d", &num);
        clrscr();
     return (num);
    }
    nodo ReservarMemoria()
    {
     nodo nuevo;
     nuevo=(nodo)malloc (sizeof(struct nodo_lista));
     return (nuevo);
    }

    nodo CrearNodo(int elem)
    {
      nodo nuevo;
      nuevo=ReservarMemoria();
      nuevo->dato=elem;
      nuevo->sgte=NULL;
      return nuevo;
    }

    nodo InsertarInicio(nodo Lista, int elem)
    {
     nodo nuevo;
     nuevo=CrearNodo(elem);
     if (Lista==NULL)
         Lista=nuevo;
     else
     {
      nuevo->sgte=Lista;
      Lista=nuevo;
     }
    return (Lista);
    }


    nodo InsertarPosicion(nodo Lista, int elem,int posicion)
    {
      int i;
      nodo nuevo,aux;
      nuevo=CrearNodo(elem);
      if(posicion==1)
          Lista=InsertarInicio(Lista,elem);
      else
        {
         i=2;
         aux=Lista;
          while(i!=posicion)
            {
        i++;
        aux=aux->sgte;
            }
             nuevo->sgte=aux->sgte;
             aux->sgte=nuevo;
         }
      return(Lista);
    }



    nodo InsertarFinal(nodo Lista, int elem)
    {
     nodo nuevo,aux;
     nuevo=CrearNodo(elem);
     if(Lista==NULL)
          Lista=nuevo;
      else
         {
          aux=Lista;
            while (aux->sgte!=NULL)
         aux=aux->sgte;
            aux->sgte=nuevo;
         }
     return (Lista);
    }



    nodo Insertar(nodo Lista)
      {
        int opcion2, elem,posicion,longitud;
        longitud=Longitud(Lista);
         do
          {
            clrscr();
            printf ("\nComo quieres insertar los datos:\n\n");
            printf ("\n[1]Al Inicio ---- [2]Posicion----[3]Al Final\n");
            printf ("\nTeclea tu opcion:");
            scanf ("%d", &opcion2);
          } while (opcion2>3 || opcion2<=0);

            printf ("Dame el elemento a insertar:");
             scanf("%d", &elem);
             if(opcion2==1)
            Lista=InsertarInicio(Lista, elem);
             if(opcion2==2)
         {
            printf("Ingrese la posicion a la que quiere Insertar");
             scanf("%d",&posicion);
            if(posicion==1)
              Lista=InsertarInicio(Lista,elem);
            if(posicion==longitud+1)
                     Lista=InsertarFinal(Lista,elem);
            if((posicion>1)&&(posicion<=longitud))
              Lista=InsertarPosicion(Lista,elem,posicion);
            else
             printf("\nLa Posicion no es Valida");
                 }
             if(opcion2==3)
            Lista=InsertarFinal(Lista, elem);
      return (Lista);
     }

    int Longitud(nodo Lista)
      {
        int i;
        if(Lista==NULL)
          return 0;
         else
          {
            i=1;
            while(Lista->sgte!=NULL)
             {
        i++;
        Lista=Lista->sgte;
             }
             return i;
            }
        }

    nodo Inicializar(nodo Lista)
    {
     nodo aux;
     aux=Lista;
     while (aux!=NULL)
     {
      Lista=Lista->sgte;
      free(aux);
      aux=Lista;
     }
     return (aux);
    }


    void Mostrar(nodo Lista)
    {
      if (Lista==NULL)
        printf ("\nLa lista esta vacia");
      else
        {
         while(Lista != NULL)
            {
        printf("³%d³->", Lista->dato);
         Lista=Lista->sgte;
            }
        }
     }

    int Existe(nodo Lista,int num)
        {
         int r=0;
         if(Lista==NULL)
             printf("Lista Vacia");
         else
            {
             while(Lista!=NULL)
                {
          if(Lista->dato==num)
             {
              r=1;
              Lista=NULL;
             }
            else
              Lista=Lista->sgte;
          }
            return r;
             }
             return r;
         }


    nodo Eliminar(nodo Lista,int num)
       {
         int i=0;
         nodo aux=Lista, temp=NULL;
         if(Lista==NULL)
        printf ("No se puede Lista Vacia");
         else
          {
          while((aux!=NULL) && (i==0))      {
        i=(aux->dato==num);
        if(i==0)
           {
         temp=aux;
         aux=aux->sgte;
        }
          }
        if(aux!=NULL)
         {
         if(aux==Lista)
          Lista=aux->sgte;
         else
          temp->sgte=aux->sgte;
         free(aux);
         }
        }
    return(Lista);
     }


    nodo Formalizar(nodo Lista)
        {
         nodo aux,temp;
         if(Lista==NULL)
             printf("Lista Vacia Ingrese Datos");
         else
            {
             aux=Lista;
             while(aux!=NULL)
         {
          temp=aux;
          temp=temp->sgte;
            while(temp!=NULL)
              {
                if(Existe(temp,aux->dato)==1)
            {
             Lista=Eliminar(Lista,aux->dato);
             temp=temp->sgte;
             }
                 else
             temp=temp->sgte;
                 }
              aux=aux->sgte;
            }
         return Lista;
             }
         return Lista;
      }


    void SumaElementos(nodo Lista)
       {
        nodo aux;
        int i=0;
        if(Lista==NULL)
           printf("Lista Vacia");
       else
         {aux=Lista;
         while(aux!=NULL)
           {
        i=i+aux->dato;
        aux=aux->sgte;
           }
          printf("La suma es :%d",i);
         }
         getch();
      }

     nodo Invertir(nodo Lista)
        {
         int x;
         nodo aux=Lista,nuevo=NULL;
          if(aux==NULL)
        printf("La lista vacia");
         else
           {
        while(aux!=NULL)
         {
          x=aux->dato;
          nuevo=InsertarInicio(nuevo,x);
          aux=aux->sgte;
         }
         aux=Inicializar(aux);
          free(aux);
          return(nuevo);
           }
        }

     void MostrarInversa(nodo Lista)
        {
         nodo aux;
        if(Lista==NULL)
        printf ("\nLa lista esta vacia");
        else
          { aux=Invertir(Lista);
           while(aux != NULL)
         {
          printf("³%d³->", aux->dato);
          aux=aux->sgte;
         }
          }
     }

    nodo EliminarNumero(nodo Lista,int num)
        {
         nodo temp;
         if(Lista==NULL)
             printf("Lista Vacia Ingrese Datos");
         else
          {
           temp=Lista;
           while(temp!=NULL)
             {
             if(Existe(Lista,num)==1)
               {
             Lista=Eliminar(Lista,num);
             temp=temp->sgte;
               }
             else
             temp=temp->sgte;
           }
         return Lista;
             }
         return Lista;
      }

    void MostrarElemento(nodo Lista,int num)
       { int i=0;
        nodo temp;
        if(Lista==NULL)
          printf("El elemento %d no Existe",num);
        else
         {
          temp=Lista;
          while(temp!=NULL)
        {
         if(temp->dato==num)
           {
            i=i+1;
            temp=temp->sgte;
           }
         else
           temp=temp->sgte;
         }
          printf("El elemento %d Existe %d veces",num,i);
         }
      }

    nodo IntercambiarNodo(nodo Lista,int num)
       {
        int temp;
        nodo aux;
        if(Lista==NULL)
          printf("Lista vacia");
        else
         {
         aux=Lista;
         while(aux->dato!=num)
           {
        aux=aux->sgte;
           }
           if(aux->dato==num)
        {
        temp=aux->dato;
        aux->dato=aux->sgte->dato;
        aux->sgte->dato=temp;
        }
           return Lista;
         }
        return Lista;
       }

     nodo ParRepetidos(nodo Lista,int num)
       { int i=0;
         nodo temp;
        if(Lista==NULL)
          printf("El elemento no Existe");
        else
         {
          temp=Lista;
          while(temp!=NULL)
        {
         if(temp->dato==num)
           {
            i++;
            temp=temp->sgte;
           }
         else
           temp=temp->sgte;
        }
        if(i%2==0&&i!=0)
        { temp=Lista;
         while(temp!=NULL)
          {
            Lista=Eliminar(Lista,num);
            temp=temp->sgte;
            }
          }
         }
           }
  2. #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    struct nodo_lista
      {
         int dato;
         nodo_lista *sgte;
      };
    typedef struct nodo_lista *nodo;


    nodo ReservarMemoria();
    nodo CrearNodo(int elem, nodo enlace);
    nodo InsertarInicio(nodo, int);
    nodo InsertarPosicion(nodo, int,int);
    nodo InsertarFinal(nodo, int);
    nodo Insertar(nodo);
    int Longitud(nodo);
    nodo InicializaLista(nodo);
    void Mostrar(nodo Lista);

    Menu(int);
    menu(int);
    nodo Concatenar(nodo, nodo, nodo);
    nodo EliminarPosicion(nodo);
    nodo EliminarDato(nodo);
    void Consultar(nodo);
    void Modificar (nodo);
    nodo Invertir (nodo);


    void main()
    {
     int opcion, opcion2, opcion3, num=0;
     nodo lista1=NULL;
     nodo lista2=NULL;
     nodo lista3=NULL;
     do
     {
     opcion=Menu(num);
     switch(opcion)
     {
      case 1:
                opcion2=menu(num);
                if (opcion2==1)
            lista1=InicializaLista(lista1);
                if (opcion2==2)
            lista2=InicializaLista(lista2);
                if (opcion2==3)
            lista3=InicializaLista(lista3);
                break;
        case 2:
                 opcion2=menu(num);
                 if (opcion2==1)
                         lista1=Insertar(lista1);
                 if (opcion2==2)
                         lista2=Insertar(lista2);
                 if (opcion2==3)
                         lista3=Insertar(lista3);
                 break;
        case 3:
                 opcion2=menu(num);
                 if (opcion2==1)
                        printf("\nLa Longitud es :%d",Longitud(lista1));

                 if (opcion2==2)
                        printf("\nLa Longitud es :%d",Longitud(lista2));

                 if (opcion2==3)
                        printf("\nLa Longitud es :%d",Longitud(lista3));
                        getch();
                  break;
        case 4:
                opcion2=menu(num);
                if (opcion2==1)
                         Mostrar (lista1);
                if (opcion2==2)
                         Mostrar (lista2);
                if (opcion2==3)
                         Mostrar (lista3);
                 break;
         case 5:
                 opcion2=menu(num);
                 if (opcion2==1)
            lista1=EliminarPosicion(lista1);
                 if (opcion2==2)
            lista2=EliminarPosicion(lista2);
                 if (opcion2==3)
            lista3=EliminarPosicion(lista3);
                 break;
         case 6:
                 opcion2=menu(num);
                 if (opcion2==1)
                         Modificar(lista1);
                 if (opcion2==2)
                         Modificar(lista2);
                 if (opcion2==3)
                         Modificar(lista3);
                 break;
        case 7:
                 opcion2=menu(num);
                 if (opcion2==1)
                         lista1=EliminarDato(lista1);
                 if (opcion2==2)
                         lista2=EliminarDato(lista2);
                 if (opcion2==3)
                         lista3=EliminarDato(lista3);
                 break;
        case 8:
                 opcion2=menu(num);
                 if (opcion2==1)
                         Consultar(lista1);
                 if (opcion2==2)
                         Consultar(lista2);
                 if (opcion2==3)
                         Consultar(lista3);
                 break;
      case 9:
                 opcion2=menu(num);
                 if (opcion2==1)
                         lista1=Invertir(lista1);
                 if (opcion2==2)
                         lista2=Invertir(lista2);
                 if (opcion2==3)
                         lista3=Invertir(lista3);
                 break;
     case 10:
                getch();
                do{
                clrscr();
                printf ("OPCIONES\n\n");
                printf ("1)LISTA1+LISTA2=LISTA3\n2)LISTA2+LISTA3=LISTA1\n3)LISTA3+LISTA1=LISTA2");
                printf ("\nCual opcion quieres:");
                scanf ("%d", &opcion3);
                }while (opcion3>3 || opcion3<=0);
                if (opcion3==1)
                         lista3=Concatenar(lista1,lista2,lista3);
                if (opcion3==2)
                         lista1=Concatenar(lista2, lista3,lista1);
                if (opcion3==3)
                         lista2=Concatenar(lista3,lista1,lista2);
                 break;
    case 11:
                 lista1=InicializaLista(lista1);
                 lista2=InicializaLista (lista2);
                 lista3=InicializaLista(lista3);
                 free (lista1);
                 free (lista2);
                 free (lista3);
                 printf ("F I N");
                 getch();
                 break;
         }//fin del switch
     }while (opcion!=11);
    }

    Menu (int num)
    {
         clrscr();
         gotoxy (36, 1);
         printf (" M E N U");
         gotoxy (19,2);
    printf("_______________________________________________\n\
            |•|   [1]   Inicializar Lista                   |•|\n\
            |•|   [2]   Insertar Datos                      |•|\n\
            |•|   [3]   Longitud Lista                      |•|\n\
            |•|   [4]   Mostrar Lista                       |•|\n\
            |•|   [5]   Eliminar el i-esimo Elemento        |•|\n\
            |•|   [6]   Modificar el i-esimo Elemento       |•|\n\
            |•|   [7]   Eliminar la Primera Ocurrencia      |•|\n\
            |•|   [8]   Consultar un Elemento               |•|\n\
            |•|   [9]   Invertir Lista                      |•|\n\
            |•|   [10]   Concatenar 2 Listas                |•|\n\
            |•|   [11]  Salir                               |•|\n\
            |•|_____________________________________________|•|\n\n");
        printf ("Que opcion quieres:");
        scanf ("%d", &num);
        clrscr();
     return (num);
    }



    menu(int num)
    {
     do
      {
        clrscr();
        printf ("ESCOGE QUE LISTA\n");
        printf ("\n    LISTA [1]\n    LISTA [2]\n    LISTA [3]\n");
        printf ("\nCual Lista quieres:");
        scanf ("%d", &num);
      }while(num>3 || num<=0);
     return (num);
    }

    nodo ReservarMemoria()
    {
     nodo nuevo;
     nuevo=(nodo)malloc (sizeof(struct nodo_lista));
     return (nuevo);
    }

    nodo CrearNodo(int elem, nodo enlace)
    {
     nodo nuevo;
     nuevo=ReservarMemoria();
     nuevo->dato=elem;
     nuevo->sgte=enlace;
     return (nuevo);
    }

    nodo InsertarInicio(nodo Lista, int elem)
    {
     nodo nuevo;
     nuevo=CrearNodo(elem, NULL);
     if (Lista==NULL)
            Lista=nuevo;
     else
     {
         nuevo->sgte=Lista;
         Lista=nuevo;
         }
    return (Lista);
    }


    nodo InsertarPosicion(nodo Lista, int elem,int posicion)
    {
        int i;
      nodo nuevo,aux;
        nuevo=CrearNodo(elem,NULL);
      if(posicion==1)
         Lista=InsertarInicio(Lista,elem);
      else
         {
             i=2;
             aux=Lista;
            while(i!=posicion)
              {
                i++;
                aux=aux->sgte;
              }
             nuevo->sgte=aux->sgte;
             aux->sgte=nuevo;
         }
      return(Lista);
    }



    nodo InsertarFinal(nodo Lista, int elem)
    {
     nodo nuevo,aux;
      nuevo=CrearNodo(elem,NULL);
     if (Lista==NULL)
              Lista=nuevo;
      else
        {    aux=Lista;
          while (aux->sgte!=NULL)
                aux=aux->sgte;
          aux->sgte=nuevo;
         }
     return (Lista);
    }



    nodo Insertar(nodo Lista)
    {
        int n, i, opcion2, elem,posicion,longitud;
         longitud=Longitud(Lista);
        //printf ("Cuantos datos vas a insertar:");
      //    scanf ("%d", &n);
        do {
        clrscr();
        printf ("\nComo quieres insertar los datos:\n\n");
        printf ("\n[1]Al Inicio ---- [2]Posicion----[3]Al Final\n");
        printf ("\nTeclea tu opcion:");
        scanf ("%d", &opcion2);
        } while (opcion2>3 || opcion2<=0);
      //    for (i=0; i<n; i++)
      //    {
             printf ("Dame el elemento a insertar:");
             scanf("%d", &elem);
             if (opcion2==1)
                  Lista=InsertarInicio(Lista, elem);
             if (opcion2==2)
             { printf("Ingrese la posicion a la que quiere Insertar");
                scanf("%d",&posicion);
                if(posicion<=longitud+1)
                     Lista=InsertarPosicion(Lista,elem,posicion);
                else
                    printf("\nLa Posicion ingresada  es mayor que Longitud de Lista");

                getch();
            //    break;
             }
             if (opcion2==3)
                  Lista=InsertarFinal(Lista, elem);
      //    }//fin del for
    return (Lista);
    }//fin de la funcion


    int Longitud(nodo Lista)
        {
           int i;
         if(Lista==NULL)
            return 0;
         else {
               i=1;
                 while(Lista->sgte!=NULL)
                    {
                     i++;
                          Lista=Lista->sgte;
                    }
          return i;
            }
         getch();

         }




    nodo InicializaLista(nodo Lista)
    {
     nodo aux;
     aux=Lista;
     while (aux!=NULL)
     {
      Lista=Lista->sgte;
      free (aux);
      aux=Lista;
     }
     return (aux);
    }



    void Mostrar(nodo Lista)
    {
      if (Lista==NULL)
      printf ("\nLa lista esta vacia");
      printf("\n\n");
     while (Lista != NULL)
         {
            printf("|%d|->", Lista->dato);
           Lista=Lista->sgte;
         }
         getch();
    }

    nodo Concatenar(nodo A, nodo B, nodo C)
    {

     C=InicializaLista(C);

     if (A==NULL || B==NULL)
     {
          printf ("NO SE PUEDE PORQUE LA(S) LISTA(S) ESTA(N) VACIA(S)");
          getch();
     }
     else
      {
        while (A!=NULL)
        {
         C=InsertarFinal(C,A->dato);
         A=A->sgte;
        }

      while(B!=NULL)
        {
            C=InsertarFinal(C,B->dato);
            B=B->sgte;
        }
      }//fin del else
        A=InicializaLista(A);
        B=InicializaLista(B);
     return(C);
     }




     nodo EliminarPosicion(nodo Lista)
    {
     nodo aux,nuevo;
     int num, i;
     aux=Lista;
     if (Lista==NULL)
     {
      printf ("NO SE PUEDE LA LISTA ESTA VACIA");
      getch();
     }
     else {
     printf("Dame la posicion que quieres eliminar:");
     scanf ("%d", &num);
     if (num==1)
     {
         Lista=Lista->sgte;
        free (aux);
     }
     else
     {
      num--;
      for(i=1; i<num; i++)
      {
         aux=aux->sgte;
              if (aux==NULL)
              {
              printf ("No se puede no hay datos en esa posicion");
              getch();
              break;
              }
     if (aux!=NULL){
     nuevo=aux;
     nuevo=nuevo->sgte;
     aux->sgte=nuevo->sgte;
     free (nuevo);
     }//fin del if
     }//fin del else
     }
     }
     return(Lista);
     }


    nodo EliminarDato(nodo Lista)
    {
     nodo actual, anterior;
     int num, encontrado=0;
     actual=Lista;
     anterior=NULL;
     if (Lista==NULL)
     {
       printf ("NO SE PUEDE LA LISTA ESTA VACIA");
       getch();
     }
     else {
     printf("Dame el numero que quieres eliminar:");
     scanf ("%d", &num);
     while ((actual!=NULL) && (!encontrado))
     {
            encontrado=(actual->dato==num);
            if(!encontrado)
            {
             anterior=actual;
             actual=actual->sgte;
            }
     }
     if (actual!=NULL)
     {
      if (actual==Lista)
          Lista=actual->sgte;
      else
         anterior->sgte=actual->sgte;
      free(actual);
     }
     }//fin del else
     return(Lista);
    }



    void Consultar(nodo Lista)
    {
     nodo aux;
     int num, cont=1, num2;
     aux=Lista;
     if (aux==NULL)
     {
       printf ("NO SE PUEDE LA LISTA ESTA VACIA");
       getch();
     }
     else {
     printf("Dame el dato que quieres consultar:");
     scanf ("%d", &num);
     while (aux->dato!=num)
     {
        cont++;
        aux=aux->sgte;
        if (aux==NULL)
        {
           printf ("NUMERO NO ENCONTRADO");
           getch();
           break;
        }
     }
     aux=aux->sgte;
     printf ("El dato esta en la posicion: %d", cont);
     if (aux!=NULL)
     {
        num2=aux->dato;
        printf("\nEl numero que esta despues  es: %d", num2);
     }//fin del if
     }//fin del else
     getch();
     }

     void Modificar (nodo Lista)
     {
      nodo aux;
     int num, i, elem;
     aux=Lista;
     if (aux==NULL)
     {
       printf ("NO SE PUEDE LA LISTA ESTA VACIA");
       getch();
     }
     else {
     printf("Dame la posicion que quieres modificar:");
     scanf ("%d", &num);
     printf ("Cual es el nuevo dato que quieres:");
     scanf ("%d", &elem);
     if (num==1)
        aux->dato=elem;
     else
     {
      for(i=1; i<num; i++)
         aux=aux->sgte;
     aux->dato=elem;
      }//fin del else
     }//fin del else
    }

    nodo Invertir (nodo Lista)
    {
       int x;
       nodo aux, nuevo=NULL;
        aux=Lista;
        if(aux==NULL)
        printf ("La lista esta vacia");
        getch();
       while (aux!=NULL)
       {
          x=aux->dato;
          nuevo=InsertarInicio(nuevo, x);
          aux=aux->sgte;
       }
        Lista=InicializaLista(Lista);
        aux=InicializaLista(aux);
        free(aux);
        free(Lista);
     return (nuevo);
     }
  3. #include<stdio.h>
    #include<conio.h>
    #include<iostream.h>
    #include<stdlib.h>
    #include<graphics.h>
    char *Titulos[]={ " Insertar Datos",
              " Mostrar Lista",
              " Longitud de Lista",
              " Invertir Lista",
              " Eliminar Numero",
              " Formalizar Lista",
              " Mayor Elemento",
              " Intercambiar Nodo",
              " Vaciar(Lista)",
              " Salir "};

    struct nodo_lista
        {
          int dato;
          nodo_lista *sgte;
        };

    typedef nodo_lista *nodo;

    nodo ReservarMemoria();
    nodo CrearNodo(int);
    nodo InsertarInicio(nodo, int);
    nodo InsertarPosicion(nodo, int,int);
    nodo InsertarFinal(nodo, int);
    nodo Insertar(nodo);
    int Longitud(nodo);
    void Mostrar(nodo);

    void ElementoMayor(nodo);
    nodo IntercambiarNodo(nodo,int);
    nodo Invertir(nodo);
    nodo Eliminar_N(nodo,int);
    nodo Formalizar(nodo);
    int Existe(nodo,int);
    nodo Vaciar(nodo);


    void cmarco(int,int,int,int);
    void cmarco_aux(int,int,int,int);
    void cmarco_temp(int,int,int,int);
    nodo lista=NULL;

    void cmarco(int x1,int x2,int y1,int y2)
    {
        int CONT1,CONT2;
        for (CONT1=x1; CONT1<x2; CONT1++)
        {
            gotoxy(CONT1,y1); cout<<"Í";
            gotoxy(CONT1,y2); cout<<"Í";
        }
        for (CONT2=y1; CONT2<y2; CONT2++)
        {
            gotoxy(x1,CONT2); cout<<"º";
            gotoxy(x2,CONT2); cout<<"º";
        }
        gotoxy(x1,y1); cout<<"É";
        gotoxy(x2,y1); cout<<"»";
        gotoxy(x1,y2); cout<<"È";
        gotoxy(x2,y2); cout<<"¼";
    }
    void cmarco_aux(int x1,int x2,int y1,int y2)
    {
        int CONT1,CONT2;
        for (CONT1=x1; CONT1<x2; CONT1++)
        {
            gotoxy(CONT1,y1); cout<<"Ä";
            gotoxy(CONT1,y2); cout<<"Ä";
        }
        for (CONT2=y1; CONT2<y2; CONT2++)
        {
            gotoxy(x1,CONT2); cout<<"³";
            gotoxy(x2,CONT2); cout<<"³";
        }
        gotoxy(x1,y1); cout<<"Ú";
        gotoxy(x2,y1); cout<<"¿";
        gotoxy(x1,y2); cout<<"À";
        gotoxy(x2,y2); cout<<"Ù";
    }
    void cmarco_temp(int x1,int x2,int y1,int y2)
    {
        int CONT1,CONT2;
        for (CONT1=x1; CONT1<x2; CONT1++)
        {
            gotoxy(CONT1,y1); cout<<"±";
            gotoxy(CONT1,y2); cout<<"±";
        }
        for (CONT2=y1; CONT2<y2; CONT2++)
        {
            gotoxy(x1,CONT2); cout<<"±";
            gotoxy(x2,CONT2); cout<<"±";
        }
        gotoxy(x1,y1); cout<<"±";
        gotoxy(x2,y1); cout<<"±";
        gotoxy(x1,y2); cout<<"±";
        gotoxy(x2,y2); cout<<"±";
    }




    void Resalta(int Y)
    {
      textcolor(RED);
      textbackground(7);
      gotoxy(3,3+Y); cprintf("%s",Titulos[Y-1]);
      textcolor(BLUE);
      textbackground(BLACK);
    }
    void Ejecuta(int Opc)
    {
      int elem;
      switch(Opc)
      {

        case 1:lista=Insertar(lista);getch();break;
        case 2:Mostrar(lista);getch();break;
        case 3:gotoxy(32,2);printf("Longitud de Lista es:_%d",Longitud(lista));
               getch();break;
        case 4:lista=Invertir(lista);getch();break;
        case 5:gotoxy(32,2);printf("Ingrese el Numero a Eliminar :_");
              scanf("%d",&elem);lista=Eliminar_N(lista,elem);getch();break;
        case 6:lista=Formalizar(lista);lista=Formalizar(lista);getch();break;
        case 7:ElementoMayor(lista);getch();break;
        case 8:gotoxy(32,2);printf("Ingrese numero para Intercambiar :_");
              scanf("%d",&elem);lista=IntercambiarNodo(lista,elem);getch();break;
           case 9:lista=Vaciar(lista);break;
           case 10:lista=Vaciar(lista); free(lista);
            cmarco(44,63,9,11); cmarco_temp(43,64,8,12);
            gotoxy(51,14);printf("F I N");
            ;getch();exit(0);break;
        }
    // getch();
    }


    int main()
    {
      char Opc;
      int Y=1;
      textmode(C80);
      do
       {
         textcolor(WHITE);
         textbackground(1);
         clrscr();
         cmarco(2,29,1,25);
         cmarco(30,79,1,25);
         gotoxy(10,2); cprintf("LISTAS ");
         for(int f=0; f<11; f++)
         {
           gotoxy(3,4+f);  cout <<Titulos[f];
         }

         Resalta(Y);
         Opc=getch();
         switch(Opc)  //claves: 72=>flecha arriba 80=>flecha abajo 13=>Enter.
          {
        case 72: if(Y==1) Y=11; else Y--; break;
        case 80: if(Y==11) Y=1; else Y++; break;
        case 13: Ejecuta(Y);
          }
       }while(1);
    }


    nodo ReservarMemoria()
    {
     nodo nuevo;
     nuevo=(nodo)malloc (sizeof(struct nodo_lista));
     return (nuevo);
    }

    nodo CrearNodo(int elem)
    {
     nodo nuevo;
     nuevo=ReservarMemoria();
     nuevo->dato=elem;
     nuevo->sgte=NULL;
    return nuevo;
    }

    nodo InsertarInicio(nodo Lista, int elem)
    {
     nodo nuevo;
     nuevo=CrearNodo(elem);
     if(Lista==NULL)
       Lista=nuevo;
     else
     { nuevo->sgte=Lista;
      Lista=nuevo;
     }
    return Lista;
    }

    nodo InsertarPosicion(nodo Lista, int elem,int pos)
    { int i=2;
     nodo nuevo,aux;
     nuevo=CrearNodo(elem);
     if(pos==1)
       Lista=InsertarInicio(Lista,elem);
     else if(pos==Longitud(Lista)+1)
       Lista=InsertarFinal(Lista,elem);
     else
      { aux=Lista;
       while(i!=pos)
        { aux=aux->sgte;
         i++;
        }
       nuevo->sgte=aux->sgte;
       aux->sgte=nuevo;
      }
    return Lista;
    }

    nodo InsertarFinal(nodo Lista, int elem)
    {  nodo nuevo,aux;
     nuevo=CrearNodo(elem);
     if(Lista==NULL)
         Lista=nuevo;
      else
       { aux=Lista;
        while(aux->sgte!=NULL)
           aux=aux->sgte;
        aux->sgte=nuevo;
       }
     return Lista;
    }

    nodo Insertar(nodo Lista)
    { int opcion,elem,pos;
      textbackground(2);
     do
      { clrscr();
      printf ("\n         --------- Como quieres Insertar los datos ---------\n\
                                      \n\
          ÉÍ[þ]ÍÍÍÍÍÍÍÍ»     ÉÍ[þ]ÍÍÍÍÍÍÍÍÍÍ»      ÉÍ[þ]ÍÍÍÍÍÍÍÍ» \n\
          º [1] Inicio º     º [2] Posicion º      º [3] Final  º \n\
          ÈÍÍÍÍÍÍÍÍÍÍÍͼ     ÈÍÍÍÍÍÍÍÍÍÍÍÍÍͼ      ÈÍÍÍÍÍÍÍÍÍÍÍͼ \n\n");

      printf ("\n  --:  Teclea tu opcion:_");
      scanf ("%d", &    opcion);
      } while(opcion>3 || opcion<=0);

      printf ("  --:  Dame el elemento a insertar:_");
      scanf("%d", &elem);

      if(opcion==1){
         Lista=InsertarInicio(Lista,elem);
         gotoxy(15,13);printf("! Ya se Insert¢ ­");}
      if(opcion==2)
        { printf("\n  --:  Ingrese la Posicion para Insertar :_");
         scanf("%d",&pos);
         if(pos>=1 && pos<=Longitud(Lista)+1){
        Lista=InsertarPosicion(Lista,elem,pos);
        gotoxy(15,15);printf("! Ya se Insert¢ ­");}
         else
        printf("\n  ....  Posicion fuera de Rango  ....");
         }
      if(opcion==3){
         Lista=InsertarFinal(Lista, elem);
         gotoxy(15,13);printf("! Ya se Insert¢ ­");}
    return Lista;
    }


    int Longitud(nodo Lista)
    { int i;
     if(Lista==NULL)
       return 0;
     else
      { i=1;
       while(Lista->sgte!=NULL)
        { i++;
         Lista=Lista->sgte;
        }
         return i;
      }
    }

    void Mostrar(nodo Lista)
    { int i=0,j=0;
     if(Lista==NULL){
       gotoxy(32,2);printf ("La lista esta vacia");}
     else
       {
        while(Lista!=NULL)
          {
          if(Longitud(Lista)>=9 && Longitud(Lista)%9==0 )
          { i=0; j=j+3;}
           i=i+5;
           cmarco_aux(26+i,28+i,2+j,4+j);
           gotoxy(27+i,3+j);
           printf("%d", Lista->dato);
           gotoxy(29+i,3+j);printf("Ä ");
           Lista=Lista->sgte;
          }
       }
     }
    nodo Eliminar_N(nodo Lista,int num)
    {  int i=0;
      nodo aux=Lista, temp=NULL;
      if(Lista==NULL){
        gotoxy(35,4); printf ("No se puede Lista Vacia");}
      else
       {
       while(aux!=NULL && i==0)
        {    i=(aux->dato==num);
          if(i==0)
           {
        temp=aux;
        aux=aux->sgte;
        }
          }
         if(aux!=NULL)
          {
           if(aux==Lista)
          Lista=aux->sgte;
           else
          temp->sgte=aux->sgte;
           free(aux);
           gotoxy(35,4);printf ("­ Ya se Elimin¢ !");
           }
          else
           {gotoxy(35,4);printf ("­ No est  el N£mero !"); }


       }
    return Lista;
    }

    nodo Eliminar_aux(nodo Lista,int num)
    {  int i=0,c=0;
      nodo aux=Lista, temp=NULL;
      if(Lista==NULL){
        gotoxy(35,4); printf ("No se puede Lista Vacia");}
      else
       {
       while(aux!=NULL && i==0)
        {    i=(aux->dato==num);
          if(i!=0)
           {  c++;
        if(c<=1)
        i=0;
        else
        i=(aux->dato==num);
           }
          if(i==0)
           {
        temp=aux;
        aux=aux->sgte;
        }
          }
         if(aux!=NULL)
          {
           if(aux==Lista)
          Lista=aux->sgte;
           else
          temp->sgte=aux->sgte;
           free(aux);
          }
       }
    return Lista;
    }




    nodo Invertir(nodo Lista)
    { int x;
     nodo aux=Lista,nuevo=NULL;
     if(aux==NULL){
       gotoxy(35,4); printf("No se puede Lista Vacia");}
     else
      {
       while(aux!=NULL)
         { x=aux->dato;
          nuevo=InsertarInicio(nuevo,x);
          aux=aux->sgte;
           gotoxy(35,4); printf("! Ya se Invirti¢ !");
         }
      }
    return nuevo;
    }


    void ElementoMayor(nodo Lista)
    { int mayor;
      nodo aux;
      if(Lista==NULL){
       gotoxy(35,4);printf("No hay Mayor");}
      else
       { mayor=Lista->dato;
         aux=Lista;
        while(aux!=NULL)
         {
          if(aux->dato>mayor)
        { mayor=aux->dato;
         aux=aux->sgte;
        }
          else
         aux=aux->sgte;
         }
       gotoxy(32,2); printf("El mayor Elemento es :%d_  ",mayor);
       }
    }


    nodo IntercambiarNodo(nodo Lista,int num)
       {
        int temp;
        nodo aux;
        if(Lista==NULL){
         gotoxy(35,4); printf("No se puede Lista Vacia");}
        else
         { aux=Lista;
         while(aux!=NULL)
           {
        if(aux->dato==num)
         {
         temp=aux->dato;
         aux->dato=aux->sgte->dato;
         aux->sgte->dato=temp;
         aux=NULL;
          gotoxy(35,4); printf("­ Ya se Intercambio !");
         }
        else{
        gotoxy(35,4); printf("­ No est  el Numero !");
         aux=aux->sgte;}
           }
       }
    return Lista;
    }
    nodo Vaciar(nodo Lista)
    { nodo aux;
     aux=Lista;
     while(aux!=NULL)
      { Lista=Lista->sgte;
        free(aux);
        aux=Lista;
      }
    return aux;
    }
    nodo Formalizar(nodo Lista)
    {  nodo aux,temp;
     if(Lista==NULL){
      gotoxy(35,4); printf("No se puede Lista Vacia ");}
     else
      {  aux=Lista;
      while(aux!=NULL)
       {  temp=aux;
          temp=temp->sgte;
          while(temp!=NULL)
          {
        if(Existe(temp,aux->dato)==1)
         {  Lista=Eliminar_aux(Lista,aux->dato);
         temp=temp->sgte;
         }
        else
         temp=temp->sgte;
          }
         aux=aux->sgte;
        }
         gotoxy(35,4); printf("! Ya se Formaliz¢ ­");
      }
    return Lista;
    }
    int Existe(nodo Lista,int num)
    {  int r=0;
     if(Lista==NULL){
       gotoxy(32,2); printf("Lista Vacia");}
     else
      {
       while(Lista!=NULL)
        {
         if(Lista->dato==num)
           {  r=1;
           Lista=NULL;
           }
         else
           Lista=Lista->sgte;
         }
      }
    return r;
    }






     

No hay comentarios:

Publicar un comentario