Advertisement
andkamen

customDoublyLinkedList

Jan 28th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.31 KB | None | 0 0
  1. package com.dataStructures;
  2.  
  3. import java.util.Iterator;
  4. import java.util.NoSuchElementException;
  5.  
  6. public class CustomDoublyLinkedList<T> implements Iterable<T> {
  7.  
  8.     private class Node<T> {
  9.         private T value;
  10.         private Node<T> nextNode;
  11.         private Node<T> previousNode;
  12.  
  13.         private Node(T value) {
  14.             this.setValue(value);
  15.         }
  16.  
  17.         private T getValue() {
  18.             return this.value;
  19.         }
  20.  
  21.         private void setValue(T value) {
  22.             this.value = value;
  23.         }
  24.  
  25.         private Node<T> getNextNode() {
  26.             return this.nextNode;
  27.         }
  28.  
  29.         private void setNextNode(Node<T> nextNode) {
  30.             this.nextNode = nextNode;
  31.         }
  32.  
  33.         private Node<T> getPreviousNode() {
  34.             return this.previousNode;
  35.         }
  36.  
  37.         private void setPreviousNode(Node<T> previousNode) {
  38.             this.previousNode = previousNode;
  39.         }
  40.     }
  41.  
  42.     private Node<T> head;
  43.     private Node<T> tail;
  44.     private int size;
  45.  
  46.     public int size() {
  47.         return size;
  48.     }
  49.  
  50.     public void addFirst(T element) {
  51.         if (this.size() == 0) {
  52.             this.head = this.tail = new Node<T>(element);
  53.         } else {
  54.             Node<T> newHead = new Node<T>(element);
  55.             newHead.setNextNode(this.head);
  56.             this.head.setPreviousNode(newHead);
  57.             this.head = newHead;
  58.         }
  59.  
  60.         this.size++;
  61.     }
  62.  
  63.     public void addLast(T element) {
  64.         if (this.size() == 0) {
  65.             this.head = this.tail = new Node<T>(element);
  66.         } else {
  67.             Node<T> newTail = new Node<T>(element);
  68.             newTail.setPreviousNode(this.tail);
  69.             this.tail.setNextNode(newTail);
  70.             this.tail = newTail;
  71.         }
  72.  
  73.         this.size++;
  74.     }
  75.  
  76.     public T removeFirst() {
  77.         if (this.size() == 0) {
  78.             throw new IllegalStateException("Empty list");
  79.         }
  80.  
  81.         T firstElement = this.head.getValue();
  82.         this.head = this.head.getNextNode();
  83.  
  84.         if (this.head != null) {
  85.             this.head.setPreviousNode(null);
  86.         } else {
  87.             this.tail = null;
  88.         }
  89.  
  90.         this.size--;
  91.         return firstElement;
  92.     }
  93.  
  94.     public T removeLast() {
  95.         if (this.size() == 0) {
  96.             throw new IllegalStateException("Empty list");
  97.         }
  98.  
  99.         T lastElement = this.tail.getValue();
  100.         this.tail = this.tail.getPreviousNode();
  101.         if (this.tail != null) {
  102.             this.tail.setNextNode(null);
  103.         } else {
  104.             this.head = null;
  105.         }
  106.  
  107.         this.size--;
  108.         return lastElement;
  109.     }
  110.  
  111.  
  112.     @Override
  113.     public Iterator<T> iterator() {
  114.         return new Iterator<T>() {
  115.             private int index = size() - 1;
  116.  
  117.             @Override
  118.             public boolean hasNext() {
  119.                 return index >= 0;
  120.             }
  121.  
  122.             Node<T> currentNode = head;
  123.  
  124.             @Override
  125.             public T next() {
  126.                 if (hasNext()) {
  127.                     T element = currentNode.getValue();
  128.                     currentNode = currentNode.nextNode;
  129.                     index--;
  130.                     return element;
  131.                 }
  132.  
  133.                 throw new NoSuchElementException();
  134.             }
  135.         };
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement