Implementation of linked list and two-way linked list in JAVA

Source: Internet
Author: User

 

Linked list is an important data structure and plays an important role in programming.In C and C ++, pointers are used to implement the linked list structure. Because pointers are not provided in JAVA, some people think that linked lists cannot be implemented in JAVA, 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 a conceptual meaning, rather than a non-verbal data type), 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

}

The data domain is defined as an Object class because the Object class is a generalized superclass (the ancestor of all classes), and any class Object can be assigned a value, increasing the universality of the Code. To make the linked list accessible, you also need to define a header. the header must contain a pointer to the first node and a pointer to the current node. To facilitate adding nodes at the end of the linked list, you can add a pointer pointing to the end of the linked list. In addition, you can use a field to indicate the size of the linked list. When the caller wants to increase the size of the linked list, you do not have to traverse the entire linked list.
We can use the List class to implement the linked List structure, and use the variable Head, Tail, Length, and Pointer to implement the header. There are some tips for storing pointers to the current node. Pointer does not store pointers to the current node, but stores pointers to its front node, if the value is null, the current node is the first node. So why should we do this? This is because after we delete the current node, we still need to ensure that the remaining nodes constitute a linked list. If Pointer Points to the current node, it will bring great difficulties to the operation. So how can we get the current node? We define a method named cursor (), and the return value is a pointer to the current node. The List class also defines some methods to perform basic operations on the linked List. By using these basic operations, 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. 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.

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 ()

/* 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 ");

System. in. println ("You can press return to quit ");

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. A two-way linked list can be implemented in a similar way, but the node class adds a pointer to the forward node.

We can use this code 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, so we will not detail them here. The implementation methods of linked lists and two-way linked lists can also be used in the implementation of stacks and queues, so I will not write more here. Interested readers can slightly change the List class code.

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.