Java single-linked list implementation-No lead node and no tail pointer

Source: Internet
Author: User

This program uses the Java language to realize the chain implementation of the linear table. First, the interface listinterface of the linear table is defined, and then the Llist class implements the Listinterface to complete the chain list.

In this implementation, the list is not a header node, and a pointer always points to the first element in the list, and does not define a tail pointer. Therefore, each time you insert a new node into the linked list, you need to traverse the linked list once.

More detailed explanations refer to "Data structure and algorithm analysis Java language Description Second Edition" Frank M. Carrano

The Listinterface interface is defined as follows:

 Public InterfaceListinterface<t> {     Public BooleanAdd (T newEntry);  Public BooleanAddintgivenposition, T newEntry);  Public voidClear ();  PublicT Remove (intgivenposition);  Public BooleanReplaceintgivenposition, T newEntry);  PublicT Getentry (intgivenposition);  Public Booleancontains (T anentry);  Public intGetLength ();  Public BooleanIsEmpty ();  Public voiddisplay ();}

The specific implementation class llist is defined as follows:

 Public classLlist<t>ImplementsListinterface<t>{    PrivateNode Firstnode;//A pointer to the first node, which is a single-linked list without a lead node    Private intLength//represents the length of a single-linked list//The get method and the set method for accessing properties are not required in the node class because node is an inner class, and the properties of the inner class can be accessed directly in the outer class    classnode{//node is an inner class, and T is already defined in the outer class, so you can use the wildcard character T        PrivateT data;//data portion of the node        PrivateNode Next;//the pointer portion of the node, pointing to the next node .//the default constructor is not required in the node class         PublicNode (T dataportion) {data=dataportion; }         Publicnode (T dataportion, node NextNode) {Data=dataportion; Next=NextNode; }    }         Publicllist () {clear (); }    //Gets the node at the specified position in the list    PrivateNode GetNodeAt (intgivenposition) {        assert(!isempty () && ((1 <= givenposition) && (givenposition <=Length))) ; Node CurrentNode=Firstnode;  for(intcounter = 1; Counter < givenposition; counter++) {CurrentNode=Currentnode.next; }        assertCurrentNode! =NULL; returnCurrentNode; } @Override Public BooleanAdd (T newEntry) {//inserts each new node at the end of the list, using the GetNodeAt () method to get the address of the last elementNode NewNode =NewNode (newEntry); if(IsEmpty ()) {//Insert First NodeFirstnode =NewNode; }        Else{//inserting nodes in other locationsNode lastnode = getnodeat (length);//every insertion of an element here needs to traverse the list at a higher cost.Lastnode.next =NewNode; } length++; return true; } @Override Public BooleanAddintGivenposition, T newEntry) {//inserts a node at a specified position        BooleanIssuccessful =true; if(givenposition >= 1 && givenposition <= length + 1) {Node NewNode=NewNode (newEntry); if(IsEmpty () | | givenposition = = 1) {//Insert a node at the first positionNewnode.next =Firstnode; Firstnode=NewNode; }            Else{//inserting nodes in other locationsNode Nodebefore = GetNodeAt (givenPosition-1); Node Nodeafter=Nodebefore.next; Nodebefore.next=NewNode; Newnode.next=Nodeafter; } length++; }        Elseissuccessful=false; returnissuccessful; } @Override Public Final voidClear () {//Clear () is called in the constructor, so the final modifier is also usedFirstnode =NULL; Length= 0; } @Override PublicT Remove (intGivenposition) {//Delete a node at a specified locationT result =NULL; if((!isempty ()) && ((givenposition >= 1) && (givenposition <=length))) {            if(givenposition = = 1) {//Delete a node at the first locationresult =Firstnode.data; Firstnode=Firstnode.next; }            Else//Delete Other location nodes in the table{Node Nodebefore= GetNodeAt (givenPosition-1); Node Nodetoremove=Nodebefore.next; Node Nodeafter=Nodetoremove.next; Nodebefore.next=Nodeafter; Result=Nodetoremove.data; } length--; }        returnresult; } @Override Public BooleanReplaceintGivenposition, T newEntry) {//Replace the value of the node at the specified position        BooleanIssuccessful =true; if((!isempty ()) && ((givenposition >= 1) && (givenposition <=length))) {Node Desirenode=getnodeat (givenposition); Desirenode.data=NewEntry; }        Elseissuccessful=false; returnissuccessful; } @Override PublicT Getentry (intGivenposition) {//gets the value of the node at the specified locationT result =NULL; if((!isempty ()) && ((givenposition >= 1) && (givenposition <=length))) {Result=getnodeat (givenposition). data; }        returnresult; } @Override Public BooleanContains (T anentry) {//determine if a node in a linked list contains a value        BooleanFound =false; Node CurrentNode=Firstnode;  while(!found && CurrentNode! =NULL){            if(CurrentNode.data.equals (anentry)) {found=true;  Break; } CurrentNode=Currentnode.next; }        returnfound; } @Override Public intGetLength () {//get the length of a linked list        returnlength; } @Override Public BooleanIsEmpty () {//determine if the linked list is empty        Booleanresult; if(length = = 0){            assertFirstnode = =NULL; Result=true; }        Else{            assertFirstnode! =NULL; Result=false; }        returnresult; } @Override Public voidDisplay () {//iterate through the linked list, showing the value of each node in the listNode CurrentNode =Firstnode;  while(CurrentNode! =NULL) {System.out.println (currentnode.data); CurrentNode=Currentnode.next; }    }}

Java single-linked list implementation-No lead node and no tail pointer

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.