First, create an interface declaration: declare the public method of a single-chain table. The interface declaration is as follows:
Public interface imylist <t> {
Public Boolean add (T entry );
Public Boolean add (INT aposition, t entry );
Public t remove (INT aposition );
Public Boolean contains (T entry );
Public Boolean Replace (INT aposition, t entry );
Public int getlength ();
Public Boolean isfull ();
Public Boolean isempty ();
}
Then define the node type used by the linked list, node class,CodeAs follows:
Class node <t> {
Private t data;
Private node <t> next;
Public node (){}
Public node (T anentry ){
Data = anentry;
}
Public t getdata (){
Return data;
}
Public node <t> getnext (){
Return next;
}
Public void setdata (T entry ){
Data = entry;
}
Public void setnext (node <t> node ){
Next = node;
}
}
Finally, implement the single-chain table class and perform the insert test. The Code is as follows:
Public class mylist <t> implements imylist <t> {
Private int length;
Private node <t> firstnode;
Public mylist (){
Clear ();
}
Private final void clear (){
Length = 0;
}
Public Boolean add (T entry ){
Node <t> newnode = new node <t> (entry );
If (! Isempty ()){
Node <t> lastnode = getnodeat (length-1 );
Lastnode. setnext (newnode );
Length ++;
Return true;
} Else {
Firstnode = newnode;
Length ++;
Return true;
}
}
Public Boolean add (INT aposition, t entry ){
Node <t> newnode = new node <t> (entry );
If (aposition = 0 ){
Newnode. setnext (firstnode );
// Firstnode. setnext (newnode );
Firstnode = newnode;
Length ++;
Return true;
}
If (! Isempty () & aposition <length ){
Node <t> nodebefor = getnodeat (aPosition-1 );
Node <t> nodeafter = nodebefor. getnext ();
Nodebefor. setnext (newnode );
Newnode. setnext (nodeafter );
Length ++;
Return true;
}
Return false;
}
Public t remove (INT aposition ){
Node <t> currentnode = firstnode;
If (aposition = 0 ){
Currentnode = firstnode;
Firstnode = firstnode. getnext ();
// Return firstnode. getdata ();
} Else {
Int Index = 1;
Node <t> befornode = firstnode;
While (index <aPosition-1 & befornode! = NULL ){
Befornode = befornode. getnext ();
Index ++;
}
Currentnode = befornode. getnext ();
Befornode. setnext (currentnode. getnext ());
}
Length --;
Return currentnode. getdata ();
}
Public Boolean contains (T entry ){
Node <t> currentnode = firstnode;
Int Index = 0;
Boolean find = false;
While (index <length &&! Find & currentnode! = NULL ){
If (currentnode. getdata (). Equals (entry )){
Find = true;
}
}
Return find;
}
Public Boolean Replace (INT aposition, t entry ){
Boolean done = false;
Int Index = 0;
Node <t> currentnode = firstnode;
While (! Done & index <length & currentnode! = NULL ){
If (currentnode. getdata (). Equals (entry ))
Done = true;
Currentnode = currentnode. getnext ();
Index ++;
}
Return done;
}
Public int getlength (){
Return this. length;
}
Private node <t> getnodeat (INT aposition ){
Node <t> currentnode = firstnode;
For (INT Index = 0; index <aposition; ++ index ){
Currentnode = currentnode. getnext ();
}
Return currentnode;
}
Public Boolean isempty (){
Return (length = 0 );
}
Public Boolean isfull (){
Return false;
}
Public void display (){
Node <t> currentnode = firstnode;
Int Index = 0;
While (currentnode! = NULL & index <length ){
System. Out. println (currentnode. getdata ());
Currentnode = currentnode. getnext ();
Index ++;
}
}
Public static void main (string [] ARGs ){
Mylist <string> lst = new mylist <string> ();
Lst. Add ("sunzhenxing ");
Lst. Add (0, "Tongji ");
Lst. Add ("sunhailong ");
Lst. Display ();
System. Out. println ("================ ");
Lst. Remove (0 );
Lst. Display ();
System. Out. println ("================ ");
Lst. Remove (0 );
Lst. Display ();
}
}