Interfaz a Utilizar:
package Colas_de_Nodos;
/**
* @author Daniel Cataño Restrepo <dasniel199410@gmail.com>
*/
public interface INodeQueue<E>{
public void add(E target);
public boolean isEmpty();
public void print();
public E remove();
}
La clase NodeQueue
package Colas_de_Nodos;
/**
* @author Daniel Cataño Restrepo <dasniel199410@gmail.com>
*/
public class NodeQueue<E> implements INodeQueue<E>{
private E item;
private NodeQueue<E> next;
private NodeQueue<E> raiz, top;
public NodeQueue(E item){
this.item=item;
}
public NodeQueue(E item, NodeQueue<E> next){
this.item=item;
this.next=next;
}
public NodeQueue(NodeQueue<E> next){
this.next=next;
}
public E getItem() {
return item;
}
public void setItem(E item) {
this.item = item;
}
public NodeQueue<E> getNext() {
return next;
}
public void setNext(NodeQueue<E> next) {
this.next = next;
}
public void add(E target) {
NodeQueue<E> nodoNuevo=new NodeQueue<>(target, null);
if(isEmpty()){
raiz=nodoNuevo;
top=nodoNuevo;
}else{
top.next=nodoNuevo;
top=nodoNuevo;
}
}
public boolean isEmpty() {
return raiz==null;
}
public void print() {
System.out.print("[");
for(NodeQueue temp=raiz; temp!=null; temp=temp.next){
if(temp==top)
System.out.print(temp.item);
else
System.out.print(temp.item+",");
}
System.out.println("]");
}
public E remove() {
if(!isEmpty()){
E info=raiz.item;
if(raiz==top){
raiz=null;
top=null;
}else{
raiz=raiz.next;
}
return info;
}
return null;
}
}
La clase principal
package Colas_de_Nodos;
/**
* @author Daniel Cataño Restrepo <dasniel199410@gmail.com>
*/
public class Main {
public static void main(String[] args) {
NodeQueue cola=new NodeQueue(1);
cola.add(3);
cola.add(3);
cola.add(3);
cola.add(3);
cola.add(3);
cola.add(3);
cola.print();
cola.remove();
cola.print();
cola.remove();
cola.print();
cola.remove();
cola.print();
cola.remove();
cola.print();
cola.remove();
cola.print();
cola.remove();
cola.print();
cola.add(3);
cola.print();
cola.add(3);
cola.print();
cola.add(3);
cola.print();
cola.add(3);
cola.print();
cola.add(3);
cola.print();
cola.add(3);
cola.print();
}
}
Lo Que imprime en pantalla:
No hay comentarios:
Publicar un comentario