--java realization of the doubly linked list of linear linked list

Source: Internet
Author: User

. Linear chain-type storage structure: Any storage unit that takes a set of addresses holds data elements in a linear table.
Linked lists can also be divided into:

    • Single linked list: Each node retains only one reference, which points to the next node of the current node, no reference to the head node, and the next reference to the tail node is null.

    • Circular link list: A linked list that is connected to the end.

    • Doubly linked list: Each node has two references, one pointing to the previous node of the current node, and the other to the next node of the current node.


The following gives the implementation of the linear table doubly linked list: LinkedList in Java is a chain implementation of linear tables, and is a doubly linked list.

Import java.util.NoSuchElementException;


Linear table bidirectional linked list storage structure
public class Dulinklist<t> {

Defines an inner class Node,node instance that represents a node of a linked list
Private Class node{
Saving data for a node
Private T data;
A reference to the previous node
Private Node prev;
Reference to the next node
Private Node Next;
constructor with no parameters
Public Node () {

}
Constructors that initialize all properties
Public node (T data, Node prev, node next) {
This.data = data;
This.prev = prev;
This.next = Next;
}
}
Save the head node of the linked list
Private Node header;
Save the tail node of the linked list
Private Node tail;
Save the number of nodes that are already contained in the list
private int size;
Create an empty list
Public Dulinklist () {
Empty list, header and tail are null
Header = null;
tail = null;
}
Creates a linked list with the specified data element, with only one element of the linked list
Public dulinklist (T element) {
Header = new Node (element, NULL, NULL);
tail = header;
size++;
}
Determine if a chain-type linear table is an empty list
public Boolean empty () {
return size = = 0;
}
Empty the linear table
public void Clear () {
for (Node current = header; current! = null;) {
Node next = Header.next;
Current.data = null;
Current.prev = null;
Current.next = null;
current = next;
}
Header = null;
tail = null;
size = 0;
}
Gets the element indexed as index in a chain-linear table
Public T get (int index) {
Return Getnodebyindex (index). data;
}
Gets the node at the specified position according to index
Private Node getnodebyindex (int index) {
if (Index < 0 | | index > SIZE-1) {
throw new Indexoutofboundsexception ("linear table index out of Bounds");
}
if (Index < (size >> 1)) {
Traversing from the header node
Node current = header;
for (int i=0; i < index; i++) {
current = Current.next;
}
return current;
}else{
Node current = tail;
for (int i = size-1; i > Index; i--) {
current = Current.prev;
}
return current;
}
}
Finding the index of a specified element in a chain-linear table
public int Locate (T element) {
Node current = header;
for (int i=0;i<size && Current! = null;i++, current = Current.next) {
if (current.data.equals (element)) {
return i;
}
}
return-1;
}
Returns the length of the linked list
public int Length () {
return size;
}
Inserting an element into the table header of a linear list
public void AddFirst (T element) {
Linkfirst (Element);
}
Inserting an element into the linear list header
public void Linkfirst (T element) {
Node F = header;
Node NewNode = new node (element,null,f);
Header = NewNode;
if (f = = null) {
tail = NewNode;
}else{
F.prev = NewNode;
}
size++;
}
Inserts an element into the footer of a linear list
public void AddTail (T element) {
Linktail (Element);
}
Inserts an element at the end of the linear list
public void Linktail (T element) {
Node t = tail;
Node NewNode = new node (element, T, NULL);
tail = NewNode;
if (t = = null) {
Header = NewNode;
}else{
T.next = NewNode;
}
size++;
}
Inserting a node in front of an element in a linear table
public void Linkbefore (T Element, node node) {
Node pre = Node.prev;
Node NewNode = new node (element, Pre, node);
Node.prev = NewNode;
if (pre = = null) {
Header = NewNode;
}else{
Pre.next = NewNode;
}
size++;
}
Inserts an element to a specified position in a linear list
public void Insert (T element, int index) {
if (Index < 0 | | | index > Size) {
throw new Indexoutofboundsexception ("linear table index out of Bounds");
}
if (index = = size) {
AddTail (Element);
}else{
Linkbefore (Element,getnodebyindex (index));
}
}
Removing the head node of a linear list
public void Removefirst () {
Node first = header;
if (first = = null)
throw new Nosuchelementexception ("This node does not exist");
Unlinkfirst (first);
}
Delete a head node
public void Unlinkfirst (node node) {
Node next = Node.next;
Node.data = null;
Node.next = null;
Header = Next;
if (next = = null) {
tail = null;
}else{
Next.prev = null;
}
size--;
}
Removing the tail node of a linear list
public void RemoveTail () {
Node last = tail;
if (last = = null)
throw new Nosuchelementexception ("This node does not exist");
Unlinklast (last);
}
Delete Tail node
public void Unlinklast (node node) {
Node pre = Node.prev;
Node.data = null;
Node.prev = null;
tail = pre;
if (pre = = null) {
Header = null;
}else{
Pre.next = null;
}
size--;
}
Remove any one of the nodes in a linear table
public void Remove (int index) {
if (Index < 0 | |, index &GT;SIZE-1) {
throw new Indexoutofboundsexception ("linear table out of bounds");
}
Unlink (Getnodebyindex (index));
}
Delete any element in a linear table
public void Unlink (node node) {
Node pre = Node.prev;
Node next = Node.next;
Node.data = null;
if (pre = = null) {
Header = Next;
}else{
Pre.next = Next;
Node.prev = null;
}
if (next = = null) {
tail = pre;
}else{
Next.prev = pre;
Node.next = null;
}
size--;
}
}

--java realization of the doubly linked list of linear linked list

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.