sábado, 19 de febrero de 2011

Laboratorio08 - Arboles


Ola este es el Laboratorio 08 del curso de estructuras de datos que trata sobre Arboles .. espero les ayude.. Saludos...


/* Arboles */

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

using namespace std;

typedef struct nodo /// Estructura Nodo
{  int dato;
   struct nodo *izq;
   struct nodo *der;
}TNodo;


typedef struct Arbol /// Estructura Arbol
{  struct nodo *Raiz;
}TArbol;


/*  CREAR NODO  */
TNodo *crearNodo(int x)
{  TNodo *nodo=(TNodo*)malloc(sizeof(TNodo));
   nodo->dato=x;
   nodo->izq=NULL;
   nodo->der=NULL;
   return nodo;
}

/*  CREAR ARBOL  */
TArbol *crearArbol()
{  TArbol *Arbol=(TArbol*)malloc(sizeof(TArbol));
   Arbol->Raiz=NULL;
   return Arbol;
}


/*  FUNCION INSERTAR  */
void insertar(TArbol *Arbol, int x)
{  TNodo *Nodo=crearNodo(x);
   char lado;
   TNodo *p=NULL;
   TNodo *a=NULL;
   if(Arbol->Raiz==NULL)
      Arbol->Raiz=Nodo;
   else
   {  p = Arbol->Raiz;
      bool band = true;
      while ( p != NULL)
      {   a = p;
          if( x > a->dato )
          {   p = a -> der;
              lado = 'D';
              continue;
          }
          if ( x < a->dato )
          {   p = a -> izq;
              lado = 'I';
              continue;
          }  
          band = false;
          break;
      }
      if( band == true )
      {   if( lado == 'D')
          a->der = Nodo;
          if( lado == 'I')
             a->izq = Nodo;
      }
      else
          cout<<"\n\t\t\t\t\t El Elemento Ya Existe!!! \n";
   }
}


/*  FUNCION PRE ORDEN  */
void PreOrden(TNodo *p)
{
   if(p!=NULL)
   {
      cout<<setw(5)<<p->dato;
 PreOrden(p->izq);
 PreOrden(p->der);
   }
}


/*  FUNCION IN ORDEN  */
void InOrden(TNodo *p)
{  if(p!=NULL)
   {   InOrden(p->izq);
  cout<<setw(5)<<p->dato;
  InOrden(p->der);
   }
}


/*  FUNCION POST ORDEN  */
void PostOrden(TNodo *p)
{

 if(p!=NULL)
   {
 
PostOrden(p->izq);
PostOrden(p->der);
cout<<setw(5)<<p->dato;
     
   }
}


/*  BUSCAR  */
bool buscar(TNodo *p , int num)
{  if (p!=NULL)
   {
       if(p->dato==num)
       {   cout<<"\n"<<setw(10)
           <<"\t\t     Dato Encontrado = "<<p->dato<<endl;
           return(1);
       }
       else
       {   if(num < p->dato)
               buscar(p->izq,num);
           else
               buscar(p->der,num);
       }
   }
   else
       return(0);
}


/*  FUNCION PRESENTACION  */
void presentacion()
{  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 08 - ARBOLES                 º  ";
   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");
}


/*  FUNCION MENU  */
void menu()
{
   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ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ \n\n";

   cout<<"\t\t\tÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»  "<<endl;
   cout<<"\t\t\tº   1. Insertar Dato en el Arbol  º  "<<endl;
   cout<<"\t\t\tº   2. Recorrido en PreOrden      º  "<<endl;
   cout<<"\t\t\tº   3. Recorrido en InOrden       º  "<<endl;
   cout<<"\t\t\tº   4. Recorrido en PostOrden     º  "<<endl;
   cout<<"\t\t\tº   5. Buscar Dato en el Arbol    º  "<<endl;
   cout<<"\t\t\tºÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͺ  "<<endl;
   cout<<"\t\t\tº   6.  Salir                     º  "<<endl;
   cout<<"\t\t\tÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ  "<<endl<<endl;
}


/*  MENU PRINCIPAL  */
int main()
{  system("color 1E");
   presentacion();
   TArbol *A = crearArbol();
   TNodo *p = NULL;
   while(1)
   {   menu();
       int opcion;
       do
       {  cout<<"\t   >> Elija una opcion: ";
          cin>>opcion;
       }
       while(opcion < -10000 || opcion > 10000);
       
          switch (opcion)
          {
              case 1: int N;
 int Nro;
 cout<<endl<<"\t\t Cantidad de Datos : ";
 cin>>N;
                      cout<<"\n";
 for( int i = 0 ; i< N ; i++ )
         {  
                         cout<<"\t\t\t\tPosicion["<<i+1<<"] : ";
cin>>Nro;
insertar(A,Nro);
         }
         cout<<"\n\t\t      Los Datos han sido almacenados...\n\n";
                      cout<<"\t   Presione una tecla para continuar... ";
                      getch();
                      system("cls");
                      break;

              case 2: cout<<"\n\t\t Recorrido en PreOrden :"<<endl<<endl<<"\t";      
                      p = A->Raiz;
                      cout<<"\t\t";
 PreOrden(p);
 cout<<endl<<endl;
                      cout<<"\t   Presione una tecla para continuar... ";
                      getch();
                      system("cls");
 break;

case 3:  cout<<"\n\t\t Recorrido en InOrden :"<<endl<<endl<<"\t";
                      p = A->Raiz;
                      cout<<"\t\t";
 InOrden(p);
 cout<<endl<<endl;
                      cout<<"\t   Presione una tecla para continuar... ";
                      getch();
                      system("cls");
 break;

case 4:  cout<<"\n\t\t Recorrido en PostOrden "<<endl<<endl<<"\t";
                      p = A->Raiz;
                      cout<<"\t\t";
 PostOrden(p);
 cout<<endl<<endl;
                      cout<<"\t   Presione una tecla para continuar... ";
                      getch();
                      system("cls");
 break;

case 5:  int num;
                      bool valor;
                      cout<<"\n\t\t  Ingrese Numero A Buscar : ";
 cin>>num;
 p = A->Raiz;
     valor = buscar(p,num);
                      if(valor == 0)
                          cout<<"\n\t\t     No se Encontro el Dato!!!";
                      cout<<endl<<endl;
                      cout<<"\t   Presione una tecla para continuar... ";
                      getch();
                      system("cls");
     break;
                     
case 6:  system("cls");
         cout<<"\n\n\n\n\n\n\n\n\n\n\n";
         cout<<"\t\t      Visita www.infobiks.blogspot.com ";
         getch();
                      exit(1);    
 break;

default: cout<<endl;
                      cout<<"\t\t     La Opcion No es Valida !!!!!!!!!";
                      cout<<endl<<endl;
                      cout<<"\t   Presione una tecla para continuar... ";
                      getch();
                      system("cls");
                      break;

        }

    }

 system("PAUSE");

}

viernes, 18 de febrero de 2011

Laboratorio07 - Colas


Ola este es el Laboratorio 07 del curso de estructuras de datos que trata sobre Colas .. espero les ayude.. Saludos...


/* Colas */

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

using namespace std;


typedef struct Nodo /// Estructura Nodo
{  int dato;
   struct Nodo *sgt;
}TNodo;


typedef struct Cola /// Estructura Cola
{  TNodo *frente;
   TNodo *final;
}TCola;


/*  CREAR NODO  */
TNodo *crearNodo(int x)
{  TNodo *nodo = (TNodo*)malloc(sizeof(TNodo));
   nodo->dato = x;
   nodo->sgt = NULL;
   return nodo;
}


/*  CREAR COLA  */
TCola *crearCola()
{  TCola *Cola = (TCola*)malloc(sizeof(TCola));
   Cola->frente = NULL;
   Cola->final = NULL;
   return Cola;
}


/*  FUNCION INSERTAR (FIRST-IN)  */
void Poner(TCola *cola, int x)
{  TNodo *nodo=crearNodo(x);
   if(cola->final==NULL)
      cola->frente=nodo;
   else
      cola->final->sgt=nodo;
      cola->final=nodo;
}


/*  FUNCION ELIMINAR (FIRST-OUT)  */
void Quitar(TCola *cola)
{  TNodo *p=NULL;
   if(cola->frente != NULL)
   {  p=cola->frente;
      cola->frente = p->sgt;
      if(cola->frente == NULL)
         cola->final = NULL;
         free(p);
   }
}



/*  FUNCION IMPRIMIR  */
void Imprimir(TCola *cola)
{  TNodo *p=cola->frente;
   int i=1;
   while(p!=NULL)
   {  cout<<endl<<"\t\t      Posicion ["<<i<<"] : "<<p->dato;
      i++;
      p=p->sgt;
   }  
}


/*  FUNCION FRENTE  */
void Frente(TCola *Cola)
{  cout<<endl;  
   cout<<"\t\t      El Frente de La Cola Es : "<<Cola->frente->dato;
   cout<<endl<<endl;  
}


/*  MENU PRINCIPAL  */
int main()
{  system("color 0B");
   TCola *P=crearCola();
   int N,i=0;
   int opcion;
   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 07 - COLAS                 º  ";
   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");

   while(1)
   {
         
      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;      
      cout<<endl<<"\t\t\tÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» ";
      cout<<endl<<"\t\t\tº 1.  FIRST-IN: Insertar Cola     º ";
      cout<<endl<<"\t\t\tº 2.  Imprimir Cola               º ";
      cout<<endl<<"\t\t\tº 3.  FIRST-OUT: Eliminar Cola    º ";
      cout<<endl<<"\t\t\tº 4.  Frente de la Cola           º ";
      cout<<endl<<"\t\t\tº 5.  Salir                       º ";
      cout<<endl<<"\t\t\tÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ \n\n";  

      do
      {
        cout<<"\t\t >> Elija una opcion: ";
        cin>>opcion;
      }
   
      while(opcion < -10000 || opcion > 10000);


      switch(opcion)
      {
        case 1:  i++;
                 cout<<endl<<"\t\t      Ingrese ["<<i<<"] elemento: ";
                 cin>>N;
                 Poner(P,N);
                 cout<<"\n\t\t      El elemento a sido almacenado...\n\n";
                 cout<<"\t\t Presione una tecla para continuar... ";
                 getch();
                 system("cls");
                 break;
               
        case 2:  Imprimir(P);
                 cout<<endl<<endl;
                 cout<<"\t\t Presione una tecla para continuar... ";
                 getch();
                 system("cls");
                 break;
                       
        case 3:  cout<<endl;
                 Quitar(P);
                 cout<<"\t\t      * Se Elimino el Frente; osea el primer elemento";
                 cout<<"\n\t\t        ingresado ubicado en la Posicion [1] ";    
                 cout<<endl<<endl;
                 cout<<"\t\t Presione una tecla para continuar... ";
                 getch();
                 system("cls");
                 break;
                 
        case 4:  cout<<endl;    
                 Frente(P);
                 cout<<endl;
                 cout<<"\t\t Presione una tecla para continuar... ";
                 getch();
                 system("cls");
                 break;

        case 5:  exit(1);
                 break;
       
        default: cout<<endl;
                 cout<<"\t\t     La Opcion No es Valida !!!!!!!!!";
                 cout<<endl<<endl;
                 cout<<"\t\t Presione una tecla para continuar... ";
                 getch();
                 system("cls");
                 break;
      }
   }                    
   cout<<endl<<endl;
   system("PAUSE");
 }

Laboratorio06 - Pilas


Ola este es el Laboratorio 06 del curso de estructuras de datos que trata sobre Pilas .. espero les ayude.. Saludos.


/* Pilas */

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

using namespace std;


typedef struct Nodo /// Estructura Nodo
{  int dato;
   struct Nodo *sgt;
}TNodo;


typedef struct Pila  /// Estructura Pila
{  TNodo *cabeza;
}TPila;


/*   CREAR NODO  */
TNodo *crearNodo(int x)
{  TNodo *nodo = (TNodo*)malloc(sizeof(TNodo));
   nodo->dato = x;
   nodo->sgt = NULL;
   return nodo;
}


/*  CREAR PILA  */
TPila *crearPila()
{  TPila *Pila = (TPila*)malloc(sizeof(TPila));
   Pila->cabeza = NULL;
   return Pila;
}


/*  FUNCION INSERTAR  ( PUSH )  */
void Push(TPila *Pila, int x)
{  TNodo *Nodo = crearNodo(x);
   Nodo->sgt = Pila->cabeza;  
   Pila->cabeza = Nodo;
}


/*  FUNCION ELIMINAR ( POP )  */
void Pop(TPila *Pila)
{  TNodo *p=NULL;
   if(Pila->cabeza!=NULL)
   {  
       p=Pila->cabeza;              
       Pila->cabeza=p->sgt;
       free(p);
   }
}


/*  FUNCION IMPRIMIR  */
void Imprime(TPila *Pila)
{  TNodo *p=Pila->cabeza;
   int i=1;
   while(p!=NULL)
   {  
      cout<<endl<<"\t\t      Posicion ["<<i<<"] : "<<p->dato;  
      i++;
      p=p->sgt;
   }
}


/*  FUNCION TOPE  */
void Tope(TPila *Pila)
{  cout<<endl;  
   cout<<"\t\t      El Tope de La Pila Es : "<<Pila->cabeza->dato;
   cout<<endl<<endl;
}



/*  MENU PRINCIPAL  */
int main()
{  system("color 2F");
   TPila *P=crearPila();
   int N,i=0;
   int opcion;
   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 06 - PILAS                 º  ";
   cout<<endl<<"\t      º       Hecho por InfoBik's ---> JP. Rodriguez       º  ";
   cout<<endl<<"\t      ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ  \n"<<endl;
   cout<<endl<<"\t\t >> Presione cualquier tecla para continuar... ";
   getch();
   system("CLS");
 
   while(1)
   {
         
   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;      
      cout<<endl<<"\t\t\tÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» ";
      cout<<endl<<"\t\t\tº 1.  PUSH - Insertar Pila     º ";
      cout<<endl<<"\t\t\tº 2.  Imprimir Pila            º ";
      cout<<endl<<"\t\t\tº 3.  POP - Eliminar Pila      º ";
      cout<<endl<<"\t\t\tº 4.  Tope de la Pila          º ";
      cout<<endl<<"\t\t\tº 5.  Salir                    º ";
      cout<<endl<<"\t\t\tÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ \n\n";  

      do
      {
        cout<<"\t\t >> Elija una opcion: ";
        cin>>opcion;
      }
   
      while(opcion < -10000 || opcion > 10000);


      switch(opcion)
      {
           case 1: i++;
                   cout<<endl<<"\t\t      Ingrese ["<<i<<"] elemento: ";
                   cin>>N;
                   Push(P,N);
                   cout<<"\n\t\t      El elemento a sido almacenado...\n\n";
                   cout<<"\t\t Presione una tecla para continuar... ";
                   getch();
                   system("cls");
                   break;
                 
           case 2: Imprime(P);
                   cout<<endl<<endl;
                   cout<<"\t\t Presione una tecla para continuar... ";
                   getch();
                   system("cls");      
                   break;
                                       
           case 3: cout<<endl;
                   Pop(P);
                   cout<<"\t\t      * Se Elimino el Tope; osea el ultimo elemento";
                   cout<<"\n\t\t        ingresado ubicado en la Posicion [1] ";    
                   cout<<endl<<endl;
                   cout<<"\t\t Presione una tecla para continuar... ";
                   getch();
                   system("cls");          
                   break;
               
           case 4: cout<<endl;    
                   Tope(P);
                   cout<<endl;
                   cout<<"\t\t Presione una tecla para continuar... ";
                   getch();
                   system("cls");
                   break;

           case 5: system("cls");
                   cout<<"\n\n\n\n\n\n\n\n\n\n\n\t\t      Visita www.infobiks.blogspot.com ";
                   getch();
                   exit(1);
                   break;
               
           default:cout<<endl;        
                   cout<<"\t\t     La Opcion No es Valida !!!!!!!!!";
                   cout<<endl<<endl;
                   cout<<"\t\t Presione una tecla para continuar... ";
                   getch();
                   system("cls");
                   break;
      }      
   }                    
}

jueves, 17 de febrero de 2011

Examen Parcial I Unidad


Ola a todos este es el examen de la I Unidad del Laboratorio de Estructura de Datos, el profesor pidio implementar el Buscaminas ; en las demas unidades no tomo examen..., espero les sirva... Saludos.


// BUSCAMINAS

#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{

char M[100][100];
int cont;
int f,c,i,j,k,p;
system("title BUSCAMINAS");
system("color 9F");
char R='S';
 do
 {
    /* ingresa tamaño de matriz y valida */
    do
    {
        cout<<endl<<"\t\t+--------------------------------------------------+ ";
        cout<<endl<<"\t\t|        1ER EXAMEN DE ESTRUCTURA DE DATOS         | ";
        cout<<endl<<"\t\t|               BUSCAMINAS(RELEASE)                | ";
        cout<<endl<<"\t\t|    - [ UNIVERSIDAD NACIONAL DE TRUJILLO ] -      | ";
        cout<<endl<<"\t\t|  Copyright © Company InfoBik's (JP. Rodriguez)   | ";        
        cout<<endl<<"\t\t+--------------------------------------------------+ ";
        cout<<endl<<endl;
        cout<<" Ingrese el numero de filas: ";
        cin>>f;
        cout<<" Ingrese el numero de columnas: ";
        cin>>c;
        cout<<endl;
        if((f<1)||(c<1))
        { system("cls"); }
    }
    while((f<1)||(c<1));

    /* ingresamos  caracteres en las posiciones de la matriz*/
    cout<<endl;
    for(i=0;i<f;i++)
    {
        for(j=0;j<c;j++)
        {
            do /* validamos q sea - y * los caracteres ingresados*/
            {
                cout<<" Ingrese el caracter (-) o (*) en la fila "<<"["<<i+1<<"], columna["<<j+1<<"]: ";
                cin>>M[i][j];
            }
            while((M[i][j]!='-')&&(M[i][j]!='*'));
        }
    }

    /*mostramos la matriz ingresada*/
    cout<<endl;
    cout<<" LA MATRIZ INGRESADA ES: ";
    cout<<endl<<endl;
    for(i=0;i<f;i++)
    {
        for(j=0;j<c;j++)
        {  cout<<"   "<<M[i][j]; }
        cout<<endl;
    }

    /* busca en las esquinas */
    if(M[0][0]=='-')
    {
        cont=0;
        for(i=0;i<2;i++)
        {
            for(j=0;j<2;j++)
            {
                if(M[i][j]=='*')
                { cont++; }
            }
        }
        M[0][0]=M[0][0]+3+cont;
    }

    if(M[0][c-1]=='-')
    {
        cont=0;
        for(i=0;i<2;i++)
        {
            for(j=c-2;j<c;j++)
            {
                if(M[i][j]=='*')
                { cont++; }
            }
        }
        M[0][c-1]=M[0][c-1]+3+cont;
    }

    if(M[f-1][0]=='-')
    {
        cont=0;
        for(i=f-2;i<f;i++)
        {
            for(j=0;j<2;j++)
            {
                if(M[i][j]=='*')
                { cont++; }
            }
        }
        M[f-1][0]=M[f-1][0]+3+cont;
    }

    if(M[f-1][c-1]=='-')
    {
        cont=0;
        for(i=f-2;i<f;i++)
        {
            for(j=c-2;j<c;j++)
            {
                if(M[i][j]=='*')
                { cont++; }
            }
        }
        M[f-1][c-1]=M[f-1][c-1]+3+cont;
    }

    /* buscamos en las aristas */
    for(k=1;k<c-1;k++)
    {
        if(M[0][k]=='-')
        {
            cont=0;
            for(i=0;i<2;i++)
            {
                for(j=k-1;j<k+2;j++)
                {
                    if(M[i][j]=='*')
                    { cont++; }
                }
            }
            M[0][k]=M[0][k]+3+cont;
        }
    }

    for(k=1;k<c-1;k++)
    {
        if(M[f-1][k]=='-')
        {
            cont=0;
            for(i=f-2;i<f;i++)
            {
                for(j=k-1;j<k+2;j++)
                {
                    if(M[i][j]=='*')
                    {  cont++; }
                }
            }
            M[f-1][k]=M[f-1][k]+3+cont;
        }
    }

    for(k=1;k<f-1;k++)
    {
        if(M[k][0]=='-')
        {
            cont=0;
            for(i=k-1;i<k+2;i++)
            {
                for(j=0;j<2;j++)
                {
                    if(M[i][j]=='*')
                    { cont++; }
                }
            }
            M[k][0]=M[k][0]+3+cont;
        }
    }

    for(k=1;k<f-1;k++)
    {
        if(M[k][c-1]=='-')
        {
            cont=0;
            for(i=k-1;i<k+2;i++)
            {
                for(j=c-2;j<c;j++)
                {
                    if(M[i][j]=='*')
                    { cont++; }
                }
            }
            M[k][c-1]=M[k][c-1]+3+cont;
        }
    }

    /* buscamos en el centro */
    for(k=1;k<f-1;k++)
    {
        for(p=1;p<c-1;p++)
        {
            if(M[k][p]=='-')
            {
                cont=0;
                for(i=k-1;i<k+2;i++)
                {
                    for(j=p-1;j<p+2;j++)
                    {
                        if(M[i][j]=='*')
                        { cont++; }
                    }
                }
                M[k][p]=M[k][p]+3+cont;
            }
        }
    }

    /* mostramos el resultado */
    cout<<endl;
    cout<<" LA MATRIZ RESULTANTE ES: ";
    cout<<endl<<endl;
    for(i=0;i<f;i++)
    {
        for(j=0;j<c;j++)
        { cout<<"   "<<M[i][j]; }
        cout<<endl<<endl;
    }

    cout<<" DESEA CONTINUAR [S/N]  :  ";
    cin>>R;
    cout<<endl;
    system("CLS");
     
 }
 while ( R == 'S' || R == 's' );
   
}

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();
}