Java data structure System--linked list (2): one-way circular linked list and related common operations

Source: Internet
Author: User

Package Linklist.onewaycircular;public class Node {public int data;public node next;//header node initialize public node (node next) { This.next=next;} General node Initialization public node (int data,node next) {This.data=data;this.next=next;}}

I'm a gorgeous cut *************************************************************************. ******

Package Linklist.onewaycircular;public class Onewaycircularlinklist {node head;//header node current;//current node int size;// List size//Initialize empty list public onewaycircularlinklist () {this.head=new Node (null); this.current=this.head;size=0; This.current.next=this.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 RuntimeException ("parameter Error");} if (index==-1) {current=head;} Else{current=head.next;} for (int i=0;i<index&¤t.next!=head;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); Node prev=getelement (index-1);//The previous node of the current node node.next=prev.next;prev.next=node;size++;} Delete 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 current=prev.next;prev.next=current.next;current.next=null;size--;} Print List public void traverse () {if (iSempty ()) {System.out.println ("null");} Else{current=head.next;while (current!=head) {System.out.print (current.data+ ""); current=current.next;} System.out.println ();}}}


Java data structure System--linked list (2): one-way circular linked list and related common operations

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.