Está en la página 1de 22

Actividad de Aprendizaje 10.

La Lista, implementación dinámica


doblemente ligada

-Datos del alumno: Luis Alberto Martínez Valdez.

-Código: 219114813.

-Datos de la materia: Estructuras de Datos I.

-Fecha de elaboración: 05/09/2022.

CONCEPTO SI NO ACOMULACION
Bajé el trabajo de
internet o alguien me -100pts 0pts 0
lo pasó (aunque sea
de forma parcial)
Incluí el código fuente
en formato de texto +25pts 0pts 25
(sólo si funciona
cumpliendo todos los
requerimientos)
Incluí las impresiones
de pantalla (sólo si +25pts 0pts 25
funciona cumpliendo
todos los
requerimientos)
Incluí una portada que
identifica mi trabajo +25pts 0pts 25
(nombre, código,
materia, fecha, título)

Incluí una descripción


y conclusiones de mi +25pts 0pts 25
trabajo
100pts
Introducción acerca de la forma en que fue abordado el problema:
Se me dificulto un poco el entender como implementar los punteros a la actividad, pero lo
bueno fue, que era utilizando la actividad anterior que, aunque no la tenía terminada, la finalice
y me apoye con algunos videos o sugerencias de un amigo.

CODIGO FUENTE:
Listexception.hpp
#ifndef LISTEXCEPTION_HPP_INCLUDED

#define LISTEXCEPTION_HPP_INCLUDED

#include<exception>

#include<string>

class ListException : public std::exception {

private:

std::string msg;

public:

explicit ListException(const char* message) : msg(message) { }

explicit ListException(const std::string& message) : msg(message) { }

virtual ~ListException() throw () { }

virtual const char* what() const throw () {

return msg.c_str();

};

#endif

songNode.hpp
#ifndef SONGNODE_HPP_INCLUDED

#define SONGNODE_HPP_INCLUDED

#include <string>

class SongNode {
private:

std::string name;

std::string interpreter;

std::string autor;

std::string file;

int ranking;

SongNode* next;

SongNode* prev;

public:

SongNode();

SongNode(const &SongNode);

std::string getName() const;

std::string getInterpreter() const;

std::string getAutor() const;

std::string getFile() const;

int getRanking() const;;

SongNode* getNext() const;

SongNode* getPrev() const;

std::string toString() const;

void setName(const std::string&);

void setInterpreter(const std::string&);

void setAutor(const std::string&);

void setFile(const std::string&);

void setRanking(const int&);

void setNext(SongNode*);

void setPrev(SongNode*);

bool operator == (const SongNode&) const;

};
#endif // SONGNODE_HPP_INCLUDED

songNode.cpp
#include "songNode.hpp"

using namespace std;

SongNode::SongNode() : next(nullptr), prev(nullptr) {

SongNode::SongNode(const& SongNode) : next(nullptr), prev(nullptr) {

string SongNode::getName() const {

return name;

string SongNode::getInterpreter() const {

return interpreter;

string SongNode::getAutor() const {

return autor;

string SongNode::getFile() const {

return file;

int SongNode::getRanking() const {

return ranking;
}

SongNode* SongNode::getNext() const {

return next;

SongNode* SongNode::getPrev() const

return prev;

string SongNode::toString() const {

string cadenaDatos;

cadenaDatos+="\t| ";

cadenaDatos+=to_string(ranking);

cadenaDatos+="| ";

cadenaDatos+=name;

cadenaDatos+="| ";

cadenaDatos+=interpreter;

cadenaDatos+="| ";

cadenaDatos+=autor;

cadenaDatos+="| ";

cadenaDatos+=file;

cadenaDatos+="|";

return cadenaDatos;

void SongNode::setName(const string& n) {

name = n;

void SongNode::setInterpreter(const string& i) {


interpreter = i;

void SongNode::setAutor(const string& a) {

autor = a;

void SongNode::setFile(const string& f) {

file = f;

void SongNode::setRanking(const int& r) {

ranking = r;

void SongNode::setNext(SongNode* n) {

next = n;

void SongNode::setPrev(SongNode* p)

prev = p;

bool SongNode::operator == (const SongNode& s) const {

return (name == s.name) || (interpreter == s.interpreter);

Lista.hpp
#ifndef LIST_HPP_INCLUDED

#define LIST_HPP_INCLUDED

#include <string>

#include "songNode.hpp"
class List {

private:

SongNode* anchor;

void copyAll(const List&);

bool isValidPos(SongNode*) const;

public:

typedef SongNode* Position;

List();

List(const List&);

~List();

bool isEmpty() const;

void addSong(SongNode*, const SongNode&, int&);

void deleteData(SongNode*);

SongNode* getFirstPos() const;

SongNode* getLastPos() const;

SongNode* getPrevPos(SongNode*) const;

SongNode* getNextPos(SongNode*) const;

SongNode* findData(const SongNode&) const;

std::string retrieve(SongNode*) const;

std::string toString() const;

void deleteAll();
List& operator = (const List&);

};

#endif // LIST_HPP_INCLUDED

Lista.cpp
#include "listexception.hpp"

#include "list.hpp"

using namespace std;

void List::copyAll(const List& l) {

SongNode* aux (l.anchor);

SongNode* last(nullptr);

SongNode* newSongNode;

while(aux != nullptr) {

newSongNode = new SongNode(*aux);

if(aux == nullptr)

throw ListException("Memoria no disponible, copyAll");

if(last == nullptr)

anchor = newSongNode;

else

newSongNode ->setPrev(last);

last->setNext(newSongNode);

bool List::isValidPos(SongNode* p) const {

SongNode* aux(anchor);
while(aux != nullptr) {

if(aux == p)

return true;

aux = aux->getNext();

return false;

List::List() : anchor(nullptr) {}

List::List(const List& l) : anchor(nullptr) {

copyAll(l);

List::~List() {

deleteAll();

bool List::isEmpty() const {

return anchor == nullptr;

void List::addSong(SongNode* p, const SongNode& e, int& op) {

if(p != nullptr and !isValidPos(p)) {

throw ListException("Posicion invalida, instertData");

SongNode* aux(new SongNode);

*aux = e;

if(aux == nullptr)

throw ListException("Memoria no disponible, instertData");


if(op == 1) {

if(p == nullptr) {

aux->setNext(anchor);

aux->setPrev(nullptr);

anchor = aux;

else {

aux->setNext(nullptr);

aux->setPrev(p);

p->setNext(aux);

if(op == 2) {

aux->setNext(anchor);

aux->setPrev(nullptr);

if(anchor != nullptr) {

anchor->setPrev(aux);

anchor = aux;

if(op == 3) {

aux->setPrev(p);

aux->setNext(p->getNext());

if(p->getNext() != nullptr) {

p->getNext()->setPrev(aux);

p->setNext(aux);

}
}

void List::deleteData(SongNode* p) {

if(!isValidPos(p))

throw ListException("Posicion invalida, DeleteData");

if(p->getPrev() != nullptr)///religado previo del posicion dada

p->getPrev()->setNext(p->getNext());

if(p->getNext() != nullptr)///religado siguiente de posicion dada

p->getNext()->setPrev(p->getPrev());

if(p == anchor)///eliminar primero

anchor = anchor->getNext();

delete p;

SongNode* List::getFirstPos() const {

return anchor;

SongNode* List::getLastPos() const {

if(isEmpty())

return nullptr;

SongNode* aux(anchor);

while(aux->getNext() != nullptr) {

aux = aux->getNext();

return aux;

}
SongNode* List::getPrevPos(SongNode* p) const {

if(!isValidPos(p))

return nullptr;

return p->getPrev();

SongNode* List::getNextPos(SongNode* p) const {

if(!isValidPos(p))

return nullptr;

return p->getNext();

SongNode* List::findData(const SongNode& e) const {

SongNode* aux(anchor);

while(aux != nullptr and !(*aux == e) ) {

aux = aux->getNext();

return aux;

string List::retrieve(SongNode* p) const {

if(!isValidPos(p))

throw ListException("Posicion invalida, retrieve");

return p->toString();

string List::toString() const {

int pos(0);

SongNode* aux(anchor);

string result;
while(aux != nullptr) {

result+=to_string(pos++) + aux->toString() + "\n";

aux = aux->getNext();

return result;

void List::deleteAll() {

SongNode* aux;

while(anchor != nullptr) {

aux = anchor;

anchor = anchor->getNext();

delete aux;

List& List::operator = (const List& l) {

deleteAll();

copyAll(l);

return *this;

Menu.hpp
#ifndef MENU_HPP_INCLUDED

#define MENU_HPP_INCLUDED
#include"list.hpp"

class Menu {

private:

public:

void mainMenu(List&);

};

#endif // MENU_HPP_INCLUDED

Menu.cpp
#include<iostream>

#include <random>

#include <chrono>

#include <string>

#include"menu.hpp"

#include"songNode.hpp"

#include"list.hpp"

using namespace std;

void Menu::mainMenu(List& theList) {

int i,ii,op,item;

string str;

SongNode theSong;

List::Position pos;

do {

cout<<theList.toString();
cout<<"\n\t MENU\n1)Inserta\t3)Anula\n2)Elimina\t4)Busqueda\n\t5)Salir\n\nSelecciona una opcion: ";

cin>>op;

system("cls");

auto semilla=chrono::system_clock::now().time_since_epoch().count();

default_random_engine generator(semilla);

switch(op) {

case 1: {

cout<<"\nInsertar?\n\n1)Al final 2)Al inicio 3)Otro lado\n\nSelecciona una opcion: ";

fflush(stdin);

cin>>op;

system("cls");

if(op == 1) {///AL FINAL

uniform_int_distribution<int>distribution(0,9999);///Ranking

int rnum = distribution(generator);

theSong.setRanking(rnum);

cout<<"\nNombre: ";

fflush(stdin);

getline(cin,str);

theSong.setName(str);

cout<<"\nInterprete: ";

fflush(stdin);

getline(cin,str);

theSong.setInterpreter(str);

cout<<"\nAutor: ";

fflush(stdin);

getline(cin,str);

theSong.setAutor(str);
cout<<"\nArchivo.mp3: ";

fflush(stdin);

getline(cin,str);

theSong.setFile(str);

theList.addSong(theList.getLastPos(),theSong, op);

if(op == 2) { ///AL INICIO

uniform_int_distribution<int>distribution(0,9999);///Ranking

int rnum = distribution(generator);

theSong.setRanking(rnum);

cout<<"\nNombre: ";

fflush(stdin);

getline(cin,str);

theSong.setName(str);

cout<<"\nInterprete: ";

fflush(stdin);

getline(cin,str);

theSong.setInterpreter(str);

cout<<"\nAutor: ";

fflush(stdin);

getline(cin,str);

theSong.setAutor(str);

cout<<"\nArchivo.mp3: ";

fflush(stdin);

getline(cin,str);

theSong.setFile(str);

theList.addSong(theList.getFirstPos(),theSong, op);
}

if(op == 3) {

cout<<theList.toString();

cout<<"\n\n(Insertar despues del punto de interes)";

cout<<"\nNombre de la cancion: ";

fflush(stdin);

getline(cin, str);

theSong.setName(str);

theSong.setInterpreter(str);

pos = theList.findData(theSong);

uniform_int_distribution<int>distribution(0,9999);

int rnum = distribution(generator);

theSong.setRanking(rnum);

cout<<"\nNombre: ";

fflush(stdin);

getline(cin,str);

theSong.setName(str);

cout<<"\nInterprete: ";

fflush(stdin);

getline(cin,str);

theSong.setInterpreter(str);

cout<<"\nAutor: ";

fflush(stdin);

getline(cin,str);

theSong.setAutor(str);

cout<<"\nArchivo.mp3: ";

fflush(stdin);

getline(cin,str);
theSong.setFile(str);

theList.addSong(pos,theSong, op);

system("cls");

break;

case 2:

cout<<theList.toString();

cout<<"\n\nNombre de la cancion: ";

fflush(stdin);

getline(cin, str);

theSong.setName(str);

theSong.setInterpreter(str);

pos = theList.findData(theSong);

cout<<"La cancion con el nombre: "<<str<<"";

if(pos == nullptr)

cout<<"NO SE ENCUENTRA EN LA LISTA...";

theList.deleteData(pos);

system("cls");

break;

case 3:

theList.deleteAll();

//system("pause");

system("cls");

break;
case 4:

cout<<"\nBuscar por??\n\n1)Cancion 2)Interprete\n\nSelecciona una opcion: ";

fflush(stdin);

cin>>op;

system("cls");

if(op == 1) {

cout<<"Nombre de la cancion: ";

fflush(stdin);

getline(cin,str);

theSong.setName(str);

pos = theList.findData(theSong);

if(pos == nullptr) {

cout<<"\nLa cancion no existe...\n\n";

else

cout<<"\n\n"<<theList.retrieve(pos);

if(op == 2) {

cout<<"Nombre del interprete: ";

fflush(stdin);

getline(cin,str);

theSong.setInterpreter(str);

theSong.setName(str);

pos = theList.findData(theSong);

if(pos == nullptr) {

cout<<"\nEl interprete no existe...\n\n";


}

else

cout<<"\n\n"<<theList.retrieve(pos);

cout<<endl<<endl;

system("pause");

system("cls");

break;

case 5:

break;

default:

cout<<"Opcion invalida...\n\n";

system("pause");

system("cls");

while(op != 5);

system("cls");

cout<<"\n\n"<<"FINALIZANDO PROGRAMA..."<<"\n"<<endl;

Main.cpp
#include "menu.hpp"

int main() {

Menu theMenu;

List theList;

theMenu.mainMenu(theList);
return 0;

Impresiones de pantallas que muestren la ejecución satisfactoria del programa:


Resumen personal del trabajo realizado: programa de lista de canciones, se me hizo un poco
complicado, aun no termino de entender todo muy bien espero seguir mejorando :p.

También podría gustarte