Question: using Java to implement linked list
Ideas:
Linked list features: linked list elements are connected by pointers. Usually there is a head pointer pointing to the first element, and other elements can be accessed through the first element.
How to store data in a linked list: Each element in a linked list should be composed of a data domain and a pointer to other elements.
Common Operations of a linked list: Create a linked list, add elements to the Linked List (Add at the end or at a specific position), modify elements in the Linked List, delete elements in the linked list, and view the chain
Elements in the table.
Public class mylist {
Private element header;
Public mylist (){}
Public mylist (Object O ){
Header = new element ();
Header. setvalue (O );
Header. setnext (null );
}
// Other operations are similar. Only the method for adding elements is written.
Public void add (Object O ){
Element temp = header;
If (temp = NULL ){
Header = new element ();
Header. setvalue (O );
Header. setnext (null );
} Else {
While (temp. getnext ()! = NULL ){
Temp = temp. getnext ();
}
Element new = new element ();
New. setvalue (O );
New. setnext (null );
Temp. setnext (new );
}
}
// Add an element at a specified position
Public void add (INT index, object O ){
}
Public void setelement (INT index, object O ){
}
}
Public class element {
Private object value;
Private element next;
// Setter and getter Methods
}