OneCompiler

Lab 2 edda Queue y LinkedList

148

import java.util.Random;
import java.util.Stack;

public class Lab2EddaQueueAndLinkedList {
public static class ListaEnlazada<Song> {
public class Node {
Song value;
Node next;
}

    Node head;

    public void Insertar(Song valor) {
        if (head == null) {
            head = new Node();
            head.value = valor;
        } else {
            Node aux = head;
            while (aux.next != null) {
                aux = aux.next;
            }
            aux.next = new Node();
            aux.next.value = valor;
        }
    }

    // metodo para eliminar nodo que tenga cierto objeto
    public void remover(Song objeto) {
        if (head.value.equals(objeto)) {
            head = head.next;
        } else {
            Node aux = head;
            while (aux != null) {
                if (aux.next.value.equals(objeto)) {
                    aux.next = aux.next.next;
                }
                aux = aux.next;
            }
        }
    }

    // imprimir con objetivos de prueba
    public void Imprimir() {
        Node aux = head;
        while (aux != null) {
            System.out.println(aux.value);
            aux = aux.next;
        }
    }

}

public static class Queue<Song> {
    private int queueLenght = 0;
    private ListaEnlazada<Song> items = new ListaEnlazada<Song>();

    public void add(Song objeto) {
        queueLenght++;
        if (items.head == null) {
            items.head = items.new Node();
            items.head.value = objeto;
        } else {
            ListaEnlazada<Song>.Node aux = items.head;
            while (aux.next != null) {
                aux = aux.next;
            }
            aux.next = items.new Node();
            aux.next.value = objeto;
        }
    }

    public boolean isEmpty() {
        if (queueLenght == 0) {
            return true;
        } else {
            return false;
        }
    }

    public Song poll() {
        if (isEmpty() == true) {
            return null;
        } else {
            Song aux = items.head.value;
            items.head = items.head.next;
            queueLenght--;
            return aux;
        }
    }

    public void Remover(int index) {
        if (index == 0) {
            items.head = items.head.next;
            queueLenght--;
            return;
        }
        ListaEnlazada<Song>.Node aux = items.head;
        int contador = 0;
        while (aux != null) {
            if (index - 1 == contador) {
                aux.next = aux.next.next;
                queueLenght--;
                return;
            }
            contador++;
            aux = aux.next;
        }
    }

    public Song getItem(int index) {
        ListaEnlazada<Song>.Node aux = items.head;
        int contador = 0;
        while (aux.value != null) {
            if (contador == index) {
                return aux.value;
            }
            aux = aux.next;
            contador++;
        }
        return null;
    }
}

public static class Stack<Song> {
    ListaEnlazada<Song> items = new ListaEnlazada<Song>();
    int StackLenght = 0;

    public void Añadir(Song nuevo){
        items.Insertar(nuevo);
        StackLenght++;
    }
    
    public Song Pop(){
        ListaEnlazada<Song>.Node aux = items.new Node();
        while (aux.next != null){
            aux = aux.next;
        }
        Song retornar = aux.value;
        aux.value = null;
        StackLenght--;
        return retornar;
    }

    public boolean isEmpty(){
        if(StackLenght == 0){
            return true;
        }
        else{
            return false;
        }
    }
    //dado que no se utilizan los metodos, se implementan los metodos mas usados de Stack
}

public static class Song {
    private String nombre;
    private String artista;
    private String album;
    private int duracion;

    public Song(String n, String a, String al, int d) {
        nombre = n;
        artista = a;
        album = al;
        duracion = d;
    }

}

public static class Playlist {
    private String nombre;
    // ---------------------------implementado para pruebas
    private Song[] cancionesA = new Song[1];
    int largo = 0;

    // ---------------------------
    private ListaEnlazada<Song> cancionesL = new ListaEnlazada<>();

    // implementado para pruebas
    public void agregarCancionPlaylist_A(Song nueva) {
        if (largo == 0) {
            largo++;
            cancionesA[0] = nueva;
        } else {
            largo++;
            Song[] aux = new Song[(cancionesA.length + 1)];
            for (int i = 0; i < cancionesA.length; i++) {
                aux[i] = cancionesA[i];
            }
            aux[aux.length - 1] = nueva;
            cancionesA = aux;
        }
    }

    // metodos utiles
    public Song getSong_A(int index) {
        return cancionesA[index];
    }

    // agregar cancion con lista enlazada
    public void agregarCancionPlaylist_L(Song nueva) {
        cancionesL.Insertar(nueva);
    }

    // quitar cancion playlist ListaEnlazada
    public void quitarCancionPlaylist_L(Song remove) {
        cancionesL.remover(remove);
    }

    // editar playlist con linked list
    public void editarPlaylist_L(int h, int k) {
        ListaEnlazada<Song>.Node valorH = cancionesL.new Node();
        ListaEnlazada<Song>.Node valorK = cancionesL.new Node();
        ListaEnlazada<Song>.Node aux = cancionesL.head;
        int contador = 0;
        while (aux != null) {
            if (contador == h) {
                valorH.value = aux.value;
            } else if (contador == k) {
                valorK.value = aux.value;
            }
            aux = aux.next;
            contador++;
        }
        aux = cancionesL.head;
        while (aux != null) {
            if (aux.value.equals(valorH.value)) {
                aux.value = valorK.value;
            } else if (aux.value.equals(valorK.value)) {
                aux.value = valorH.value;
            }
            aux = aux.next;
        }
    }
}

public static class Reproductor {
    private Playlist playlists = new Playlist();
    private Queue<Song> cola_reproduccion_C = new Queue<Song>();

    // agregar cancion con cola
    public void agregarCancionCola_C(Song nueva) {
        cola_reproduccion_C.add(nueva);
    }

    // quitar cancion con lista enlazada
    public void quitarCancionCola_L(Song remove) {
        ListaEnlazada<Song> aux = new ListaEnlazada<>();
        while (!cola_reproduccion_C.isEmpty()) {
            aux.Insertar(cola_reproduccion_C.poll());
        }
        // la cola funciona de forma FIFO y la manera en la que recorro mi linked list
        // tambien, por lo que tiene sentido esta implementacion
        aux.remover(remove);
        ListaEnlazada<Song>.Node auxHead = aux.head;
        while (auxHead != null) {
            cola_reproduccion_C.add(auxHead.value);
            auxHead = auxHead.next;
        }
    }

    // reproducir cola en orden con QUEUE
    public void reproducirColaEnOrden_C(Queue<Song> cola) {
        while (!cola.isEmpty()) {
            Song cancion = cola.poll();
            System.out.println(cancion.nombre);
        }
    }

    // reproducir cola en orden con lista enlazada
    public void reproducirColaEnOrden_L(ListaEnlazada<Song> Lista) {
        ListaEnlazada<Song>.Node aux = Lista.head;
        while (aux.value != null) {
            Song cancion = aux.value;
            // considerar que imprimirla seria reproducirla
            System.out.println(cancion.nombre);
            aux = aux.next;
        }
    }

    // reproducir playlist en orden COLA
    public void reproducirPlaylistEnOrden_C(Playlist playlist) {
        Queue<Song> aux = new Queue();
        int largo = playlist.cancionesA.length;
        for (int i = 0; i < largo; i++) {
            aux.add(playlist.getSong_A(i));
        }
        Song cancion = aux.poll();
        System.out.println(cancion.nombre);
    }

    // reproducir playlist en orden ListaEnlazada
    public void reproducirPlaylistEnOrden_L(Playlist playlist) {
        ListaEnlazada<Song>.Node aux = playlist.cancionesL.head;
        while (aux.value != null) {
            Song cancion = aux.value;
            System.out.println(cancion.nombre);
            aux = aux.next;
        }
    }

    // reproducir playlist aleatoriamente COLA
    public void reproducirAleatoriamente_C(Playlist playlist) {
        // al no haber cola en la variable playlist, tomare el arreglo y lo convertire
        // en una cola
        Queue<Song> aux = new Queue<>();
        int largo = playlist.cancionesA.length;
        for (int i = 0; i < largo; i++) {
            aux.add(playlist.getSong_A(i));
        }
        Random rand = new Random();
        while (!aux.isEmpty()) {

            int limit = aux.queueLenght;
            int random_number = rand.nextInt(limit);
            Song cancion = aux.getItem(random_number);
            System.out.println(cancion.nombre);
            aux.Remover(random_number);

        }
    }

    // reproducir intercalando aleatorio
    public void reproducirIntercalandoAleatorio(Playlist play1, Playlist play2) {
        Queue<Song> cola = new Queue<>();
        for (int i = 0; i < play1.cancionesA.length; i++) {
            cola.add(play1.cancionesA[i]);
        }
        for (int i = 0; i < play2.cancionesA.length; i++) {
            cola.add(play2.cancionesA[i]);
        }
        // al no especificar el ejercicio, usare un aux de cola donde introducire todos
        // los elementos de la playlist 1 y 2, considerando que estos elementos esten en
        // arreglos
        // ahora, reproducimos todos los elementos de la cola aleatoriamente
        Random rand = new Random();
        while (!cola.isEmpty()) {
            if (cola.queueLenght == 1) {
                Song cancion = cola.poll();
                System.out.println(cancion.nombre);
            } else {
                int limit = cola.queueLenght - 1;
                int random_number = rand.nextInt(limit);
                Song cancion = cola.getItem(random_number);
                System.out.println(cancion.nombre);
                cola.Remover(random_number);
            }
        }
    }
}

public static void main(String[] args) {

    Song song1 = new Song("Shape of You", "Ed Sheeran", "Divide", 234);
    Song song2 = new Song("Bohemian Rhapsody", "Queen", "A Night at the Opera", 355);
    Song song3 = new Song("Rolling in the Deep", "Adele", "21", 228);
    Song song4 = new Song("Smells Like Teen Spirit", "Nirvana", "Nevermind", 301);

    Playlist myPlaylist = new Playlist();
    myPlaylist.agregarCancionPlaylist_A(song1);
    myPlaylist.agregarCancionPlaylist_A(song2);
    myPlaylist.agregarCancionPlaylist_A(song3);
    myPlaylist.agregarCancionPlaylist_A(song4);

    Queue<Song> myQueue = new Queue<>();
    myQueue.add(song1);
    myQueue.add(song2);
    myQueue.add(song3);
    myQueue.add(song4);

    Reproductor reproductor = new Reproductor();
    // Llamar al método reproducirColaEnOrden_C desde la instancia de Reproductor
    reproductor.reproducirAleatoriamente_C(myPlaylist);
    System.out.println();

    Song song5 = new Song("Imagine", "John Lennon", "Imagine", 195);
    Song song6 = new Song("Hotel California", "Eagles", "Hotel California", 390);
    Song song7 = new Song("Stairway to Heaven", "Led Zeppelin", "Led Zeppelin IV", 480);
    Song song8 = new Song("Hey Jude", "The Beatles", "The Beatles", 420);

    Playlist myPlaylist2 = new Playlist();
    myPlaylist2.agregarCancionPlaylist_A(song5);
    myPlaylist2.agregarCancionPlaylist_A(song6);
    myPlaylist2.agregarCancionPlaylist_A(song7);
    myPlaylist2.agregarCancionPlaylist_A(song8);
    reproductor.reproducirIntercalandoAleatorio(myPlaylist, myPlaylist2);

}

}