Double Circular Linked List

慕和惬
2023-12-01

 

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));
		}
	}
}

 类似资料:

相关阅读

相关文章

相关问答