19 abril 2020

Lista Simple en C++

Exported from Notepad++
#include<iostream> #include<string> #include<stdlib.h> using namespace std; class Nodo { private: string Valor; Nodo *Siguiente; public: Nodo(string v, Nodo*sig=NULL){ this->Valor=v; this->Siguiente=sig; } friend class lista; }; class lista { private: Nodo *primero; Nodo *actual; public: lista(){this->primero=NULL; this->actual=NULL; } void insertar(string v); void buscar(string n); void eliminar(); bool listavacia() { return (this->primero==NULL); } void mostrar(); }; void lista::insertar(string v) { Nodo *nuevo=new Nodo(v); if (listavacia()){ this->primero=nuevo; } else { this->actual->Siguiente = nuevo; } ; this->actual=nuevo; } void lista::mostrar(){ Nodo *temp=this->primero; while(temp){ cout<<temp->Valor<<"-->"; temp=temp->Siguiente; } cout<<"NULL\n"; } void lista::buscar(string n) { Nodo *temp=this->primero; while(temp && temp->Valor!=n){ temp=temp->Siguiente; } if(temp){ cout<<"Encontrado dato "<<temp->Valor<<" en esta lista\n"; } else{ cout<<"No se encontro ningun dato\n"; } } int main() { lista l; int opi; string val,val2; do{ system("cls"); cout<<"Lista enlazada simple\n"; cout<<"Que quiere hacer:\n 1-INSERTAR,\n 2-BUSCAR,\n 3-MOSTRAR\n"; cin>>opi; cin.get(); switch(opi) { case 1: system("cls"); cout<<"Ingrese valor "; cin>>val; l.insertar(val);break; case 2: system("cls"); cout<<"Ingrese valor a buscar "; cin>>val2; l.buscar(val2);break; case 3: system("cls"); l.mostrar();break; default: cout<<"Opción no valida\n"; return 0;break; } system("pause"); } while(true); }

0 comentarios: