Define a list class, like other classes, including member variables and member functions.
A member variable can be a reference to an object, which includes assigning a variable through a constructor, or assigning a value to a member function by a normal method or returning a variable value.
public class Singlelistelem
{
Fields
protected Object data; variable is defined as type object, which means that data can be a reference to an object. It can be assigned a value of one object.
protected Singlelistelem Nextelem; Point to the next linked list element, and note that its data type is the name of this class.
Constructor constructor to assign an initial value to a variable.
Public Singlelistelem (Object data, Singlelistelem next)
{
This.data = data;
This.nextelem = Next;
}
constructor overload, Nextelem value is empty if the object has no next linked list.
Public Singlelistelem (Object data)
{
Singlelistelem (data, NULL);
}
Non-static method
Assign a value to a member variable Nextelem by a function, which is the reference to the next linked element.
public void Setnext (Singlelistelem next)
{
Nextelem = Next;
}
Returns the value of a member variable through a function, where it returns a reference to the object.
Public Object value ()
{
return data;
}
Assigns a value to a reference to an object through a function.
public void SetValue (Object value)
{
data = value;
}
}
Define a class that operates on a linked list, such as adding a header element, adding a tail element, deleting a tail element, and so on.
public class Singlelist
{
Field
protected Long Count; A sequence used to record the elements of a linked list
Protected Singlelistelem head;//defines the head element of a linked list, and when referencing a linked list, you simply start with the element from scratch and pass the header element as a parameter to the referenced function.
Constructor constructor, first assigns an initial value.
Public Singlelist ()
{
head = NULL;
Count = 0;
}
return size
public long Size ()
{
return count;
}
Why write two functions up and down, write count==0 directly is not good.
public Boolean IsEmpty ()
{
return size () ==0;
}
add element to head produces the first list element, adding the header element
public void Addtohead (Object value)
{
Using the first method of construction, in fact, you can use the second method of construction, the first list object reference value assigned to head
Head = new Singlelistelem (value, head);
count++;
}
remove element from head Remove header element
Public Object Removefromhead ()
{
Singlelistelem tmp = head;
Head = Head.next ();
count--;
return TMP;
}
add element to tail chain footer
public void Addtotail (Object value)
{
Singlelistelem tmp = new Singlelistelem (value,null);
if (head!=null)//If the first element exists
{
Singlelistelem pointor = head; Loop from the first element
while (Pointor.next ()!=null)///As long as there is an element in the list, look down until the next reference to the last element is obtained
{
Pointor = Pointor.next ();
}
Pointor.setnext (TMP);//Assign a new object to a reference variable
}
Else
{head = tmp;}
count++;
}
Remove from tail Delete tail element
Public Object Removefromtail ()
{
Singlelistelem Pointor = head;//attention, all started from scratch.
Singlelistelem pre = NULL;
while (Pointor.next ()!=null) {
Pre = Pointor;
Pointor = Pointor.next ();
}
if (pre = = null) head = NULL;
else pre.setnext (NULL);
count--;
return pointor.value;
}
Public Clear ()
{
head = NULL;
Count = 0;
}
and other method such as contains, remove, toString ...
I-I-I-I-I-it's easy for you, hehe
}