Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
#include <iostream>
#include<stdio.h>
#include<stdlib.h>
#define TAM 10//en realidad el tamaño es de TAM-3, porque la posicion [0] y la ultima [9] no se utilizan
using namespace std;
class Cola
{
public:
int cola[TAM];
int frente, fin, tamanio;//tamanio es para tener un contador el cual defina el tamaño de la cola y poder ver si
esta vacia o llena mas facil
void inicializar();
int vacia();
int llena();
int enque(int e);//agregar
int denque();//eliminar
int MostrarFrente();//mostrar el elemento que se encuentra al frente(el que saldra primero)
int MostrarFin();//mostrar el ultimo elemento de la cola(el ultimo ingresado)
};
void Cola::inicializar()
{
this -> frente=0;
this -> fin=0;
this -> tamanio=0;
}
int Cola::vacia()
{
if((frente == 0 && fin == TAM /*-1*/) || (frente == fin + 1) || (frente == 0 && fin == 0))
{
return 1;//esta vacia
}
else if(tamanio == 0)
{
return 1;
}
return 0;
}
int Cola::llena()
{
if((frente == 0 && fin == TAM - 2) || (frente == TAM && fin == 1) || (frente == fin + 2))//la ultima posicion no se
utiliza :'v
{
return 1;
}
else if(tamanio == TAM - 1)
{
return 1;
}
return 0;
}
int Cola::denque()//eliminar
{
if(vacia())
{
return -1;
}
this -> tamanio--;
this -> frente = frente + 1;
if(frente == TAM - 1)
{
this -> frente = 0;
return cola[fin];//si se regresa el frente arroja basura
}
return cola[frente];//se regresa el emento eliminado
}
int main()
{
Cola c;
c.inicializar();//por si se olvida inicializar no de error :v
int opc,e;
do
{
cout << "\n \tMENU\n Cola Estatica\n"<<
" 1.Inicializar\n 2.Ver si esta vacica\n"<<
" 3.Ver si esta llena\n 4.Enque(push[agregar])\n"<<
" 5.Desque(pop[eliminar])\n 6.Frente\n"<<
" 7.Fin\n 8.Salir\n Seleccionar: ";
cin >> opc;
cin.clear();//para que al ingresar una letra no se cicle el menu ;v
system("cls");//limpiar pantalla :v
fflush(stdin);//evitar ciclado del menu por algun error de opcion
switch(opc)
{
case 1: c.inicializar();
cout << " \n Cola inicializada" << endl;
break;
case 2:
if(c.vacia())
{
cout << " \n La cola esta vacia" << endl;
}
else
{
cout << " \n La cola NO esta vacia" << endl;
}
break;
case 3:
if(c.llena())
{
cout << " \n La cola esta llena" << endl;
}
else
{
cout << " \n La cola NO esta llena" << endl;
}
break;
case 4:
cout << "\n Ingresa el elemento: ";
cin >> e;
if(c.llena())
{
cout << " \n La cola esta llena" << endl;
}
else
{
cout << " \n Elemento " << c.enque(e) << " agregado" << endl;
}
break;
case 5:
if(c.vacia())
{
cout << " \n La cola esta vacia" << endl;
}
else
{
cout << "\n \tDenque\n elemento " << c.denque() << " desencolado\n" << endl;
//si se llena y eliminas todo y vuelves a agregar a la segunda corrida arroja basura en el elemento
de frente :'v
}
break;
case 6:
if(c.vacia())
{
cout << " \n La cola esta vacia" << endl;
}
else
{
cout << " Frente: " << c.MostrarFrente() << endl;
}
break;
case 7:
if(c.vacia())
{
cout << " \n La cola esta vacia" << endl;
}
else
{
cout << " Fin: " << c.MostrarFin() << endl;
}
break;
case 8: cout << "\n Saliendo...\n" << endl;
break;
default: cout << "\n Opcion invalida..." << endl;
}
}while(opc!=8);
}
Pila estatica
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define TAM 10
using namespace std;
class pila
{
public:
int pilad[TAM];
int tope;
};
}
//agregar
int pila::push(int e, pila *P)
{
if(P->esta_lleno(P))
{
return -1;
}
else
{
tope++;
P->pilad[tope]=e;
return P->pilad[tope];
}
//Eliminar
int pila::pop(pila *P)
{
if(P->esta_vacio(P) || tope < 0 || tope > P->tope)
{
return -1;
}
else
{
P->tope--;
}
int main()
{
int op;
class pila P;
P.inicializa(&P);
do
{
switch(op)
{
case 1:
{
cout << "Se ha inicializado";
system("pause");
system("cls");
}
break;
case 2:
{
int e;
cout << "\nPUSH\n";
cout << "ingrese un elemento\n";
cin >> e;
}
system("pause");
system("cls");
}
break;
case 3:
{
cout <<"\nPOP\n";
if(P.pop(&P) == -1)
{
cout << "\nNo se agrego el elemento\n";
}
else
{
cout << "El ha eliminado el elemento\n";
}
system("pause");
system("cls");
}
break;
case 4:
{
cout <<"\nIMPRIME\n";
int pos;
if(P.esta_vacio(&P) == 1)
{
printf("La Lista esta vacia\n");
}
else
{
P.imprime(&P);
}
}
break;
case 5:
{
}
break;
default:
{
}while(op != 5);
}
Lista foblemente ligada
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
class Nodo{
public:
Nodo* sig;
Nodo* ant;
int dato;
return 1;
}
if( p -> sig == NULL ){
aux -> sig = p -> sig;
aux -> ant = p;
p -> sig = aux;
}
else{
aux -> ant = p -> ant;
aux -> sig = p;
p -> ant -> sig = aux;
p -> ant = aux;
}
}
Nodo *p=lista;
if(p->dato==x)
{
if( p == lista )
{
lista=p->sig;
lista->ant=NULL;
}
else
{
p->ant->sig=p->sig;
}
delete p;
}
p=p->sig;
}
return 1;
void Nodo::imprimir(){
Nodo *p = lista;
while(p != NULL){
cout << p->dato<<endl;
p = p->sig;
}
if ( lista == NULL )
cout << "\nLa lista esta vacia...\n";
}
int main()
{
class Nodo N;
int op, x;
do
{
switch (op)
{
case 1:
{
cout << "Ingrese el numero a ingresar\n" << endl;
cin >> x;
if(N.agregar(x) == 1)
{
cout << "Se agrego el elemento" << endl;
}
else
{
cout << "Elemento NO agregado";
}
system("pause");
system("cls");
}
break;
case 2:
{
if(N.elimina(x) == 1)
{
cout << "Elemento Eliminado" << endl;
}
else
{
cout << "No existe el elemento"<< endl;
}
system("pause");
system("cls");
}
break;
case 3:
{
cout << endl;
N.imprimir();
system("pause");
system("cls");
}
break;
case 4:
{
}
break;
default:
{
cout << "Seleccion Incorrecta" << endl;
}
system("pause");
system("cls");
}
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
class nodo
{
public:
int dato;
nodo *sig;
nodo *ant;
};
nodo *inicio=NULL;
nodo *fin=NULL;
nodo *frente=NULL;
int nodo::enquee(int x)
{
nodo *aux=new nodo;
aux->dato=x;
if(inicio == NULL)
{
aux->sig=NULL;
inicio=aux;
fin=aux;
aux->ant=NULL;
}
else
{
aux->sig=inicio;
inicio->ant=aux;
inicio=aux;
}
return 1;
}
int nodo::denquee()
{
nodo *p=fin;
fin=p->ant;
fin->sig=NULL;
delete p;
return 1;
void nodo::imprimir()
{
nodo *p=inicio;
while(p!=NULL)
{
cout<<p->dato<<endl;
p=p->sig;
void nodo::frente()
{
nodo *p = fin;
void nodo::fina()
{
nodo *p = inicio;
cout << p->dato << endl;
int main()
{
class nodo N;
int op, x;
do
{
switch(op)
{
case 1:
{
cout << "\NENQUEE"<< endl;
cout << "INGRESE UN NUMERO"<<endl;
cin >> x;
if(N.enquee(x) == 1)
{
}
break;
case 2:
{
if(N.denquee() == 1)
{
cout << "Se ha eliminado el elemento" << endl;
}
else
{
cout << "Lista vacia" << endl;
}
}
break;
case 3:
{
cout << endl;
N.imprimir();
cout << endl;
}
break;
case 4:
{
cout << endl;
N.frente();
cout << endl;
}
break;
case 5:
{
cout << endl;
N.fina();
cout << endl;
}
break;
case 6:
{
cout << "Saliendo del programa" << endl;
}
break;
default:
{
cout << "Seleccion Incorrecta"<< endl;
}
}while(op != 6);