Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private class Node {
- public E item;
- public Node next;
- public Node() {
- this(null, null);
- }
- public Node(E item) {
- this(item, null);
- }
- public Node(E item, Node next) {
- this.item = item;
- this.next = next;
- }
- public String toString() {
- return this.item.toString();
- }
- }
- @Override
- public Iterator<E> iterator() {
- return new MyIterator(this.head);
- }
- private class MyIterator implements Iterator<E> {
- private Node actual;
- public MyIterator(Node inicio) {
- this.actual = inicio;
- }
- @Override
- public boolean hasNext() {
- return this.actual != null;
- }
- @Override
- public E next() {
- if (!this.hasNext()) {
- throw new RuntimeException("La lista está vacía...");
- }
- E item = this.actual.item;
- this.actual = this.actual.next;
- return item;
- }
- }
- }
- }
- public class List<E> {
- private Node<E> head;
- private Node<E> tail;
- int count;
- public Node<E> getHead(){
- return this.head;
- }
- public Node<E> getTail(){
- return this.tail;
- }
- public boolean isEmpty() {
- if (this.count == 0) {
- return true;
- }
- return false;
- }
- public int size() {
- return this.count;
- }
- public void AddToHead(E item) {
- if(this.count==0) {
- this.head=this.tail=new Node<E>(item,null);
- ++this.count;
- }else {
- Node<E> temp = new Node<E>(item,null);
- temp.next=this.head;
- this.head=temp;
- ++this.count;
- }
- }
- public void AddToTail(E item) {
- if (this.count==0) {
- this.head=this.tail=new Node<E>(item,null);
- ++this.count;
- }else {
- Node<E> temp = new Node<E>(item, null);
- this.tail.next=temp;
- this.tail=temp;
- ++this.count;
- }
- }
- public boolean Contains(E item) {
- if (this.count==0) {
- return false;
- }
- Node<E> nodo;
- for (nodo = this.head; nodo.getNext() != null; nodo = nodo.getNext()) {
- if (nodo.getItem().equals(item)) {
- return true;
- }
- }
- if (nodo.getItem().equals(item)) {
- return true;
- }
- return false;
- }
- public List() {
- this.head = null;
- this.tail = null;
- this.count = 0;
- }
- public E RemoveFromHead() {
- if (this.count==0) {
- throw new RuntimeException("Está vacío");
- }else {
- E aux = this.head.getItem();
- this.head = this.head.getNext();
- if(this.head==null) {
- this.tail=null;
- }
- --this.count;
- return aux;
- }
- }
- public E RemoveFromTail() {
- if (this.count==0) {
- throw new RuntimeException("Está vacío");
- }
- E aux = this.tail.getItem();
- if(this.head.next==null) {
- this.head=this.tail=null;
- }else {
- Node<E> nodo = this.head;
- for (; nodo.getNext() != null; nodo = nodo.getNext()) {}
- this.tail=nodo;
- this.tail.next=null;
- }
- --this.count;
- return aux;
- }
- public String toString() {
- if (this.count==0) {
- throw new RuntimeException("Está vacío");
- }else {
- Node<E> nodo = this.head;
- String str = "";
- for (nodo=this.head; nodo.getNext() != null; nodo = nodo.getNext()) {
- str += nodo.getItem().toString();
- }
- str += nodo.getItem().toString();
- return str;
- }
- }
- }
- // Clase generica Cola con una Lista
- public class Queue<E> {
- List<E> lista;
- public boolean isEmpty() {
- return lista.isEmpty();
- }
- public E DeQueue() {
- if(lista.isEmpty()) {
- return null;
- }else {
- E obj = lista.RemoveFromHead();
- return obj;
- }
- }
- public void EnQueue(E elem) {
- if(lista.isEmpty()) {
- lista.AddToHead(elem);
- }else {
- lista.AddToTail(elem);
- }
- }
- public E Peek() {
- if(lista.isEmpty()) {
- System.out.println("Esta vacia");
- return null;
- }else {
- return lista.getHead().getItem();
- }
- }
- public Queue() {
- this.lista= new List<E>();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement