Double linked list Java implementation

Source: Internet
Author: User
Tags int size

The

focuses on the creation of the structure and changes in the application of the structure chart in the double linked list add delete operation. Details of the changes to see the code comments, the code is not the place I want you to make a point, thank you

/** * <p> * Function Description: Cycle double Linked list * </p> * @author Zhongliangjian * @company * @version V1.0 * * public class util<
	t> {private node<t> head;

	private int size;
		@SuppressWarnings ("hiding") public class Node<t> {private node<t> next;
		Private node<t> privious;

		private T value;
			Public Node (node<t> privious, node<t> Next, T value) {this.privious = privious;
			This.next = Next;
		This.value = value;
		} public Util () {head = new node<t> (null, NULL, NULL);
		head.privious = Head.next = head;
	size = 0; 
	 /** * * Method Description: 1, first of all to determine the location of the node is the first half of the list or the second half of the paragraph 2, if the first half is through the order of the head node to find 3, if the second half is through the head node reverse lookup * Note: 1, the default head node location is 0 2, note for the loop size * * @param index * @return node at the specified position * @author Zhongliangjian/public node<t> getnodebyindex (int index) {if (index =
			= 0) {System.out.println (head);
		return head;
		} if (Index < 0 | | | index > size) {throw new Indexoutofboundsexception ("Out of Lookup range"); } node<t> Node= NULL;
			if (Index < SIZE/2) {for (int i = 0; i < index; i++) {node = Head.next;
			} System.out.println (node);
		return node;
		for (int j = 0; J < Size-index + 1; j + +) {node = head.privious;
		} System.out.println (node);
	return node; /** * * Method Description: Insert the node to the specified location node 1, first, according to the location of the insertion node to find out the original location of the node * 2, a new node, the new node of the predecessor node is the original location node of the predecessor, the new node after the drive node for the original location node * 3, Original Standard The rear drive node of the predecessor node of the node points to the new node, and the predecessor node of the original position is the new node * * @param index * Specifies the position * @param t * Node value * @author
		Zhongliangjian */public void Insertnodebyindex (int index, T t) {node<t> Oldpositionnode = getnodebyindex (index);
		node<t> Newpositionnode = new Node<t> (oldpositionnode.privious, Oldpositionnode, T);
		OldPositionNode.privious.next = Newpositionnode;
		oldpositionnode.privious = Newpositionnode;
	size++; /** * * Method Description: * * @param index * @author Zhongliangjian/public void Deletenodebyindex (int index) {node<t& Gt Deletenode = Getnodebyindex (index);
		DeleteNode.privious.next = Deletenode.next;
		deleteNode.next.privious = deletenode.privious;
		Deletenode = null;
	size--;
		public void Displaylist () {node<t> Node = head;
			for (int i = 0; i < size; i++) {node = Node.next;
		System.out.println (Node.value);
		} public static void Main (string[] args) {util<string> list = new util<string> ();
		List.insertnodebyindex (0, "1");
		List.insertnodebyindex (0, "2");
		List.insertnodebyindex (0, "3");
		List.insertnodebyindex (0, "4");
	List.displaylist ();
 }
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.