| Linked list is an important data structure and plays an important role in programming. C and C ++
In other words, pointers are used to implement the linked list structure. Because the Java language does not provide pointers, some people think that
The linked list cannot be implemented in Java. In fact, Java is easier to implement the linked list structure than C and C ++.
. The object reference in Java is actually a pointer (the pointer in this article is conceptual,
Rather than the data type provided by the language), so we can write such a class to implement the node in the linked list.
Class Node
{
Object Data;
Node next; // point to the next node
}
Define the data domain as an object class because the object class is a generalized superclass and any class object can
The value assignment adds the versatility of the Code. To make the linked list accessible, you also need to define a table header.
The header must contain a pointer to the first node and a pointer to the current node. For convenience at the end of the linked list
Add a node and add a pointer pointing to the end of the linked list. In addition, you can use a field to represent the linked list.
When the caller wants to get the hour of the linked list, the entire linked list does not have to be traversed. This is the linked list.
:
Data Structure of the linked list
We can use the list class to implement the linked list structure, and use the variables head, tail, length, and pointer.
To implement the header. There are some tips for storing the pointer of the current node. pointer is not a method for storing the pointer pointing to the current node.
Instead, it stores the pointer to its front node. When its value is null, it indicates that the current node is
The first node. So why? This is because after deleting the current node, you still need to ensure the remaining
Nodes constitute a linked list. If Pointer Points to the current node, the operation will be very difficult. Such
How can we get the current node? We have defined a method named cursor (). The returned value is the pointer to the current node.
Needle. The List class also defines some methods to perform basic operations on the linked list.
We can perform various operations on the linked list. For example, the reset () method makes the first node the current node.
The insert (Object D) method inserts a node before the current node and makes it the current node.
The remove () method deletes the current node and returns its content, making its successor node the current node, as shown in figure
If the last node is deleted, the first node becomes the current node.
The source code of the list type is as follows:
Import java. Io .*;
Public class list
{
/* Use variables to implement the header */
Private node head = NULL;
Private node tail = NULL;
Private node pointer = NULL;
Private int length = 0;
Public void deleteall ()
/* Clear the entire linked list */
{
Head = NULL;
Tail = NULL;
Pointer = NULL;
Length = 0;
}
Public void reset ()
/* Reset the linked list to make the first node the current node */
{
Pointer = NULL;
}
Public Boolean isempty ()
/* Determine whether the linked list is empty */
{
Return (length = 0 );
}
Public Boolean isend ()
/* Determine whether the current node is the last node */
{
If (length = 0)
Throw new java. Lang. nullpointerexception ();
Else if (length = 1)
Return true;
Else
Return (cursor () = tail );
}
Public object nextnode ()
/* Return the value of the next node of the current node and make it the current node */
{
If (length = 1)
Throw new java. util. nosuchelementexception ();
Else if (length = 0)
Throw new java. Lang. nullpointerexception ();
Else
{
Node temp = cursor ();
Pointer = temp;
If (temp! = Tail)
Return (temp. Next. data );
Else
Throw new java. util. nosuchelementexception ();
}
}
Public object currentnode ()
/* Return the value of the current node */
{
Node temp = cursor ();
Return temp. Data;
}
Public void insert (Object D)
/* Insert a node before the current node and make it the current node */
{
Node E = new node (d );
If (length = 0)
{
Tail = E;
Head = E;
}
Else
{
Node temp = cursor ();
E. Next = temp;
If (pointer = NULL)
Head = E;
Else
Pointer. Next = E;
}
Length ++;
}
Public int size ()
/* Return the size of the linked list */
{
Return (length );
}
Public object remove ()
/* Remove the current node from the linked list. The next node becomes the current node. If the removed node is the last node
One node, the first node becomes the current node */
{
Object temp;
If (length = 0)
Throw new java. util. nosuchelementexception ();
Else if (length = 1)
{
Temp = head. Data;
Deleteall ();
}
Else
{
Node cur = cursor ();
Temp = cur. Data;
If (cur = head)
Head = cur. Next;
Else if (cur = tail)
{
Pointer. Next = NULL;
Tail = pointer;
Reset ();
}
Else
Pointer. Next = cur. Next;
Length --;
}
Return temp;
}
Private node cursor ()
/* Return the pointer of the current node */
{
If (Head = NULL)
Throw new java. Lang. nullpointerexception ();
Else if (pointer = NULL)
Return head;
Else
Return pointer. Next;
}
Public static void main (string [] ARGs)
/* Simple example of linked list application */
{
List A = new list ();
For (INT I = 1; I <= 10; I ++)
A. insert (New INTEGER (I ));
System. Out. println (A. currentnode ());
While (! A. isend ())
System. Out. println (A. nextnode ());
A. Reset ();
While (! A. isend ())
{
A. Remove ();
}
A. Remove ();
A. Reset ();
If (A. isempty ())
System. Out. println ("there is no node in list/N ");
System. In. println ("you can press return to quit/N ");
Try
{
System. In. Read ();
// Make sure that the user sees the running result
}
Catch (ioexception E)
{}
}
}
Class Node
/* Create a node definition for the linked list */
{
Object Data;
Node next;
Node (Object D)
{
Data = D;
Next = NULL;
}
}
Readers can also define new methods to operate the linked list as needed. Two-way linked list can be used
Similar implementation only adds a pointer to the forward node for the node class.
This code can be used to implement:
Class Node
{
Object Data;
Node next;
Node previous;
Node (Object D)
{
Data = D;
Next = NULL;
Previous = NULL;
}
}
Of course, the basic operations of two-way linked list are slightly different. The implementation method of linked list and two-way linked list is also
It can be used in the implementation of stacks and queues, so I will not write more here. If you are interested, you can use the list class
The code can be slightly changed. |