PackageCom.lanyuan.log; Public classTest1 {Privatetnode Head; Private intsize; PrivateTnode last; Public intsize () {return This. Size + 1; } PublicTest1 () {head= last =NULL; } Public voidAddintval) {tnode node=NewTnode (Val); if(Head = =NULL) {Head= last =node; Last.next=Head; } Else{Last.next=node; Last=Last.next; Last.next=Head; } size++; } Public intDeletelast () {if(Head = =NULL) { Throw NewRuntimeException ("It ' s not allow"); } tnode Node=Head.next; intval =Last.val; for(inti = 0; Node! =NULL&& Node.next! = Last; i++) {node=Node.next; } size--; Last.next=node; Last=Last.next; Last.next=Head; returnVal; } Public BooleanUpdaetlast (intval) { if(Head = =NULL) { Throw NewRuntimeException ("It ' s not allow"); } last.val=Val; returnLast! =NULL; } Public intFindLast () {if(Head = =NULL) { Throw NewRuntimeException ("It ' s not allow"); } returnLast.val; } PublicString toString () {if(Head = =NULL) { return"Tlink[]"; } StringBuilder SB=NewStringBuilder (); Tnode node=Head.next; Sb.append ("Tlink[" +head.val); for(inti = 0; I < size && node! = head; i++) {sb.append ("," +node.val); Node=Node.next; } sb.append ("]"); returnsb.tostring (); }}classTnode {//to simply store int type data here, you can change to generics Public intVal; PublicTnode Next; Publictnode () {Super(); } PublicTnode (intval) { This. val =Val; }}
Linklist and additions and deletions to change