Realization of linked list and bidirectional linked list in Java

Source: Internet
Author: User

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;//指向下一个结点
}

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.

Related Article

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.