Herramientas Informaticas

Categoría: PILAS EN C++ Estructura de datos instituto tecnologico de los mochis funciones

PILAS EN C++ USANDO STRUCT

/JULIO CESAR LEYVA RODRIGUEZ
//PILA CON ESTRUCTURAS

#include
#include
#include
#define max 5

typedef int tipodatopila;

typedef struct pila
{
int tope;
int *pila;
};

//RESETEA LA PILA
void resetearpila(pila *resetea)
{
resetea->tope=-1;
gotoxy(40,6);    cout<<"SE HA RESETEADO LA PILA";
}

//PILA VACIA

int pilavacia(pila *vacia)
{
if(vacia->tope==-1)
        {
    return (1);
    }
else
    {
    return (0);
    }
}

//PILA LLENA

int pilallena(pila *llena)
{
if(llena->tope==max-1)
    {
    return (1);
    }
else
    {
    return (0);
    }
}

//PUSH
void push(pila *push, tipodatopila dato)
{
(push->tope)++;
*(push->pila+push->tope)=dato;
}

//INSERTAR EL DATO

void insertareldato(pila *insertar)
{
tipodatopila dato;
if(pilallena(insertar)==1)
    {
    gotoxy(40,6);    cout<<"LA PILA ESTA LLENA";
    }
else
    {
    gotoxy(40,6);    cout<>dato;
    push(insertar,dato);
    }
}

//POP

tipodatopila pop(pila *pop)
{
tipodatopila dato;
dato=*(pop->pila+pop->tope);
(pop->tope)–;
return(dato);
}

//BORRAR
void borrar(pila *borrar)
{
if(pilavacia(borrar)==1)
    {
    gotoxy(40,6);    cout<<"LA PILA ESTA VACIA";
    }
else
    {
    gotoxy(40,6);    cout<<"EL DATO "<<pop(borrar)<<" HA SIDO ELIMINADO";
    }
}

//MOSTRAR PILA
void mostrarpila(pila *mostrar)
{
if(pilavacia(mostrar)==1)
    {
    gotoxy(20,6);    cout<<"NO HAY DATOS";
    }
else
    {
    for(int x=0; xtope+1; x++)
        {
        gotoxy(40,5+x);    cout<pila[x];
        }
    }
}

//MENU
void menu(pila *menu)
{
int op;
do
    {
    clrscr();
    gotoxy(20,5);    cout<<"MENU";
    gotoxy(1,6);    cout<<"1. RESETEAR PILA";
    gotoxy(1,7);    cout<<"2. INSERTAR DATO";
    gotoxy(1,8);    cout<<"3. BORRAR DATO";
    gotoxy(1,9);    cout<<"4. VER PILA";
        gotoxy(1,10);    cout<<"ELIGA UNA OPCION [   ]";
        gotoxy(20,10);    cin>>op;
    switch(op)
        {
        case 1:
               resetearpila(menu);
               gotoxy(40,7);    getch();
               break;
        case 2:
               insertareldato(menu);
                       getch();
               break;   
        case 3:
            borrar(menu);
                        getch();
            break;
        case 4:
            mostrarpila(menu);
            getch();
            break;

        }   
    }
    while(op!=5);
}

main()
{
pila pilota;
tipodatopila pila[max];
pilota.pila=&pila[0];
menu(&pilota);
}

PILAS EN C++

ARCHIVO QUE CONTIENE LAS FUNCIONES DEL PAQUETE PILAS

/*********************************************************************************
*                            TAREA: FUNCIONES DEL PAQUETE PILAS                     *
*                            PROGRAMADOR: JULIO CESAR LEYVA RODRIGUEZ             *
*                            INSTITUTO TECNOLOGICO DE LOS MOCHIS                     *
*                            HTTP://SHALOM-NOW.BLOGSPOT.COM                         *
**********************************************************************************
#include
#include
#include
# define MAX 5

//DECLARA UN ALIAS
typedef int tipodatopila;

//GENERA UNA ESTRUCTURA UN PAQUETE DE VARIABLES EN UNA SOLA
typedef struct tipopila
{
 tipodatopila *pila;
 int tope;
};

//ESTA FUNCION INICIALIZA LA PILA
void inicializarpila(tipopila *p)
{
 p->tope=-1;
}

//ESTA FUNCION INSERTA UN DATO EN LA PILA
void push(tipopila *p ,tipodatopila dato)
{
 (p->tope)=p->tope+1;

 *(p->pila+p->tope)=dato;
}

//ESTA FUNCION TE AVISA SI LA PILA ESTA LLENA
int pilallena(tipopila *p)
{
 if (p->tope==MAX-1)
  return(1);
 else
  return(0);
}

//ESTA FUNCION TE AVISA SI LA PILA ESTA VACIA
int pilavacia(tipopila *p)
{
 if (p->tope==-1)
  return(1);
 else
  return(0);
}

//ESTA FUNCION BORRA UN ELEMENTO DE LA PILA
tipodatopila pop(tipopila *p)
{
 tipodatopila dato;
 dato=*(p->pila+p->tope);
 (p->tope)–;
 return(dato);
}

//FUNCION QUE VISUALIZA LA PILA
void imprimepila(tipopila *p)
{
 int x;
 for (x=p->tope;x>-1;x–)
 {
   cout<pila+x)<<"n";
   getch();
 }
 getch();
}

//MENU
void menu(tipopila *tipopila)
{
int op;
do
     {                           
     clrscr();
         imprimepila(tipopila);
     gotoxy(10,1);    cout<<"                  MENU";
     gotoxy(10,2);    cout<<"1. INICIALIZAR";
     gotoxy(10,3);    cout<<"2. ALTA";
     gotoxy(10,4);    cout<<"3. BAJA";
     gotoxy(10,5);    cout<<"4. VISUALIZAR";
     gotoxy(10,6);    cout<<"5. SALIR";
     gotoxy(10,7);    cout<<"    ELIJA UNA OPCION [   ]";
     gotoxy(36,7);    cin>>op;
     switch(op)
        {
        case 1:
            inicializarpila(pilita);
            getch();
                        break;
                }
     }
     while(op!=5);
}

void main()
{
tipodatopila pila[MAX];

tipopila pilita;
pilita.pila=&pila[0];
menu(&pilita);
}

Creado con WordPress & Tema de Anders Norén