Beispiel #1
0
  /**
   * Will add a new item to the start of the deque. Will throw an error if an empty item is passed
   * through. If this is the first item that has been added to the deque, then this will also set
   * the last item in the deque to be the first.
   *
   * @param item The item to add to the start of the deque
   */
  public void addFirst(Item item) {
    if (item == null) {
      throw new NullPointerException("Woah, you can't add nothing");
    }

    Node previousFirst = first;
    first = new Node();
    first.item = item;
    first.previous = null;

    if (previousFirst == null) {
      // This is the first time something has been added to the deque
      first.next = null;
    } else {
      first.next = previousFirst;
      previousFirst.previous = first;
    }

    // Is this the only node in the deque?
    if (last == null) {
      last = first;
    }

    N++;
  }
Beispiel #2
0
  void add(int num) {
    Node newNode = new Node();
    newNode.contents = num;

    if (first == null) {
      first = newNode;
      return;
    }

    assert first != null;
    if (first.contents >= num) {
      first.previous = newNode;
      newNode.next = first;
      first = newNode;
      return;
    }

    assert first.contents < num;
    Node tmp = first;
    while (tmp.contents < num && tmp.next != null) tmp = tmp.next;

    assert tmp != null;
    if (tmp.contents < num && tmp.next == null) {
      tmp.next = newNode;
      newNode.previous = tmp;
    } else {
      assert tmp.contents >= num;
      assert tmp.previous.contents < num;
      newNode.previous = tmp.previous;
      newNode.previous.next = newNode;
      newNode.next = tmp;
      newNode.next.previous = newNode;
    }
  }
 /** Move node to the front of the list. */
 public void setHead(Node node) {
   node.next = head;
   node.previous = null;
   if (head != null) {
     head.previous = node;
   }
   head = node;
   if (end == null) {
     end = head;
   }
 }
  public boolean removeNode(Node node) {

    if (null == node) {
      return false;
    }

    Node previousNode = node.previous;
    Node nextNode = node.next;

    // VB: Alternate way to check if head == node && tail == node
    if (null == previousNode) {
      if (null == nextNode) {
        tail = head = null;
        node = null;
        return true;
      }
      nextNode.previous = null;
      head = nextNode;

      node = null;
      return true;
    }

    // VB: Use else if which will improve readability
    if (null == nextNode) {
      previousNode.next = null;
      tail = previousNode;
      node = null;
      return true;
    }

    return false;
  }
  public boolean removeNode(T data) {
    if (null == head) return false;

    if (head.data.equals(data)) {
      Node n = head;
      head = head.next;
      head.previous =
          null; // VB: This will throw NULL Pointer exception of there is only one elemnt in the
      // linkedlist  before removal
      n = null;
      return true;
    }

    if (tail.data.equals(data)) {
      Node n = tail;
      tail = tail.previous; // VB : Same here
      tail.next = null;
      n = null;
      return true;
    }

    Node n = head;
    while (null != n) {
      if (n.data.equals(data)) {
        n.previous.next = n.next;
        n.next.previous = n.previous;
        n = null;
        return true;
      }
      n = n.next;
    }

    return false;
  }
Beispiel #6
0
 /** Constructor for BufferPool */
 public BufferPool() {
   size = 0;
   head = new Node<K, V>();
   tail = new Node<K, V>();
   head.next = tail;
   tail.previous = head;
 }
  public void insert(int valueToInsertAfter, int value) {
    Node newNode = new Node(value);
    ++size;

    Node nodeToInsertAfter = getNodeByValue(valueToInsertAfter);
    if (nodeToInsertAfter.next == null) {
      newNode.previous = nodeToInsertAfter;
      nodeToInsertAfter.next = newNode;
      tail = newNode;
      return;
    }

    newNode.previous = nodeToInsertAfter;
    newNode.next = nodeToInsertAfter.next;
    nodeToInsertAfter.next.previous = newNode;
    nodeToInsertAfter.next = newNode;
  }
Beispiel #8
0
 /** Add the item to the stack. */
 public void push(Item item) {
   Node previous = head;
   head = new Node();
   head.item = item;
   head.previous = previous;
   N++;
   assert check();
 }
 public void addFront(String item) {
   if (header == null) header = new Node(null, item, null);
   else {
     Node n = new Node(null, item, header);
     header.previous = n;
     header = n;
   }
 }
Beispiel #10
0
 // @param capacity, an integer
 public LRUcache(int capacity) {
   // write your code here
   this.capacity = capacity;
   this.map = new HashMap<Integer, Node>();
   this.head = new Node(-1, -1);
   this.tail = new Node(-1, -1);
   head.next = tail;
   tail.previous = head;
 }
Beispiel #11
0
 /**
  * Insert a new key-value pair into the pool
  *
  * @param key
  * @param value
  */
 public void insert(K key, V value) {
   Node<K, V> newNode = new Node<K, V>(key, value);
   newNode.next = head.next;
   newNode.previous = head;
   head.next.previous = newNode;
   head.next = newNode;
   size++;
   // if the number of elements exceeds the limit, delete the node at the
   // end.
   if (size > CAPACITY) {
     Node<K, V> last = tail.previous;
     tail.previous = last.previous;
     tail.previous.next = tail;
     last.previous = null;
     last.next = null;
     size--;
   }
 }
 // remove last element
 public void removeLast() {
   Node last = findLast();
   if (last != null && last.previous == null) {
     last = null;
   } else if (last != null) {
     last.previous.next = null;
     last.previous = null;
     last = null;
   }
 }
Beispiel #13
0
  /**
   * remove and return the item from the front
   *
   * @return removed generic item
   */
  public Item removeFirst() {
    if (size() == 0) throw new java.util.NoSuchElementException();

    Node currentNode = firstNode;
    firstNode = currentNode.next;
    if (firstNode != null) {
      firstNode.previous = null;
    } else {
      lastNode = null;
    }
    N--;
    return currentNode.item;
  }
  public void insertAtHead(int value) {
    Node newNode = new Node(value);
    ++size;

    if (head == null) {
      head = newNode;
      tail = newNode;
      return;
    }

    newNode.next = head;
    head.previous = newNode;
    head = newNode; // no need to update tail
  }
 public void preAddNode(Node n, String value) {
   Node newNode;
   if (n == null) {
     // List is empty
     newNode = new Node(null, value, null);
   } else if (n.previous == null) {
     // n is the first node
     addFront(value);
   } else {
     newNode = new Node(n.previous, value, n);
     n.previous.next = newNode;
     n.previous = newNode;
   }
 }
Beispiel #16
0
  // @param key, an integer
  // @param value, an integer
  // @return nothing
  public void set(int key, int value) {
    // write your code here
    if (!map.containsKey(key)) {
      if (map.size() == capacity) {
        Node previous = tail.previous;
        tail.previous = previous.previous;
        tail.previous.next = tail;
        map.remove(previous.key);
      }
    } else {
      Node node = map.get(key);
      Stack<Integer> stack = new Stack<Integer>();
      node.previous.next = node.next;
      node.next.previous = node.previous;
    }
    Node newNode = new Node(key, value);

    newNode.next = head.next;
    head.next = newNode;
    newNode.next.previous = newNode;
    newNode.previous = head;

    map.put(key, newNode);
  }
Beispiel #17
0
  // @return an integer
  public int get(int key) {
    // write your code here
    if (!map.containsKey(key)) return -1;

    Node node = map.get(key);
    node.previous.next = node.next;
    node.next.previous = node.previous;

    node.next = head.next;
    head.next = node;
    node.next.previous = node;
    node.previous = head;

    return node.value;
  }
Beispiel #18
0
  public boolean insert(T data) {

    if (null == head) {
      head = tail = new Node(data);
      return true;
    }

    Node newNode = new Node(data);

    newNode.previous = tail;
    tail.next = newNode;
    tail = newNode;

    return true;
  }
Beispiel #19
0
  public boolean insert(int index, T data) {

    if ((index > size()) || (index < 0)) {
      return false;
    }
    Node n = new Node(data);

    if (index == 0) {
      if (null == head) {
        head = tail = n;
        return true;
      }
      head.previous = n;
      n.next = head;
      head = n;
      return true;
    }
    if (index == size()) {
      tail.next = n;
      n.previous = tail;
      tail = n;
      return true;
    }

    Node pNode = head;
    for (int i = 0; i < index; i++) {
      pNode = pNode.next;
    }
    pNode = pNode.previous;
    pNode.next.previous = n;
    n.next = pNode.next;
    n.previous = pNode;
    pNode.next = n;

    return true;
  }
Beispiel #20
0
  /**
   * add the item to the end
   *
   * @param item generic item ready to add to deque
   */
  public void addLast(Item item) {
    if (item == null) throw new java.lang.NullPointerException();

    Node currentNode = new Node();
    currentNode.item = item;
    currentNode.previous = lastNode;

    if (isEmpty()) {
      firstNode = currentNode;
    } else {
      lastNode.next = currentNode;
    }
    lastNode = currentNode;
    N++;
  }
Beispiel #21
0
  public void addFirst(Item item) {
    if (item == null) {
      throw new java.lang.NullPointerException();
    }

    Node newNode = new Node();
    newNode.item = item;
    newNode.next = this.front;
    newNode.previous = null;

    if (!isEmpty()) this.front.previous = newNode;
    else this.rear = newNode;

    this.front = newNode;
    size++;
  }
  /** Adds a new item at the start of the sequence. */
  public void insertFirst(Object o) {
    // There is a special case when the sequence is empty.
    // Then the both the head and tail pointers needs to be
    // initialised to reference the new node.
    if (listHead == null) {
      listHead = new Node(o, listHead, null);
      listTail = listHead;
    }

    // In the general case, we simply add a new node at the start
    // of the list via the head pointer.
    else {

      listHead.previous = new Node(o, listHead, null);
      listHead = listHead.previous;
    }
  }
Beispiel #23
0
 /**
  * Get the value associated with the key.
  *
  * @param key
  * @return the value associated with the key. null if the key doesn't exist
  */
 public V get(K key) {
   Node<K, V> current = head.next;
   // traver the list and find a match. On found, move this match to the
   // top of this list.
   while (current != tail) {
     if (current.key.equals(key)) {
       current.previous.next = current.next;
       current.next.previous = current.previous;
       current.next = head.next;
       current.previous = head;
       head.next.previous = current;
       head.next = current;
       return current.value;
     }
     current = current.next;
   }
   return null;
 }
  public void addFirst(Item item) throws NullPointerException {
    // null item addition
    if (item == null) {
      throw new NullPointerException();
    }
    Node oldFirst = this.first;
    this.first = new Node();
    this.first.previous = null;
    this.first.item = item;
    this.first.next = oldFirst;

    if (this.size++ == 0) {
      // in the case of an empty queue oldFirst == null && last = first
      this.last = this.first;
    } else {
      // it is the non-empty queue case so the oldFirst != null...
      oldFirst.previous = this.first;
    }
  }
  /** Removes the item at the start of the sequence. */
  public void deleteFirst() throws SequenceDLListException {
    // Check there is something in the sequence to delete.
    if (listHead == null) {
      throw new SequenceDLListException("Sequence Underflow");
    }

    // There is a special case when there is just one item in the
    // sequence. Both pointers then need to be reset to null.
    if (listHead.next == null) {
      listHead = null;
      listTail = null;
    }

    // In the general case, we just unlink the first node of the
    // list.
    else {
      listHead = listHead.next;
      listHead.previous = null;
    }
  }
Beispiel #26
0
  public boolean remove(int position) {

    if ((position < 0) || (position >= size())) return false;

    if (null == head) return false;

    if (position == 0) {
      if (null == head.next) {
        head = tail = null;
        return true;
      }

      Node n = head;
      n = null;
      head = head.next;
      head.previous = null;
      return true;
    }
    if (position == (size() - 1)) {
      Node n = tail;
      tail.previous.next = null;
      tail = tail.previous;
      n = null;
      return true;
    }

    Node n = head;
    for (int i = 0; i < position; i++) {
      n = n.next;
    }
    n = n.previous;

    n.previous.next =
        n.next; // VB : Dont do multiple references as you usually miss null check. Please use
    // temporary variable and do null check
    n.next.previous = n.previous;
    // VB: Not necessary but good to mark the deleted node's next and prev pointer as null
    n = null; // VB: no need to mark n as null, as n is local variable
    return true;
  }
Beispiel #27
0
  void remove(int num) {
    if (first == null) return;

    assert first != null;
    if (first.contents == num) {
      first = first.next;
      if (first != null) first.previous = null;
    }

    assert first.contents != num;
    Node tmp = first;
    while (tmp != null && tmp.contents < num) tmp = tmp.next;

    if (tmp == null) return;

    assert tmp != null;
    if (tmp.contents > num) return;

    assert tmp.contents <= num;
    tmp.previous.next = tmp.next;
    if (tmp.next != null) tmp.next.previous = tmp.previous;
  }
Beispiel #28
0
  /**
   * Removes the item from the start of the deque. Will throw an exception if the deque is empty.
   * After the successful removal, this function will return the item that just got removed.
   *
   * @return The item that just got removed
   */
  public Item removeFirst() {
    if (isEmpty()) {
      throw new NoSuchElementException();
    }

    Item itemToReturn = first.item;
    first = first.next;

    // Clear whatever previous value we might have
    if (first != null && first.previous != null) {
      first.previous = null;
    }

    // Have we just emptied out the deque?
    if (first == null) {
      last = null;
    }

    N--;

    return itemToReturn;
  }
  /** Adds a new item at a specified position in the sequence. */
  public void insert(Object o, int index) throws SequenceDLListException {
    // Check the index is positive.
    if (index < 0) {
      throw new SequenceDLListException("Indexed Element out of Range");
    }

    // There is a special case when the sequence is empty.
    // Then the both the head and tail pointers needs to be
    // initialised to reference the new node.
    if (listHead == null) {
      if (index == 0) {
        listHead = new Node(o, listHead, null);
        listTail = listHead;
      } else {
        throw new SequenceDLListException("Indexed element is out of range");
      }
    }

    // There is another special case for insertion at the head of
    // the sequence.
    else if (index == 0) {
      listHead.previous = new Node(o, listHead, null);
      listHead = listHead.previous;
    }

    // In the general case, we need to chain down the linked list
    // from the head until we find the location for the new
    // list node. If we reach the end of the list before finding
    // the specified location, we know that the given index was out
    // of range and throw an exception.
    else {
      Node nodePointer = listHead;
      int i = 1;
      while (i < index) {
        nodePointer = nodePointer.next;
        i += 1;
        if (nodePointer == null) {
          throw new SequenceDLListException("Indexed Element out of Range");
        }
      }

      // Now we've found the node before the position of the
      // new one, so we 'hook in' the new Node.

      nodePointer.next = new Node(o, nodePointer.next, nodePointer);

      // set the previous Node on the Node one above the new Node
      // ensure that this doesn't occur when adding to the last entry in
      // the list
      if (nodePointer.next.next != null) {
        nodePointer.next.next.previous = nodePointer.next;
      }
      // Finally we need to check that the tail pointer is
      // correct. Another special case occurs if the new
      // node was inserted at the end, in which case, we need
      // to update the tail pointer.
      if (nodePointer == listTail) {
        listTail = listTail.next;
      }
    }
  }