Sei sulla pagina 1di 16

Cola circular

#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::enque(int e)//agregar


{
if(llena())
{
return -1;
}
this -> tamanio++;
this -> fin = fin + 1;
this -> cola[fin] = e;
return cola[fin];//se regresa el elemento agregado
}

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 Cola::MostrarFrente()//el elemento que saldra primero


{
if(vacia())
{
return -1;
}
return cola[frente + 1];//+1 porque en la posicion 0 muestra basura :'v// y si no lo tiene sigue mostrando el
elemento eliminado
}

int Cola::MostrarFin()//el ultimo elemento ingresado


{
if(vacia())
{
return -1;
}
return cola[fin];//se muestra el ultimo elemento de la cola
}

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;

void inicializa(pila *P);


int push(int e, pila *P);
int esta_vacio(pila *P);
int esta_lleno(pila *P);
int pop(pila *P);
int imprime(pila *P);

};

void pila::inicializa(pila *P)


{
this->tope = -1;

}
//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];

int pila::esta_vacio(pila *P)


{
if(P->tope == -1)
{
return 1;
}
else
{
return 0;
}

int pila::esta_lleno(pila *P)


{
if(P->tope == TAM -1)
{
return 1;
}
else
{
return 0;
}

}
//Eliminar
int pila::pop(pila *P)
{
if(P->esta_vacio(P) || tope < 0 || tope > P->tope)
{
return -1;
}
else
{

P->tope--;
}

int pila::imprime(pila *P)


{
int i;
if(P->esta_vacio(P))
{
return -1;
}
else
{
for (i = 0; i <= P->tope; i++)
{
printf("posicion: %i dato: %i \n",i, P->pilad[i]);
}
}
}

int main()
{
int op;
class pila P;
P.inicializa(&P);

do
{

cout << "\nPILAS\n";


cout << "1. Inicializa\n";
cout << "2. push\n"; // AGREGAR
cout << "3. pop\n"; //ELIMINAR
cout << "4. tope\n"; // VER ULTIMO
cout << "5. Salir\n";
cout << "Seleccione una opcion\n";
cin >> op;

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;

if(P.push(e, &P) == -1)


{
cout << "\nNo se agrego el elemento\n";
}
else
{
cout << "El ha agregado el elemento\n";

}
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:
{

cout << "Seleccion Incorrecta";


}

}while(op != 5);

}
Lista foblemente ligada

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

class Nodo{
public:

Nodo* sig;
Nodo* ant;
int dato;

int agregar( int );


int elimina( int );
void imprimir();
};

Nodo *lista = NULL;

int Nodo::agregar( int x ){


Nodo *p = lista;
Nodo *aux = new Nodo;
aux -> dato = x;

if( lista == NULL ){


lista = aux;
aux -> ant = NULL;
aux -> sig = NULL;
return 1;
}

while( p -> sig != NULL && p -> dato < x ){


p = p -> sig;
}
if( p == lista ){
aux -> sig = lista;
aux -> ant = NULL;
lista -> ant = aux;
lista = aux;

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

int Nodo::elimina( int x )


{

Nodo *p=lista;

while(p!=NULL && p->dato<=x)


{

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
{

cout <<"\n\nLISTA DOBLEMENTE LIGADA\n" << endl;


cout <<"1. AGREGAR\n" << endl;
cout <<"2. ELIMINAR\n" << endl;
cout <<"3. MOSTRAR\n" << endl;
cout <<"4. SALR\n" << endl;
cout <<"\nSELECCIONE UNA OPCION\n" << endl;
cin >> op;

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:
{

cout << "Ingrese el dato a eliminar\n" << endl;


cin >> x;

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:
{

cout << "Saliendo del programa" << endl;

}
break;
default:
{
cout << "Seleccion Incorrecta" << endl;
}
system("pause");
system("cls");
}

}while (op != 4);


}
Cola nodos

#include <iostream>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

class nodo
{

public:
int dato;
nodo *sig;
nodo *ant;

int enquee(int x);


int denquee();
void imprimir();
void frente();
void fina();

};

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;

cout << p->dato << endl;


}

void nodo::fina()
{
nodo *p = inicio;
cout << p->dato << endl;

int main()
{
class nodo N;
int op, x;

do
{

cout<< "COLA" << endl;


cout<< "1.ENQUEE" << endl;
cout<< "2. DENQUEE" << endl;
cout<< "3. IMPRIMIR" << endl;
cout<< "4. FRENTE" << endl;
cout << "5.FINAL"<< endl;
cout<< "6. SALIR" << endl;
cout<< "SELECCIONE UNA OPCION" << endl;
cin>> op;

switch(op)
{
case 1:
{
cout << "\NENQUEE"<< endl;
cout << "INGRESE UN NUMERO"<<endl;
cin >> x;

if(N.enquee(x) == 1)
{

cout << "Se ha agregado elemento" << endl;


}
else
{
cout << "Lista vacia" << endl;

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

Potrebbero piacerti anche