List of linked lists

Source: Internet
Author: User

To create a linked list, first create a node class, in Java called the entry (entry), the class is a nested class, which contains three elements, element, next, Previous.

public class Link<e> {

private Static Class Entry<e> {
private E element;

Private entry<e> Next;

Private entry<e> Previous;


Public Entry (e E, entry<e> Next, entry<e> previous) {
This.element = e;
This.next = Next;
This.previous = previous;
}
}
}

Create a head node and an initial head node, plus a size to count the nodes.

Public class Link<e> {

private transient entry<e> header = new entry<e> (null, NULL, NULL);
private transient int size = 0;

/**
* Constructs an empty list.
*/
Public Link () {
Header.next = header.previous = header;
}

private Static class Entry<e> {
private E element;

private entry<e> Next;

private entry<e> previous;


Public Entry (e E, entry<e> Next, entry<e> previous) {
this.element = e;
this.next = next;
this.previous = previous;
}
}
}

Add and get, delete nodes

Package com.hyd.link;

Import java.util.NoSuchElementException;

public class Link<e> {

Private transient entry<e> Header = new entry<e> (null, NULL, NULL);
private transient int size = 0;

/**
* Constructs an empty list.
*/
Public Link () {
Header.next = Header.previous = header;
}

public int size () {
return this.size;
}

Public boolean Add (E e) {
Addbefore (E, header);
return true;
}

Public E get (int index) {
Return Entry (index). element;
}

Public E Remove (int index) {
Return Remove (Entry (index));
}

Private entry<e> Addbefore (e E, entry<e> Entry) {
entry<e> newEntry = new Entry<e> (E, Entry, entry.previous);
NewEntry.previous.next = NewEntry;
newEntry.next.previous = NewEntry;
Size + +;
return newEntry;
}

/**
* Returns the indexed entry.
*/
Private entry<e> Entry (int index) {
if (Index < 0 | | | index > Size) {
throw new Indexoutofboundsexception ("index:" + index +
", Size:" + size);
}

entry<e> E = header;
if (Index < (size >> 1)) {
for (int i = 0; I <= index; i++)
e = E.next;
} else {
for (int i = size; i > index; i--)
e = e.previous;
}


return e;
}

Private e Remove (entry<e> e) {
if (E = = header)
throw new Nosuchelementexception ();

E result = e.element;
E.previous.next = E.next;
e.next.previous = e.previous;
E.next = e.previous = null;
E.element = null;
size--;
return result;
}

private Static Class Entry<e> {
private E element;

Private entry<e> Next;

Private entry<e> Previous;


Public Entry (e E, entry<e> Next, entry<e> previous) {
This.element = e;
This.next = Next;
This.previous = previous;
}
}
}

List of linked lists

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.