Double Circular Linked List

 

package org.sugite.algorithms;

public class LinkList<E> {
	private static class Node<E> {	//定义结点类,Nested Class
		E node;						//结点元素
		Node<E> next;				//后继结点
		Node<E> previous;			//前驱结点

		public Node(Node<E> previous, E node, Node<E> next) {//Node Constructor
			this.node = node;
			this.next = next;
			this.previous = previous;
		}
	}

	private Node<E> head = new Node<E>(null, null, null);//head Node ,a null Node

	private int size;	//size of this list

	LinkList() {
		head.next = head.previous = head;
	}//LinkList Constructor 

	public int size() {
		return this.size;
	}// return size of LinkList
	
    
    private void checkElementIndex(int index) {
        if (!(index >= 0 && index < this.size))
            throw new IndexOutOfBoundsException();
    }// Tells if the argument is the index of an existing element.

    private void checkPositionIndex(int index) {
        if (!(index >= 0 && index <= this.size))
            throw new IndexOutOfBoundsException();
    }// Tells if the argument is the index of a valid position for an add operation.
    
	private void addBefore(E e, Node<E> node) {
		Node<E> newNode = new Node<E>(node.previous, e, node);//new a Node for e
		newNode.previous.next = newNode;//add newNode to node.previous.next
		newNode.next.previous = newNode;//add newNode to node.previous
		this.size++;
	}//Inserts element e before non-null Node node.

	public void addHead(E e) {
		this.addBefore(e, head.next);
	}//Inserts the specified element at the beginning of this list.

	public void addLast(E e) {
		this.addBefore(e, head);
	}//Appends the specified element to the end of this list.

	public void add(E e, int index) {
		checkPositionIndex(index);
		Node<E> x = head.next;
		for (int i = 0; i<index ;i++ ) {
			x = x.next;
		}
		this.addBefore(e, x);
	}//Inserts the specified element at the specified position in this list.

	public void remove(int index) {
		checkElementIndex(index);
		Node<E> p = head.next;
		for (int i = 0 ; i<index;i++) {
			 p = p.next;
		}
		p.previous.next = p.next;
		p.next.previous = p.previous;
		this.size--;
	}//Removes the element at the specified position in this list.

	public E get(int index) {
		checkElementIndex(index);
		return getNode(index).node;
	}//Returns the element at the specified position in this list.

	public E set(int index, E e) {
		checkElementIndex(index);
		Node<E> x = getNode(index);
		E oldVal = x.node;
		x.node = e;
		return oldVal;
	}/*Replaces the element at the specified position in this list with the specified element.*/

	Node<E> getNode(int index) {
		if (index < (size >> 1)) {
			Node<E> x = head.next;
			for (int i = 0; i < index; i++)
				x = x.next;
			return x;
		} else {
			Node<E> x = head.previous;
			for (int i = size - 1; i > index; i--)
				x = x.previous;
			return x;
		}
	}//Returns the (non-null) Node at the specified element index.

	public static void main(String[] args) {
		LinkList<String> li = new LinkList<String>();
		for (int i = 0; i < 10; i++) {
			li.addLast("第" + (i + 1) + "个节点");
		}
		li.addHead("这是头结点");
		li.remove(3);
		li.set(6, "改变第6个节点");
		li.add("插入到第六个位置", 6);
		for (int i = 0; i < li.size(); i++) {
			System.out.println(li.get(i));
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值