Java data structure System--linked list (3): Doubly linked list and related common operations

Source: Internet
Author: User

Package Linklist.doublelinklist;public class Node {public int data;//data domain public node next;//node field, next node public node prve;// node field, previous node//head node Initialize public node (node next) {This.data=data;this.next=next;this.prve=null;} Non-head node initialization public node (int data,node next,node prve) {this.data=data;this.next=next;this.prve=prve;}}

*************************************************************************************************************** *********************

Package Linklist.doublelinklist;public class Doublelinklist {node head;//head node int size;//list size Node current;//current node//Initialize empty chain. Table Public doublelinklist () {this.head = new Node (null); this.size = 0;this.current = head;} Determines whether the linear table is empty public boolean isEmpty () {return size = = 0;} Gets the element at the specified position, where we define the head node head at index -1public Node getelement (int index) {if (Index <-1 | | index >= size) {throw new Ru Ntimeexception ("parameter Error");} if (index = =-1) {current = head;} else {current = Head.next;} for (int i = 0; i < index && current.next! = null; i++) {current = Current.next;} return current;} Inserts the element at the specified position, where we define the head node head at index -1public void Insert (int index, int data) {node node = new Node (data, null,null);     Node prev = getelement (index-1);//The previous node of the current node current=prev.next;//the current node if (current!=null) {node.prve=prev;     Node.next=current;     Current.prve=node; Prev.next=node;}    Else{node.prve=prev;    Node.next=current; Prev.next=node;} size++;} Deletes the element at the specified location public void Delete (int index) {if (Index > size -1) {throw new RuntimeException ("parameter Error");} Node prev = getelement (index-1);//The previous node of the current node is present = Prev.next;prev.next = Current.next;current.prve = prev;size--;} Print List public void traverse () {if (IsEmpty ()) {System.out.println ("null"),} else {current = Head.next;while (current! = Nu ll) {System.out.print (Current.data + "); current = Current.next;} System.out.println ();}}}


Java data structure System--linked list (3): Doubly linked list and related common operations

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.