jtentor icon

DemoList2 - DoubleLinkedList.java

jtentor | PRO | 06/08/20 04:21:09 PM UTC | 0 ⭐ | 1073 👁️ | Never ⏰ | []
Java |

3.38 KB

|

None

|

0 👍

/

0 👎

// Created by Julio Tentor <[email protected]>
//
public class DoubleLinkedList<ELEMENT> {
 
    private class Node {
        public ELEMENT item;
        public Node next;
        public Node prev;
 
        public Node() {
            this(null, null, null);
        }
        public Node(ELEMENT item) {
            this(item, null, null);
        }
        public Node(ELEMENT item, Node next) {
            this(item, next, null);
        }
        public Node(ELEMENT item, Node next, Node prev) {
            this.item = item;
            this.next = next;
            this.prev = prev;
        }
 
        @Override
        public String toString() {
            return this.item.toString();
        }
    }
 
    private Node head;
    private int count;
    private Node tail;
 
    public int size() {
        return this.count;
    }
 
    public DoubleLinkedList() {
        this.head = null;
        this.count = 0;
        this.tail = null;
    }
 
    public void addFirstRookieVersion(ELEMENT item) {
        if (this.count == 0) {
            this.head = this.tail = new Node(item, null, null);
            ++this.count;
        }
        else {
            Node temp = new Node(item, null, null);
            temp.next = this.head;
            this.head.prev = temp;
            this.head = temp;
            ++this.count;
        }
    }
    public void addFirst(ELEMENT item) {
        Node temp = new Node(item, this.head, null);
        if (this.count == 0) {
            this.tail = temp;
        }
        else {
            this.head.prev = temp;
        }
        this.head = temp;
        ++this.count;
    }
 
    public void addLastRookieVersion(ELEMENT item) {
        if (this.count == 0) {
            this.head = this.tail = new Node(item, null, null);
            ++this.count;
        }
        else {
            Node temp = new Node(item, null, null);
            temp.prev = this.tail;
            this.tail.next = temp;
            this.tail = temp;
            ++this.count;
        }
    }
    public void addLast(ELEMENT item) {
        Node temp = new Node(item, null, this.tail);
        if (this.count == 0) {
            this.head = temp;
        }
        else {
            this.tail.next = temp;
        }
        this.tail = temp;
        ++this.count;
    }
 
    public ELEMENT removeFirst() {
        if (this.count == 0) {
            throw new RuntimeException("La lista está vacía...");
        }
        ELEMENT item = this.head.item;
        this.head = this.head.next;
        if (this.head == null) {
            this.tail = null;
        }
        else {
            this.head.prev = null;
        }
        --this.count;
        return item;
    }
 
    public ELEMENT removeLast() {
        if (this.count == 0) {
            throw new RuntimeException("La lista está vacía...");
        }
        ELEMENT item = this.tail.item;
        if (this.head.next == null) {
            this.head = this.tail = null;
        } else {
            this.tail = this.tail.prev;
            this.tail.next = null;
        }
        --this.count;
        return item;
    }
 
    public void Mostrar() {
        for (Node skip = this.head; skip != null; skip = skip.next) {
            System.out.printf("%s ", skip.item.toString());
        }
        System.out.println();
    }
}

Comments