martes, 27 de mayo de 2014

PILA DINAMICA

C:\Users\sku-59183\Desktop\repasodepilas\Nodo.h

#ifndef NODO_H
#define NODO_H

class Nodo
{
 public:
  Nodo(int, Nodo *);
  int getValor() const;
  Nodo *getSiguiente() const;
 
 
 private:
  int valor;
  Nodo *siguiente;
};
#endif

C:\Users\sku-59183\Desktop\repasodepilas\Nodo.cpp

#include<iostream>
#include"Nodo.h"

Nodo::Nodo(int dato, Nodo *nuevo)
{
 valor = dato;
 siguiente = nuevo;
}

int Nodo::getValor() const
{
 return valor;
}

Nodo *Nodo::getSiguiente() const
{
 return siguiente;
}

C:\Users\sku-59183\Desktop\repasodepilas\Pila.h

#ifndef PILA_H
#define PILA_H
#include"Nodo.h"
class Pila
{
 public:
  Pila();
  ~Pila();
  bool estaVacia() const;
  int pop();
  void push(int);
  void imprimir() const;
 private:
  Nodo *tope;
  
};
#endif

C:\Users\sku-59183\Desktop\repasodepilas\Pila.cpp

#include<iostream>
using std::cout;



#include"Pila.h"

Pila::Pila()
{
 tope = NULL;
}

Pila::~Pila()
{
 Nodo *aux;
 while(tope != NULL)
 {
  aux = tope;
  tope =(*tope).getSiguiente();
  delete aux;
 }
}



void Pila::push(int dato)
{
 Nodo *nuevo = new Nodo(dato,tope);
 tope = nuevo;
}

int Pila::pop()
{
 int r;
 if(tope == NULL)
 return 0;
 else
 {
  Nodo *nuevo = tope;
  tope = nuevo->getSiguiente();
  r = nuevo->getValor();
  delete nuevo;
  return r; 
 }
}

void Pila::imprimir() const
{
 Nodo *n;
 n = tope;
 
 while(n != NULL)
 {
  cout << n->getValor()<<"\n";
  n=n->getSiguiente();
  
  
 }
}

C:\Users\sku-59183\Desktop\repasodepilas\AppPilaDinamica.cpp
#include "Pila.h"
int main()
{
 Pila p;
 p.push(10);
 p.push(50);
 p.push(45);
 p.push(345);
 p.imprimir();
 p.pop();
 p.imprimir();
}

lunes, 21 de octubre de 2013

cola con lista simple

colas.htm
/*
 
  Author:Carlos Marin Ramirez Garcia
  Date: 21/10/13 08:40
  Description: implementacion de una cola con
               una lista simple, usando las
               operciones basicas de una cola
               estaVacia(),agregar(dato),extraer(),
               mostrarprimero(),limpiar() que se
               implementa dentro del destructor.
*/

#include<iostream>
using namespace std;



class NodoP
{
      public:
             int dato;
             NodoP *sig;
             NodoP(int dato)
             {
               this->dato=dato;
               sig=NULL;
               }
};


class ColaP
{
      public:
             NodoP *primero;
             NodoP *ultimo;
             ColaP()
             {
                primero=NULL;
                ultimo=NULL;
                }
                
int estaVacia()
{
    return(primero==NULL && ultimo==NULL);
}   
void agregar(int dato)
{
  NodoP *nodo=new NodoP(dato);
  if(estaVacia())
     {
       primero=nodo;
       ultimo=nodo;
       }
       else
       {   ultimo->sig=nodo;
           ultimo=nodo;
           }
           }
int extraer()
{
  if(estaVacia())
  {
    cout<<"no hay datos"<<endl;
    return 0Xfffff;
    }
   else
   {
    NodoP *nodo;
    nodo=primero;
    primero=nodo->sig;
    if(primero==NULL)
    {
      ultimo=NULL;
      }
      return nodo->dato;
      delete nodo;
      }
      }
void imprimir()
{
     NodoP *nodo;
     nodo=primero;
     if(estaVacia())
     {
       cout<<"sin nodos"<<endl;
       }
       else
       {
       while(nodo!=NULL)
       {
       cout<<"nodo:"<<nodo->dato<<endl;
       nodo=nodo->sig;
      }
      }
      } 
int mostrarprimero()
{
    if(estaVacia())
    {
      cout<<"no hay nodo"<<endl;
      return 0Xffff;
      }
      else
      {
          return primero->dato;
          }
          }
void limpiar()
{
     NodoP *nodo,*prev;
     nodo=primero;
     while(nodo!=NULL)
     {
        prev=nodo->sig;
        delete nodo;
        nodo=prev;
        }
        primero=NULL;
        ultimo=NULL;
        }
~ColaP()
{
   limpiar();
}
};                               
main()
{
      int x;
      ColaP cola;
      cola.agregar(10);
      cola.agregar(11);
      cola.agregar(12);
      cola.agregar(13);
      cola.imprimir();
      x=cola.extraer();
      cout<<"elemto extraido: "<<x<<endl;
      x=cola.mostrarprimero();
      cout<<"el primer elemento es:"<<x<<endl;
      cola.imprimir();
      cola.~ColaP();
      cola.imprimir();
      cola.extraer();
      cola.agregar(10);
      x=cola.extraer();
      cout<<"elemto extraido: "<<x<<endl;
      cola.imprimir();
      system("pause");
      }
      








martes, 15 de octubre de 2013

Agenda con estructura dinamica

___Users_alejandro_Desktop_LISTA_lista_c.html
#include "lista.h"

void menu(){
     
     printf("\n1. Insertar estudiante");     
     printf("\n2. Recorrer lista ");       
     printf("\n3. SALIR "); 
}

alumno* leerDatos(alumno *inicio){
     alumno *nuevo; 
     alumno *nodoSiguiente;
     
     nuevo= (alumno *)malloc(sizeof(alumno));
     printf("\n Nombre:"); 
     scanf(" %[^\n]", nuevo->nom);      
     printf(" Matricula:"); 
     scanf(" %[^\n]", nuevo->mat);       
     printf(" Edad:"); 
     scanf("%d", &(nuevo->edad));
     printf(" Prom General:"); 
     scanf("%lf", &(nuevo->promG));
     nuevo->sig=NULL;
     
     if (inicio== NULL){
       inicio= nuevo;
     }  
     else{
        nodoSiguiente=inicio;
        
        while(nodoSiguiente->sig != NULL ){
           nodoSiguiente=nodoSiguiente->sig;                      
        }
        nodoSiguiente->sig=nuevo; 
     }     
     return inicio;
}

void mostrarLista(alumno *inicio){
     alumno *ultimo;
     int i=1;
     if (inicio == NULL)
        printf("\nNO HAY LISTA\n");
     else {
        ultimo=inicio;   
        printf("\n       LISTA");    
    
        printf("\nNum Nombre\t\tMatricula\tPromedio ", i);   
        do{              
           printf("\n %d. %s\t\t\t\t%s\t%g ", i, ultimo->nom, ultimo->mat,ultimo->promG);                  
           ultimo=ultimo->sig;
           i++;
        }while(ultimo != NULL );
     }
}

___Users_alejandro_Desktop_LISTA_lista_h.html
#include <stdio.h>
#include <stdlib.h>



struct estudiante {
    char mat[20];
    char nom[80];
    int edad;
    double promG;
    struct estudiante *sig;    
};


typedef struct estudiante alumno;

void menu();
alumno* insertarEstudiante(alumno *inicio);
void mostrarLista(alumno *inicio);

___Users_alejandro_Desktop_LISTA_lista_c.html
#include "lista.h"

void menu(){
     
     printf("\n1. Insertar estudiante");     
     printf("\n2. Recorrer lista ");       
     printf("\n3. SALIR "); 
}

alumno* leerDatos(alumno *inicio){
     alumno *nuevo; 
     alumno *nodoSiguiente;
     
     nuevo= (alumno *)malloc(sizeof(alumno));
     printf("\n Nombre:"); 
     scanf(" %[^\n]", nuevo->nom);      
     printf(" Matricula:"); 
     scanf(" %[^\n]", nuevo->mat);       
     printf(" Edad:"); 
     scanf("%d", &(nuevo->edad));
     printf(" Prom General:"); 
     scanf("%lf", &(nuevo->promG));
     nuevo->sig=NULL;
     
     if (inicio== NULL){
       inicio= nuevo;
     }  
     else{
        nodoSiguiente=inicio;
        
        while(nodoSiguiente->sig != NULL ){
           nodoSiguiente=nodoSiguiente->sig;                      
        }
        nodoSiguiente->sig=nuevo; 
     }     
     return inicio;
}

void mostrarLista(alumno *inicio){
     alumno *ultimo;
     int i=1;
     if (inicio == NULL)
        printf("\nNO HAY LISTA\n");
     else {
        ultimo=inicio;   
        printf("\n       LISTA");    
    
        printf("\nNum Nombre\t\tMatricula\tPromedio ", i);   
        do{              
           printf("\n %d. %s\t\t\t\t%s\t%g ", i, ultimo->nom, ultimo->mat,ultimo->promG);                  
           ultimo=ultimo->sig;
           i++;
        }while(ultimo != NULL );
     }
}

Plantilla generica para una cola

Cola.htm
/*
  
  
  Author:Carlos Martin ramirez Garcia
  Date: 15/10/13 12:11
  Description:Plantilla generica de una cola 
*/

#include"NodoE.h"
#include"ColaE.h"
#include"NodoE.cpp"
#include"ColaE.cpp"
#include<iostream>
using namespace std;

int main()
{
ColaE<int> cola; 
cola.agregar(10);
cola.agregar(11);
cola.agregar(12);
cola.agregar(13);
cola.agregar(14);
cola.agregar(15);
cola.imprimir();

system("pause");
return 0;
}


ColaEEeee.htm
#ifndef COLAE_H
#define COLAE_H
#include"NodoE.h"
template<class T>
class ColaE
{
 public:
 NodoE<T> *primero;
 NodoE<T> *ultimo;
 ColaE();
int estaVacia();
void agregar(T dato);
T extraer();
T primerElemento();
void limpiar();
void imprimir();
};
#endif


ColaEEee.htm
#include<iostream>
using namespace std;

#include"NodoE.h"
#include"ColaE.h"

template <typename T>
ColaE<T>::ColaE()
 {
  primero=NULL;
  ultimo=NULL;
}
template <typename T>
int ColaE<T>::estaVacia()
{
  return(primero==NULL);      
}
template <typename T>
void ColaE<T>::agregar(T dato)
{
  NodoE<T> *nodo=new NodoE<T>(dato);
  if(estaVacia())
  {
    primero=nodo;
    ultimo=nodo;
   }
   else
   {
     ultimo->sig=nodo;
     ultimo=nodo;
     }
     }
template <typename T>
T ColaE<T>::extraer()
{
  NodoE<T> *nodo;
  nodo=primero;
  if(estaVacia())
  {
    return ;
  }
  else
  {
    primero=nodo->sig;
      if(primero==NULL)
      {
        ultimo=NULL;
        return nodo->dato;
        }
        }
        }
template <typename T>
T ColaE<T>::primerElemento()
{
  if(estaVacia())
  return NULL;
  else
  {
     return primero->dato;
     }
     }
template <typename T>     
void ColaE<T>::limpiar()
{
   primero=NULL;
   ultimo=NULL;
}
template <typename T>     
void ColaE<T>::imprimir()
{
  NodoE<T> *nodo;
  nodo=primero;
  int i=1;
  if(estaVacia())
  {
    cout<<"no existen datos"<<endl;
  }
  else
  {
  while(nodo!=NULL)
  {
     cout<<"nodo "<<i<<":"<<nodo->dato<<endl;
     nodo=nodo->sig;
     i++;
     }
     }
     }
     
   

Col.htm
#ifndef NODOE_H
#define NODOE_H
template<class T>
class NodoE
{
public:
      T dato;
      NodoE *sig;
      NodoE(T);
      NodoE();
             };
             #endif


ColaEEe.htm
#include"NodoE.h"
#include<iostream>
using namespace std;
template <typename T>
NodoE<T>::NodoE(T dato)
{
   this->dato=dato;
   sig=NULL;
}
template<typename T>
NodoE<T>::NodoE()
{
   dato=NULL;
   sig=NULL;
}