Package com. csmzxy. rjxy. j2se. array_collection_04;
Public class implements listtest {
Private Static int size;
Private node header = NULL; // indicates the header node.
Public writable listtest (){
Header = new node (null, null );
Size = 0;
}
Public Boolean addhead (Object O ){
If (header! = NULL ){
Node node = new node (O, null );
Node. setnext (header. getnext ());
Header. setnext (node );
Size ++;
Return true;
}
Return false;
}
Public void add (Object O ){
Node node = new node (O, null );
Node temp = header;
While (temp. getnext ()! = NULL ){
Temp = temp. getnext ();
}
Temp. setnext (node );
Size ++;
}
// Print all nodes in zookeeper
Public void print (){
Node temp = header;
While (temp. getnext ()! = NULL ){
System. Out. println ("element:" + temp. getnext (). value );
Temp = temp. getnext ();
}
}
// 1. Insert the specified element at the specified position in the list
Public Boolean add (INT index, object O ){
Node newnode = new node (O, null );
If (index> 0 & index <= size ){
Node temp = header;
For (INT I = 1; I <index; I ++ ){
Temp = temp. getnext ();
}
Newnode. setnext (temp. getnext ());
Temp. setnext (newnode );
Size ++;
Return true;
} Else {
Return false;
}
}
// 2. Insert the specified element at the beginning of the list.
Public void addfirst (Object O ){
// First, we know that the header node is 1 and the specified element is inserted to the 0 position.
Node newnode = new node (O, null );
Node temp = header;
Newnode. setnext (temp. getnext ());
Temp. setnext (newnode );
Size ++;
}
// 3. Add the specified element to the end of the list
Public void addlast (Object O ){
Node newnode = new node (O, null );
Node temp = header;
For (INT I = 1; I <= size; I ++ ){
Temp = temp. getnext ();
If (temp. getnext () = NULL ){
Temp. setnext (newnode );
}
}
Size ++;
}
// 4. Remove all elements from the list
Public void clear (){
Int J = size;
Node temp = header;
Header = NULL;
For (INT I = 1; I <= J; I ++ ){
Temp = temp. getnext ();
Temp. setvalue (null );
System. Out. println (temp. getvalue ());
Size --;
}
}
// 5. Return the element at the specified position in this list
Private node get (INT index ){
Node temp = header;
If (index <= 0 | index> size ){
Throw new indexoutofboundsexception ("index:" + index + ", size :"
+ Size );
} Else {
// Repeat all the vertex elements to return the last Vertex
For (INT I = 1; I <= index; I ++ ){
Temp = temp. getnext ();
}
Return temp;
}
}
// 6. Return the first element in the list
Public object getfirst (){
Node temp = header;
Temp = temp. getnext ();
Return temp. getvalue ();
}
// 7. Return the last element of this list
Public object getlast (){
Node temp = header;
While (size> 0 ){
Temp = temp. getnext ();
Size --;
}
Return temp. getvalue ();
}
// 8. Get and remove the header of this list (first element)
Public object remove (){
Node temp = header;
Temp = temp. getnext ();
Temp. setvalue (null );
Size --;
Return temp. getvalue ();
}
// 9. Remove the element at the specified position in this list
Public void remove (INT index ){
Get (INDEX). setvalue (null );
Size --;
}
// 10. Remove and return the last element of this list
// 11. Number of elements returned from this list
Public int size (){
Return size;
}
Public static void main (string [] ARGs ){
Required listtest synchronized listtest = new synchronized listtest ();
Using listtest. addhead ("adding a node to the header position ");
Using listtest. Add ("444 ");
Using listtest. Add ("222 ");
Using listtest. Add (2, "adding a node to 2 ");
Using listtest. Add (3, "adding a node to 3 ");
Using listtest. Add (5, "adding nodes to 5 locations ");
// 1. Insert the specified element at the specified position in the list
// 2. Insert the specified element at the beginning of the List
Using listtest. addfirst ("addfirst ");
Using listtest. Remove (3 );
// System. Out. println ("returns the first element of this list:" + response listtest. getfirst ());
// System. Out. println ("returns the last element of this list:" + response listtest. getlast ());
// Define listtest. addlast ("addlast ");
Using listtest. Print ();
System. Out. println ("chain table length:" + response listtest. Size ());
// Define listtest. Clear ();
// System. Out. println ("linked list length:" + response listtest. Size ());
// System. Out. println ("index 5 Location:" + response listtest. Get (5). getvalue ());
}
}