_JSP programming of linked list and bidirectional linked list in Java language

Source: Internet
Author: User
Tags int size

The linked list is an important data structure, which occupies a very important position in the program design. C and C + + language is the use of pointers to implement the linked list structure, because the Java language does not provide pointers, so some people think that in the Java language can not implement the linked list, in fact, the Java language than C and C + + is easier to implement the linked list structure. Object references in the Java language are actually a pointer (the pointers in this article are conceptual, not language-provided data types), so we can write classes to implement the nodes in the list.

Class Node
{
Object data;
Node next;//point to the next node
}

The data field is defined as an object class because the object class is a generalized superclass, and any class object can assign a value to it, increasing the versatility of the code. In order for the linked list to be accessible you also need to define a table header, which must contain a pointer to the first node and a pointer to the current node. In order to facilitate the addition of nodes at the end of the list, you can add a pointer to the tail of the linked list, and you can also use a field to represent the size of the linked list, and when the caller wants the size of the linked list, you do not have to traverse the entire list. The following figure is a schematic of this list:

data structure of a linked list

We can use class list to implement the list structure, with variable head, Tail, Length, pointer to implement the table header. There is a technique for storing pointers to the current node, and instead of storing pointers to the current node, pointer stores pointers to its forward nodes, indicating that the current node is the first node when its value is null. So why do you do it? This is because when you delete the current node, you still need to ensure that the remaining nodes form a linked list, which can be very difficult if the pointer points to the current node. So how do we get the current node, we define a method cursor (), and the return value is a pointer to the current node. Class list also defines a number of methods to achieve the basic operation of the linked list, by using these basic operations we can do 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 contents, making its successor node the current node, and if the last node is deleted, the first node becomes the current node.

The source code for the list of list categories is as follows:

Import java.io.*;
public class List
{
/* Use variables to implement the table head * *
Private Node Head=null;
Private Node Tail=null;
Private Node Pointer=null;
private int length=0;
public void DeleteAll ()
* * Clear the entire list/
{
Head=null;
Tail=null;
Pointer=null;
length=0;
}
public void Reset ()
/* Linked list reset, so that the first node to become the current node.
{
Pointer=null;
}
public Boolean IsEmpty ()
/* To determine whether the linked list is empty * *
{
return (length==0);
}
public boolean isend ()
/* To 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 ()
/* Returns the value of the next node of the current node and makes 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 ()
/* Returns 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 list Size * *
{
return (Length);
}
Public Object Remove ()
/* The current node is moved out of the list, the next node becomes the current node, if the removed node is the last node, then 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 ()
/* Returns the pointer to 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)
/* List of simple application examples * *
{
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 ("Can press Return to quit\n");
Try
{
System.in.read ();
Ensure users see the results of the program running
}
catch (IOException E)
{}
}
}
Class Node
/* The definition of the node that forms the linked list * *
{
Object data;
Node Next;
Node (Object D)
{
Data=d;
Next=null;
}
}

The reader can also define a new method to manipulate the linked list according to the actual needs. A bidirectional linked list can be implemented in a similar way by simply adding a node to a pointer to a forward junction point.

You can use this code to implement the following:

Class Node
{
Object data;
Node Next;
Node previous;
Node (Object D)
{
Data=d;
Next=null;
Previous=null;
}
}

Of course, the implementation of the two-way linked list basic operation is slightly different. Linked list and two-way linked list implementation method, can also be used in the stack and queue implementation, here is no longer write, interested readers can be a list of code changes can be slightly.

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.