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