Java single-linked list implementation-no lead node but with tail pointer

Source: Internet
Author: User

1, this program realizes the chain-type storage structure of linear table. The implemented list has two pointers, one always points to the first node in the list, and the other pointer always points to the last node in the list.

The tail pointer is set because, when the element is inserted at the end of the list, the last element of the linked list can be found directly through the tail pointer, so that the insertion can be done without traversing the list.

2, the specific implementation of the list of the class name Llist2.java, it first implemented a linear table interface Listinterface, the definition of the interface is as follows: http://www.cnblogs.com/hapjin/p/4549492.html

The code for Llist2.java is as follows:

 Public classLlist2<t>ImplementsListinterface<t>{    PrivateNode Firstnode;//A pointer to the first node, which is a single-linked list without a lead node    PrivateNode Lastnode;//Tail hands,points to the last node in the list    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; }    }         PublicLList2 () {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) {Node NewNode=NewNode (newEntry); if(IsEmpty ()) {//Insert First NodeFirstnode =NewNode; }        Else{//inserting nodes in other locationsLastnode.next =NewNode; } lastnode=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 ()) {//table empty for, insert an elementFirstnode =NewNode; Lastnode=NewNode; }            Else if(givenposition = = 1) {//inserting an element at the first position when the table is not emptyNewnode.next =Firstnode; Firstnode=NewNode; }            Else if(Givenposition = = length + 1) {//inserts an element at the last position when the table is not emptyLastnode.next =NewNode; Lastnode=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; Lastnode=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; if(length = = 1)//when there is only one element in the linked list, the tail pointer is empty after deletionLastnode =NULL; }            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; if(givenposition = = length)//when the last element is removed, the tail pointer should point to its previous elementLastnode =Nodebefore; } 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 but with 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.