DataStructure Algorithm 花旗银行信息系统-立哥开发

package DataStructures.Lists;
// Copyrights 2008-2021 by Jacky Zong . All rights reserved.
// 纸上得来终觉浅,
// 绝知此事要躬行

public class CircleLinkedList<E> {
  private static class Node<E> {
    Node<E> next;
    E value;

    private Node(E value, Node<E> next) {
      this.value = value;
      this.next = next;
    }
  }

  // For better O.O design this should be private allows for better black box design
  private int size;
  // this will point to dummy node;
  private Node<E> head = null;

  // constructer for class.. here we will make a dummy node for circly linked list implementation
  // with reduced error catching as our list will never be empty;
  public CircleLinkedList() {
    // creation of the dummy node
    head = new Node<E>(null, head);
    size = 0;
  }

  // getter for the size... needed because size is private.
  public int getSize() {
    return size;
  }

  // for the sake of simplistiy this class will only contain the append function or addLast other
  // add functions can be implemented however this is the basses of them all really.
  public void append(E value) {
    if (value == null) {
      // we do not want to add null elements to the list.
      throw new NullPointerException("Cannot add null element to the list");
    }
    // head.next points to the last element;
    head.next = new Node<E>(value, head);
    size++;
  }

  public E remove(int pos) {
    if (pos > size || pos < 0) {
      // catching errors
      throw new IndexOutOfBoundsException("position cannot be greater than size or negative");
    }
    // we need to keep track of the element before the element we want to remove we can see why
    // bellow.
    Node<E> before = head;
    for (int i = 1; i <= pos; i++) {
      before = before.next;
    }
    Node<E> destroy = before.next;
    E saved = destroy.value;
    // assigning the next reference to the the element following the element we want to remove...
    // the last element will be assigned to the head.
    before.next = before.next.next;
    // scrubbing
    destroy = null;
    size--;
    return saved;
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值