Listing list in Java

Source: Internet
Author: User

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 ());
}

}

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.