The list structure of Java programming

Source: Internet
Author: User

Alas, say a little nonsense, yesterday occasionally saw myself a year ago with the C solution to Joseph Ring problem of the circular chain list, sigh unceasingly, think oneself a year ago embedded dream, these two days happened a lot, and some people are not really in, the mood is not good, not much said, directly on the code, just some of the basic operation of the list, take some kung fu to look good.

First, set up a node class, inside a node object and data (to distinguish);

 Public classNode {protectedNode Next;//Pointers    protected intData//Data             PublicNode (intdata) {         This. data =data; }        //Display Node     Public voiddisplay () {System.out.println (data+" "); }}

Then create a linklist class that contains some basic methods of the linked list,

 Public classlinklist { PublicNode first;//define a head node.         Publiclinklist () { This. First =NULL; }        //Insert a head node     Public voidAddfirstnode (intdata) {Node Node=NewNode (data); Node.next=First ; First=node; }        //deletes a head node and returns the head nodes.     Publicnode Deletefirstnode () {node Tempnode=First ; First=Tempnode.next; returnFirst ; }        //Replace the node that is behind index.      Public voidAddintIndex,intdata) {Node Node=First ; Node Current=First ;  while(index-->0) { current=Node.next; Node=Current ; } current.data=data; }        //Insert a node after the index node     Public voidInsert (intIndex,intdata) {Node Node=NewNode (data); Node Current=First ; Node privious=First ;  while(index-->0) {privious=Current ; Current=Current.next; } Node.next=Current ; Privious.next=node; }        //remove nodes from any location     Public voidDeleteintindex) {Node current=First ; Node privious=First ;  while(index-->0) {privious=Current ; Current=Current.next; }        if(Current = =First ) { First=First.next; }Else{Privious.next=Current.next; }    }        //deletes the node based on the value of data, deleting the first node found     Public voidDeleteData (intdata) {Node privious=First ; Node Current=First ;  while(current.data!=data) {privious=Current ; Current=Current.next; }        if(Current = =First ) { First=First.next; }Else{Privious.next=Current.next; }    }}

And then create a main class,

 Public classTestlink { Public Static voidMain (string[] args) {linklist L=Newlinklist (); L.addfirstnode (1); L.addfirstnode (2); L.addfirstnode (3); Node Node=L.first; L.insert (2, 5); L.deletedata (5);  while(node!=NULL) {node.display (); Node=Node.next; }    }}

is not in the mood to continue to write, hope will be better in the future, August 9, 2015 18:55 minutes.

The list structure of Java programming

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.