jueves, 17 de febrero de 2011

Laboratorio05 - Listas


Ola este es el Laboratorio 05 del curso de estructuras de datos que trata sobre listas enlazadaz .. espero les ayude.. Saludos


/* Listas */

#include <cstdlib>
#include <iostream>
#include <conio.h>

using namespace std;


/* estructura  elemento*/
struct elemento
 {
  int dato;
  struct elemento *sgt;
 };
typedef struct elemento NODO;

/* estructura unir  */
struct unir
 {
  NODO *inicio;
 };
typedef struct unir LISTA;

/* estructura para la creacion de un nodo */
NODO *crear_nodo(int num)
 {
  NODO *nodo = (NODO*)malloc(sizeof(NODO));
  nodo -> dato = num;
  nodo -> sgt = NULL;
  return nodo;
 }

/* estructura para la creacion de una lista */
LISTA *crear_lista()
 {
  LISTA *lista = (LISTA*)malloc(sizeof(LISTA));
  lista -> inicio = NULL;
  return lista;
 }

void insertar_inicio(LISTA *lista,int num)
 {
  NODO *nodo = (NODO*)malloc(sizeof(NODO));
  nodo -> dato = num;
  nodo -> sgt = lista -> inicio;
  lista -> inicio = nodo;
 }



void insertar_antes(LISTA *lista,int dato,int num)
 {
  int band;
  NODO *p = NULL;
  NODO *ant = NULL;
  band=1;
  p=lista->inicio;
  if(p==NULL)
   {
    band=0;
   }
  else
   {
    while( (p->dato != dato)&&(band==1) )
     {
      if(p->sgt != NULL)
       {
        ant=p;
        p=p->sgt;
       }
      else
       {
        band=0;
       }
     }
    if(band==1)
     {
      NODO  *nodo = crear_nodo(num);
      if(ant==NULL)
       {
        nodo->sgt=p;
        lista->inicio=nodo;
       }
      else
       {
        ant->sgt=nodo;
        nodo->sgt=p;
       }
     }
    else
     {
      cout<<" No se encontro el elemento en la lista";
      cout<<endl<<endl;
     }
   }
 }


void insertar_despues(LISTA *lista,int dato,int num)
 {
  int band;
  NODO *p = NULL;
  band=1;
  p=lista->inicio;
  if(p==NULL)
   {
    band=0;
   }
  else
   {
    while( (p->dato!=dato)&&(band==1) )
     {
      if(p->sgt!=NULL)
       {
        p=p->sgt;
       }
      else
       {
        band=0;
       }
     }
   }
  if(band==1)
   {
    NODO *nodo = NULL;
    nodo = (NODO*)malloc(sizeof(NODO));
    nodo -> dato = num;
    nodo -> sgt = p -> sgt;
    p -> sgt = nodo;
   }
  else
   {
    cout<<" No se encontro el elemento en la lista";
    cout<<endl<<endl;
   }
 }


void insertar_final(LISTA *lista,int num)
 {
  NODO *p = NULL;
  NODO *nodo = crear_nodo(num);
  p=lista->inicio;
  if(p==NULL)
   {
    lista->inicio=nodo;
   }
  else
   {
    while(p->sgt!=NULL)
     {
      p=p->sgt;
     }
    p->sgt=nodo;
   }
 }


void eliminar_inicio(LISTA *lista)
 {
  NODO*p=lista->inicio;
  lista->inicio=p->sgt;
  free(p);
 }


void eliminar_elemento(LISTA *lista,int num)
 {
  int band;
  NODO *p = NULL;
  NODO *ant = NULL;
  band=1;
  p=lista->inicio;
  if(p==NULL)
   {
    band=0;
   }
  else
   {
    while((p->dato!=num)&&(band==1))
     {
      if(p->sgt!=NULL)
       {
        ant=p;
        p=p->sgt;
       }
      else
       {
        band=0;
       }
     }
   }
  if(band==1)
   {
    if(ant==NULL)
     {
      lista->inicio=p->sgt;
     }
    else
     {
      ant->sgt=p->sgt;
     }
    free(p);
   }
  else
   {
    cout<<" No se encontro el elemento en la lista";
    cout<<endl<<endl;
   }
 }


void eliminar_final(LISTA *lista)
 {
  NODO *p=lista->inicio;
  NODO *ant=NULL;
  if(p!=NULL)
   {
    if(p->sgt!=NULL)
     {
      while(p->sgt!=NULL)
       {
        ant=p;
        p=p->sgt;
       }
      ant->sgt=NULL;
     }
    else
     {
      p=NULL;
     }
    free(p);
   }
 }



void buscar_secuencial(LISTA *lista,int num)
 {
  NODO *p=lista->inicio;
  if(p!=NULL)
   {
    while((p!=NULL)&&(p->dato!=num))
     {
      p=p->sgt;
     }
   }
  if(p==NULL)
   {
    cout<<"\t\t No se encontro el elemento en la lista";
    cout<<endl;
   }
  else
   {
    cout<<"\t\t SI se encontro el elemento en la lista";
    cout<<endl;
   }
 }


int ordenar(LISTA *lista,int n)
 {
  int aux;
  NODO *p=NULL;
  p=lista->inicio;
  cout<<"\t\t    ";
  while(p!=NULL)
   {
    if(p->sgt!=NULL)
     {
      if(p->dato>(p->sgt)->dato)
       {
        aux=p->dato;
        p->dato=(p->sgt)->dato;
        (p->sgt)->dato=aux;
       }
     }
    p=p->sgt;
   }
 }


void buscar_binaria(LISTA *lista,int num)
 {
  NODO *p=lista->inicio;
  if(p!=NULL)
   {
    while((p!=NULL)&&(p->dato<num))
     {
      p=p->sgt;
     }
   }
  if((p==NULL)||(p->dato>num))
   {
    cout<<"\t\t No se encontro el elemento en la lista";
    cout<<endl;
   }
  else
   {
    cout<<"\t\t SI se encontro el elemento en la lista";
    cout<<endl;
   }
 }


void mostrar(LISTA *lista)
 {
  NODO *p=NULL;
  p=lista->inicio;
  cout<<"\t\t    ";
  while(p!=NULL)
   {
    cout<<" "<<p->dato<<" ";
    p=p->sgt;
   }
 }



int main(int argc, char *argv[])
{
system("color 8F");    
LISTA *lista=crear_lista();
int num,n,i;
char x;
    cout<<"\n\n\n\n\n\n";
    cout<<endl<<"\t      ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»  ";
    cout<<endl<<"\t      º     |<-[ UNIVERSIDAD NACIONAL DE TRUJILLO ]->|     º  ";
    cout<<endl<<"\t      º          CURSO ::: ESTRUCTURA DE DATOS             º  ";
    cout<<endl<<"\t      º      Laboratio 05 - Listas Enlazadas Simples       º  ";
cout<<endl<<"\t      º       Hecho por InfoBik's ---> JP. Rodriguez       º  ";
cout<<endl<<"\t      ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ  \n"<<endl;
cout<<endl<<"\t\t >> Presione cualqier tecla para continuar... ";
getch();
system("CLS");

cout<<endl;
cout<<"\n\n\n\n\n\t  >>>> Cuantas elementos desea ingresar en la lista: ";
cin>>n;
cout<<"\t  ======================================================\n"<<endl;
//llenamos la lista
for(i=1;i<=n;i++)
 {
  cout<<"\t\t Ingrese dato ["<<i<<"] : ";
  cin>>num;
  insertar_final(lista,num);//cda dato ingresado lo va almacenando al final de la lista
 }
cout<<"\n\t  ======================================================\n"<<endl;

cout<<" \t   Presione cualquier tecla para continuar...";
getch();
system("CLS");

do
 {
  int opc;
  do
   {
    cout<<"\n\n";  
    cout<<endl<<"\t\t                  MENU PRINCIPAL ";
    cout<<endl<<"\t\t                ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ ";
    cout<<endl<<"\t\t DISENIE E IMPLEMENTE LOS SIGUIENTES ALGORITMOS: ";
    cout<<endl<<"\t\tÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ ";
 
    cout<<endl<<"\t\t\tÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» ";
    cout<<endl<<"\t\t\tº  1. INSERCION EN UNA LISTA    º ";
    cout<<endl<<"\t\t\tº  2. ELIMINACION DE UNA LISTA  º ";
    cout<<endl<<"\t\t\tº  3. ORDENACION DE UNA LISTA   º ";
    cout<<endl<<"\t\t\tº  4. BUSQUEDA DE UNA LISTA     º ";
    cout<<endl<<"\t\t\tº  5. RECORRIDO DE UNA LISTA    º ";
    cout<<endl<<"\t\t\tÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ \n\n";
    cout<<"\t\t >> Elija una opcion: ";
    cin>>opc;
    cout<<endl;
    if((opc<1)||(opc>5))
     {
      cout<<"\t\t >> OPCION INCORRECTA [Presione Enter para continuar]";
      getch();
      system("CLS");
     }
   }
  while((opc<1)||(opc>5));


  switch (opc)
   {
    case 1: int opc1;
            do
             {system("CLS");
              cout<<"\n";
              cout<<endl<<"\t\t\t         INSERCION EN UNA LISTA ";
              cout<<endl<<"\t\t\t        ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ ";
               
              cout<<endl<<"\t\tÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» ";
              cout<<endl<<"\t\tº  1. INSERTAR AL INICIO                              º ";
              cout<<endl<<"\t\tº  2. INSERTAR AL FINAL                               º ";
              cout<<endl<<"\t\tº  3. INSERTAR ELEMENTO ANTES DE UNO ESPECIFICADO     º ";
              cout<<endl<<"\t\tº  4. INSERTAR ELEMENTO DESPUES DE UNO ESPECIFICADO   º ";
              cout<<endl<<"\t\tÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ \n\n";
              cout<<"\t\t >>Elija una opcion: ";
              cin>>opc1;
              cout<<endl;
              if((opc1<1)||(opc1>4))
               {
                cout<<"\t\t >> OPCION INCORRECTA [Presione Enter para continuar]";
                getch();
                system("CLS");
           
               }
             }
            while((opc1<1)||(opc1>4));
            cout<<"\t ==================================================================== \n"<<endl;

            switch (opc1)
             {
              case 1: cout<<"\t\t LOS ELEMENTOS DE LA LISTA SON: ";
                      cout<<endl<<endl;
                      mostrar(lista);
                      cout<<" NULL";
                      cout<<endl<<endl;
                      cout<<"\t\t >> Ingrese elemento a insertar al inicio: ";
                      cin>>num;
                      cout<<endl<<endl;
                      insertar_inicio(lista,num);
                      cout<<"\t\t SU NUEVA LISTA DE ELEMENTOS ES: ";
                      cout<<endl<<endl;
                      mostrar(lista);
                      cout<<" NULL";
                      cout<<endl<<endl;
                      break;
           
              case 2: cout<<"\t\t LOS ELEMENTOS DE LA LISTA SON: ";
                      cout<<endl<<endl;
                      mostrar(lista);
                      cout<<" NULL";
                      cout<<endl<<endl;
                      cout<<"\t\t >> Ingrese elemento a insertar al final: ";
                      cin>>num;
                      cout<<endl<<endl;
                      insertar_final(lista,num);
                      cout<<"\t\t SU NUEVA LISTA DE ELEMENTOS ES: ";
                      cout<<endl<<endl;
                      mostrar(lista);
                      cout<<" NULL";
                      cout<<endl<<endl;
                      break;
                   
              case 3: int dato;
                      cout<<"\t\t LOS ELEMENTOS DE LA LISTA SON: ";
                      cout<<endl<<endl;
                      mostrar(lista);
                      cout<<" NULL";
                      cout<<endl<<endl;
                      cout<<"\t\t >> Insertar nuevo elemento: ";
                      cin>>num;
                      cout<<"\t\t >> Antes del elemento: ";
                      cin>>dato;
                      cout<<endl<<endl;
                      insertar_antes(lista,dato,num);
                      cout<<"\t\t SU NUEVA LISTA DE ELEMENTOS ES: ";
                      cout<<endl<<endl;
                      mostrar(lista);
                      cout<<" NULL";
                      cout<<endl<<endl;
                      break;
                   
              case 4: cout<<"\t\t LOS ELEMENTOS DE LA LISTA SON: ";
                      cout<<endl<<endl;
                      mostrar(lista);
                      cout<<" NULL";
                      cout<<endl<<endl;
                      cout<<"\t\t >> Insertar nuevo elemento: ";
                      cin>>num;
                      cout<<"\t\t >> Despues del elemento: ";
                      cin>>dato;
                      cout<<endl<<endl;
                      insertar_despues(lista,dato,num);
                      cout<<"\t\t SU NUEVA LISTA DE ELEMENTOS ES: ";
                      cout<<endl<<endl;
                      mostrar(lista);
                      cout<<" NULL";
                      cout<<endl<<endl;
                     break;
             }      
             break;

    case 2: int opc2;
            do
             {system("cls");
              cout<<endl<<endl;
              cout<<"\t\t          OPERACIONES CON ELIMINACION DE DATOS"<<endl;
              cout<<"\t\t         ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
           
              cout<<endl<<"\t\tÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» ";
              cout<<endl<<"\t\tº  1. Eliminar Elemento Inicial De La Lista         º ";
              cout<<endl<<"\t\tº  2. Eliminar Elemento Final De La Lista           º ";
              cout<<endl<<"\t\tº  3. Eliminar Un Elemento X De La Lista            º ";
              cout<<endl<<"\t\tÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ \n\n";
              cout<<"\t\t >> Elija una opcion: ";
              cin>>opc2;
              cout<<endl;
              if((opc2<1)||(opc2>3))
               {
                cout<<"\t\t >> OPCION INCORRECTA [Presione Enter para continuar]";
                getch();
                system("CLS");        
               }
             }
            while((opc2<1)||(opc2>3));
            cout<<"\t   ===============================================================\n"<<endl;

            switch (opc2)
             {
              case 1: cout<<"\t\t LOS ELEMENTOS DE LA LISTA SON: ";
                      cout<<endl<<endl;
                      mostrar(lista);
                      cout<<" NULL";
                      cout<<endl<<endl;
                      eliminar_inicio(lista);
                      cout<<"\t\t SU NUEVA LISTA DE ELEMENTOS ES: ";
                      cout<<endl<<endl;
                      mostrar(lista);
                      cout<<" NULL";
                      cout<<endl<<endl;
                      break;
                   
              case 2: cout<<"\t\t LOS ELEMENTOS DE LA LISTA SON: ";
                      cout<<endl<<endl;
                      mostrar(lista);
                      cout<<" NULL";
                      cout<<endl<<endl;
                      eliminar_final(lista);
                      cout<<"\t\t SU NUEVA LISTA DE ELEMENTOS ES: ";
                      cout<<endl<<endl;
                      mostrar(lista);
                      cout<<" NULL";
                      cout<<endl<<endl;
                      break;

              case 3: cout<<endl;
                      cout<<"\t\t LOS ELEMENTOS DE LA LISTA SON: ";
                      cout<<endl<<endl;
                      mostrar(lista);
                      cout<<" NULL";
                      cout<<endl<<endl;
                      cout<<"\t\t >> Ingrese elemento que desea eliminar: ";
                      cin>>num;
                      cout<<endl<<endl;
                      eliminar_elemento(lista,num);
                      cout<<"\t\t SU NUEVA LISTA DE ELEMENTOS ES: ";
                      cout<<endl<<endl;
                      mostrar(lista);
                      cout<<" NULL";
                      cout<<endl<<endl;
                      break;
             }
             break;

    case 3: cout<<"\t\t LOS ELEMENTOS DE LA LISTA SON: ";
            cout<<endl<<endl;
            mostrar(lista);
            cout<<" NULL";
            cout<<endl<<endl;
            cout<<"\t\t SU NUEVA LISTA DE ELEMENTOS ORDENADOS ES: ";
            cout<<endl<<endl;
            ordenar(lista,n);
            cout<<" NULL";
            cout<<endl<<endl;
            break;

    case 4: int opc4;
            do
             {system("cls");
              cout<<endl<<"\n\n\t\t         OPERACIONES CON BUSQUEDA DE DATOS";
              cout<<endl<<"\t\t       ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
           
              cout<<endl<<"\t\tÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» ";
              cout<<endl<<"\t\tº  1. Buscar Un Elemento En Forma Secuencial        º ";
              cout<<endl<<"\t\tº  2. Buscar Un Elemento En Forma Binaria           º ";
              cout<<endl<<"\t\tÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ \n\n";
              cout<<"\t\t >> Elija una opcion: ";
              cin>>opc4;
              cout<<endl;
              if((opc4<1)||(opc4>2))
               {
                cout<<"\t\t >> OPCION INCORRECTA [Presione Enter para continuar]";
                getch();
                system("CLS");            
               }
             }
            while((opc4<1)||(opc4>2));
            cout<<"\t    =============================================================\n"<<endl;

            switch (opc4)
             {
              case 1: cout<<"\t\t LOS ELEMENTOS DE LA LISTA SON: ";
                      cout<<endl<<endl;
                      mostrar(lista);
                      cout<<" NULL";
                      cout<<endl<<endl;
                      cout<<"\t\t Ingrese elemento que desea buscar: ";
                      cin>>num;
                      cout<<endl<<endl;
                      buscar_secuencial(lista,num);
                      cout<<endl<<endl;
                      break;

              case 2: cout<<"\t\t LOS ELEMENTOS DE LA LISTA SON: ";
                      cout<<endl<<endl;
                      mostrar(lista);
                      cout<<" NULL";
                      cout<<endl<<endl;
                      cout<<"\t\t Ingrese elemento que desea buscar: ";
                      cin>>num;
                      cout<<endl<<endl;
                      buscar_binaria(lista,num);
                      cout<<endl<<endl;
                      break;
             }
             break;

    case 5: cout<<"\t    =============================================================\n"<<endl;
            cout<<"\t\t LOS ELEMENTOS DE LA LISTA SON: ";
            cout<<endl<<endl;
            mostrar(lista);
            cout<<" NULL";
            cout<<endl<<endl;
            break;

   }
  cout<<"\t\t >>DESEA REGRESAR AL MENU PRINCIPAL [s/n] : ";
  cin>>x;
  system("cls");
 }
while((x=='s')||(x=='S'));
cout<<"\n\n\n\n\n\n\n\n\n\n\n\t\t      Visita www.infobiks.blogspot.com ";
getch();
}

No hay comentarios.:

Publicar un comentario