Own a 20-minute Java implementation of the one-way list (including additions and deletions to change the operation)

Source: Internet
Author: User

 Packagetest1;//create a linked list structure Public classSinglelist {//defines the head node pointer.Node head =NULL; //node data structure defining a linked list    classnode{intVal;        Node Next;  PublicNode (intval) {             This. val =Val; Next=NULL; }    }     //Add a node to the tail of the list     Public voidAddintnum) {Node Node=NewNode (num); if(head==NULL) Head=node; Elsesearch (). Next=node; }    //get the node at the tail of the list     Publicnode Search () {node Nodelast=Head;  while(nodelast.next!=NULL) {Nodelast=Nodelast.next; }        returnNodelast; }    //implement to find the data for a particular node in the list is num and return the node     PublicNode Search (intnum) {Node Nodelast=Head;  while(nodelast.next!=NULL){            if(nodelast.val==num)returnNodelast; Nodelast=Nodelast.next; }        return NULL; }    //implement to find a particular node in a linked list the data for the next node is NUM and returns that node     PublicNode searchprevious (intnum) {Node Nodelast=Head;  while(nodelast.next!=NULL){            if(nodelast.next.val==num)returnNodelast; Nodelast=Nodelast.next; }        return NULL; }    //change actions in a linked list     Public voidChangeintNumintnumnew) {        if(num)! =NULL) search (num). val=numnew; }    //Delete a node of a linked list     Public voidDeleteintnum) {        if(num)! =NULL) searchprevious (num). Next=search (num). Next; }    //Print linked list     Public voidprintlist () {Node cur=Head;  while(cur!=NULL) {System.out.println (cur.val); Cur=Cur.next; }    }     Public Static voidMain (string[] args) {singlelist singlelist=Newsinglelist (); Singlelist.add (1); Singlelist.add (2); Singlelist.add (3); Singlelist.add (4); Singlelist.change (3,8); Singlelist.delete (8); Singlelist.printlist ();//While (singlelist.head!=null) {//System.out.println (singlelist.head.val);//Singlelist.head=singlelist.head.next;//        }    }}

Note 1 Define a head node in Singlelist 2 note how to implement a list of 3 queries for a list delete operation you can use node node = new node (num) in any non-main function.

It's not in the main function. I know this is an internal category.

Own a 20-minute Java implementation of the one-way list (including additions and deletions to change the operation)

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.