Sei sulla pagina 1di 12

#include "lista.h" #include <stdio.h> Lista::Lista() { _cabeza=_cola=_cursor=NULL; _t=0; } Lista::~Lista() { while(!

vacia()) { borra(); } } int Lista::tama(void) { return _t; } bool Lista::vacia(void) { if (_cabeza==NULL) { return true; } else { return false; } } bool Lista::reset(void) { if (!vacia()) { _cursor=_cabeza; return true; } else { return false; } } bool Lista::final(void) {

if (!vacia()) { _cursor=_cola; return true; } else { return false; } } bool Lista::siguiente(void) { if (_cursor->siguiente!=NULL) { _cursor=_cursor->siguiente; return true; } else { return false; } } bool Lista::anterior(void) { if (_cursor->anterior!=NULL) { _cursor=_cursor->anterior; return true; } else { return false; } } bool Lista::get(int &info) { if (_cursor!=NULL) { info=_cursor->info; return true; } else { return false; } }

bool Lista::set(int valor) { if (_cursor!=NULL) { _cursor->info=valor; return true; } else { return false; } } bool Lista::inserta(int valor) { Nodo * aux = new Nodo; aux->info=valor; if (vacia()) { aux->siguiente=NULL; aux->anterior=NULL; _cabeza=_cursor=_cola=aux; } else { if((_t==1)||(_cursor==_cabeza)) { aux->anterior=NULL; aux->siguiente=_cabeza; _cursor->anterior=aux; _cabeza=_cursor=aux; } else { aux->anterior=_cursor->anterior; aux->siguiente=_cursor; _cursor->anterior->siguiente=aux; _cursor->anterior=aux; _cursor=aux; } } _t++; return true; } int Lista::insertaPrincipio(int valor) {

if(vacia()) { inserta(valor); } else { reset(); inserta(valor); } return true; } int Lista::insertaFinal(int valor) { if(vacia()) { inserta(valor); } else { Nodo * aux; aux=new Nodo; aux->info=valor; aux->anterior=_cola; aux->siguiente=NULL; _cola->siguiente=aux; _cola=_cursor=aux; _t++; } return true; } bool Lista::borra(void) { if(vacia()) { return false; } else { if(_t==1) { delete _cursor; _cursor=_cabeza=_cola=NULL; } else { if(_cursor==_cabeza)

{ _cursor=_cursor->siguiente; _cursor->anterior=NULL; delete _cabeza; _cabeza=_cursor; } else { if(_cursor==_cola) { _cursor=_cursor->anterior; _cursor->siguiente=NULL; delete _cola; _cola=_cursor; } else { Nodo * aux; _cursor->anterior->siguiente=_cursor->siguiente; _cursor->siguiente->anterior=_cursor->anterior; aux=_cursor->siguiente; delete _cursor; _cursor=aux; } } } } _t--; return true; } 2__________________________________________________________________________ #ifndef LISTA_H #define LISTA_H #endif /* TAD Lista DESCRIPCION: El principal objetivo es contrastar el TAD Lista con la estructura de datos dinmica lista, y sus operaciones. OPERACIONES: */ struct Nodo

{ int info; struct Nodo * siguiente; struct Nodo * anterior; }; class Lista { private: Nodo *_cabeza,*_cola,*_cursor; int _t; //nmero de nodos de la lista public: Lista(); //constructor de la clase ~Lista(); //destructor de la clase int tama(void); bool vacia(void); bool reset(void); bool final(void); bool siguiente(void); bool anterior(void); bool get(int &info); bool set(int valor); bool inserta(int valor); int insertaPrincipio(int valor); int insertaFinal(int valor); bool borra(void); }; 3________________________________________________________________________ #include <iostream> #include "lista.cpp" using namespace std; int menu(void); int pedirValor(void); int main() { int opcion=0,valor=0,salir=0; Lista l; do{ opcion=menu(); switch(opcion) { case 1: { valor=pedirValor();

l.inserta(valor); break; } case 2: { l.borra(); break; } case 3: { cout << "El tamao de la lista es: " << l.tama() << endl << endl; break; } case 4: { if(!l.vacia()) { l.reset(); cout << "Lista: "; for(int i=0;i<l.tama();i++) { l.get(valor); cout << valor << " "; l.siguiente(); } cout << endl << endl; } else { cout << "La lista est vaca." << endl << endl; } break; } case 5: { l.get(valor); cout << "El valor actual es: " << valor << endl << endl; break; } case 6: { valor=pedirValor(); l.set(valor); break; } case 7: { l.reset(); l.get(valor);

cout << "Actual: " << valor << endl << endl; break; } case 8: { l.final(); l.get(valor); cout << "Actual: " << valor << endl << endl; break; } case 9: { l.siguiente(); l.get(valor); cout << "Actual: " << valor << endl << endl; break; } case 10: { l.anterior(); l.get(valor); cout << "Actual: " << valor << endl << endl; break; } case 11: { valor=pedirValor(); l.insertaPrincipio(valor); break; } case 12: { valor=pedirValor(); l.insertaFinal(valor); break; } case 0: { salir=1; break; } } }while(salir!=1); } int menu(void) { int opcion=0;

do{ cout << "****************************************************************************" << endl; cout << "1. Insertar elemento." << endl; cout << "2. Borrar elemento." << endl; cout << "3. Contar elementos." << endl; cout << "4. Listar todos los elementos." << endl; cout << "5. Leer actual." << endl; cout << "6. Modificar actual." << endl; cout << "7. Ir al principio." << endl; cout << "8. Ir al final." << endl; cout << "9. Ir al siguiente." << endl; cout << "10. Ir al anterior." << endl; cout << "11. Insertar al principio." << endl; cout << "12. Insertar al final." << endl; cout << "0. Salir." << endl; cout << "****************************************************************************" << endl; cout << "Introduce una opcin: "; cin >> opcion; cout << endl; }while((opcion<0)||(opcion>12)); return opcion; } int pedirValor(void) { int valor; do{ cout << "Inserta valor: "; cin >> valor; }while((valor<0)||(valor>99)); cout << endl; return valor; } 1________________________________________________________ #include <cstdio> #include <stack> #include <iostream> using namespace std;

int main() { // stack <tipo, en este caso, entero> nombre_de_la_pila; stack <int> s; // insertamos tres elementos s.push(1); s.push(2); s.push(3); // el ultimo elemento insertado debe salir primero (LIFO): 3, 2, 1 while(!s.empty()) { cout << s.top() << endl; s.pop(); } cout << "\n" << endl; cin.get(); } 2______________________________________________________________ #include <iostream> using namespace std; class nodo { public: nodo(int v, nodo *sig = NULL) { valor = v; siguiente = sig; } private: int valor; nodo *siguiente; friend class pila; }; typedef nodo *pnodo; class pila { public: pila() : ultimo(NULL) {} ~pila(); void Push(int v); int Pop(); private: pnodo ultimo; };

pila::~pila() { while(ultimo) Pop(); } void pila::Push(int v) { pnodo nuevo; /* Crear un nodo nuevo */ nuevo = new nodo(v, ultimo); /* Ahora, el comienzo de nuestra pila es en nuevo nodo */ ultimo = nuevo; } int pila::Pop() { pnodo nodo; /* variable auxiliar para manipular nodo */ int v; /* variable auxiliar para retorno */ if(!ultimo) return 0; /* Si no hay nodos en la pila retornamos 0 */ /* Nodo apunta al primer elemento de la pila */ nodo = ultimo; /* Asignamos a pila toda la pila menos el primer elemento */ ultimo = nodo->siguiente; /* Guardamos el valor de retorno */ v = nodo->valor; /* Borrar el nodo */ delete nodo; return v; } int main() { pila Pila; Pila.Push(20); cout << "Push(20)" << endl; Pila.Push(10); cout << "Push(10)" << endl; cout << "Pop() = " << Pila.Pop() << endl; Pila.Push(40); cout << "Push(40)" << endl; Pila.Push(30); cout << "Push(30)" << endl; cout << "Pop() = " << Pila.Pop() << endl; cout << "Pop() = " << Pila.Pop() << endl; Pila.Push(90); cout << "Push(90)" << endl; cout << "Pop() = " << Pila.Pop() << endl; cout << "Pop() = " << Pila.Pop() << endl;

cin.get(); return 0; } 3__________________________________________________________________________ #include <cstdlib> #include <iostream> #include <queue> #include <vector> using namespace std; // declaracin de tipo typedef priority_queue<string, vector<string>, greater<string> > STRPQUE; int main(int argc, char *argv[]) { vector<string> v; v.push_back("pera"); v.push_back("uva"); v.push_back("manzana"); v.push_back("banana"); v.push_back("coco"); vector<string>::iterator it = v.begin(); cout << "Contenido del vector" << endl; while (it != v.end() ) cout << "\t" << *it++ << endl; STRPQUE p( v.begin(), v.end() ); cout << "\nContenido de la priority_queue" << endl; while (! p.empty() ) { cout << "\t" << p.top() << endl; p.pop(); } cout << endl; system("PAUSE"); return EXIT_SUCCESS; }

Potrebbero piacerti anche