Java implementation of one-way linked list additions and deletions

Source: Internet
Author: User

classnode{ Public intVal;  PublicNode Next;  PublicNode (intval) {         This. val =Val; }}classlisthead{ Public intcount;  PublicNode Next;  PublicListhead () { This. Count =0;  This. Next =NULL; }}classlist{ PublicListhead Head; PrivateNode Current;  PublicList () { This. Head =NewListhead ();  This. Current =NULL; }     Public voidAddNew (intval) {Node NewNode=NewNode (Val); if( This. Head.next = =NULL)             This. Head.next =NewNode; Else             This. Current.next =NewNode;  This. Current =NewNode; }     PublicBoolean contains (intval) {         for(Node current = This. Head.next; Current! =NULL; Current =current.next) {if(Current.val = =val)return true; }        return false; }     Public voidReplaceintValintnewval) {         for(Node current = This. Head.next; Current! =NULL; Current =current.next) {if(Current.val = =val) {Current.val=newval; return; }        }    }     Public voidReplaceAll (intValintnewval) {          for(Node current = This. Head.next; Current! =NULL; Current =current.next) {if(Current.val = =val) current.val=newval; }    }     Public voidRemoveintval) {Node theonetoberemoved=NULL; if( This. head.next! =NULL&& This. Head.next.val = =val) {theonetoberemoved= This. Head.next;  This. Head.next =Theonetoberemoved.next; Theonetoberemoved.next=NULL; return ; }         for(Node current = This. Head.next; Current! =NULL; Current =current.next) {if(Current.next! =NULL&& Current.next.val = =val) {theonetoberemoved=Current.next; Current.next=Theonetoberemoved.next; Theonetoberemoved.next=NULL; return; }        }    }     Public voidRemoveAll (intval) {Node theonetoberemoved=NULL;  while( This. head.next! =NULL&& This. Head.next.val = =val) {theonetoberemoved= This. Head.next;  This. Head.next =Theonetoberemoved.next; Theonetoberemoved.next=NULL; }         for(Node current = This. Head.next; Current! =NULL; Current =current.next) { while(Current.next! =NULL&& Current.next.val = =val) {theonetoberemoved=Current.next; Current.next=Theonetoberemoved.next; Theonetoberemoved.next=NULL; }        }    }     Public voidClear () {Node theonetoberemoved=NULL;  while( This. head.next! =NULL) {theonetoberemoved= This. Head.next;  This. Head.next =Theonetoberemoved.next; Theonetoberemoved.next=NULL; }    }     Public voidprintlist () { for(Node current = This. Head.next; Current! =NULL; Current =current.next) {System. out. Print (Current.val +" "); } System. out. println ("\n================================="); }} Public classhello{ Public Static voidMain (string[] args) {List myList=NewList (); Mylist.addnew (1); Mylist.addnew (1); Mylist.addnew (1); Mylist.addnew (1); Mylist.addnew (1); Mylist.addnew (2); Mylist.addnew (1); Mylist.addnew (2); Mylist.addnew (1); Mylist.addnew (2); Mylist.addnew (7); Mylist.addnew (7); Mylist.addnew (8); Mylist.addnew (2);        Mylist.printlist (); Mylist.replaceall (2,9); Mylist.removeall (7);    Mylist.printlist (); } }

Java implementation of one-way linked list additions and deletions

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.