Java implements a list structure based on a two-way linked list (algorithm source code)

Source: Internet
Author: User

/* For your reference, I share the source code as a collector! */

 

/*
* List ADT Interfaces
*/

Package DSA;

Public interface list {
// Query the current scale of the List
Public int getsize ();

// Check whether the list is empty
Public Boolean isempty ();

// Returns the first element (position)
Public position first ();

// Return the last element (position)
Public position last ();

// Return the element (location) following the given position)
Public position getnext (position P)
Throws exceptionpositioninvalid, exceptionboundaryviolation;

// Returns the element (position) that is near the given position)
Public position getprev (position P)
Throws exceptionpositioninvalid, exceptionboundaryviolation;

// Insert e into the list as the first element
Public position insertfirst (Object E );

// Insert e into the list as the last element
Public position insertlast (Object E );

// Insert e to the position following the given position
Public position insertafter (position P, Object E)
Throws exceptionpositioninvalid;

// Insert e to a position near the given position
Public position insertbefore (position P, Object E)
Throws exceptionpositioninvalid;

// Delete the element at the given position and return it
Public object remove (position P)
Throws exceptionpositioninvalid;

// Delete the first element and return it
Public object removefirst ();

// Delete the last element and return it
Public object removelast ();

// Replace the element at the given position with the new element and return the replaced element.
Public object Replace (position P, Object E)
Throws exceptionpositioninvalid;

// Position iterator
Public iterator positions ();

// Element iterator
Public iterator elements ();
}

 

/*
* List structure based on two-way linked list
*/

Package DSA;

Public class list_dlnode implements list {
Protected int numelem; // the actual scale of the List
Protected dlnode header, trailer; // sentinel: first node + Last Node

// Constructor
Public list_dlnode (){
Numelem = 0; // empty table
Header = new dlnode (null, null, null); // header Node
Trailer = new dlnode (null, header, null); // End Node
Header. setnext (trailer); // the header and tail nodes are linked to each other.
}

********* *******************/
// Check whether the specified position is valid in the list. If yes, convert it to * dlnode
Protected dlnode checkposition (position P) throws exceptionpositioninvalid {
If (null = P)
Throw new exceptionpositioninvalid ("unexpected: the location passed to list_dlnode is null ");
If (header = P)
Throw new exceptionpositioninvalid ("unexpected: the position of the header node is invalid ");
If (trailer = P)
Throw new exceptionpositioninvalid ("unexpected: the position of the last node is invalid ");
Dlnode temp = (dlnode) P;
Return temp;
}

/**************************** ADT method ********* *******************/
// Query the current scale of the List
Public int getsize () {return numelem ;}
 
// Check whether the list is empty
Public Boolean isempty () {return (numelem = 0 );}

// Returns the first element (position)
Public position first () throws exceptionlistempty {
If (isempty ())
Throw new exceptionlistempty ("unexpected: Empty List ");
Return header. getnext ();
}
 
// Return the last element (position)
Public position last () throws exceptionlistempty {
If (isempty ())
Throw new exceptionlistempty ("unexpected: Empty List ");
Return trailer. getprev ();
}

// Returns the element (position) that is near the given position)
Public position getprev (position P)
Throws exceptionpositioninvalid, exceptionboundaryviolation {
Dlnode v = checkposition (P );
Dlnode Prev = V. getprev ();
If (prev = header)
Throw new exceptionboundaryviolation ("unexpected: attempting to cross the list front-end ");
Return Prev;
}

// Return the element (location) following the given position)
Public position getnext (position P)
Throws exceptionpositioninvalid, exceptionboundaryviolation {
Dlnode v = checkposition (P );
Dlnode next = V. getnext ();
If (next = trailer)
Throw new exceptionboundaryviolation ("unexpected: attempting to cross the list backend ");
Return next;
}

// Insert e to a position near the given position
Public position insertbefore (position P, object element)
Throws exceptionpositioninvalid {
Dlnode v = checkposition (P );
Numelem ++;
Dlnode newnode = new dlnode (element, V. getprev (), V );
V. getprev (). setnext (newnode );
V. setprev (newnode );
Return newnode;
}

// Insert e to the position following the given position
Public position insertafter (position P, object element)
Throws exceptionpositioninvalid {
Dlnode v = checkposition (P );
Numelem ++;
Dlnode newnode = new dlnode (element, V, V. getnext ());
V. getnext (). setprev (newnode );
V. setnext (newnode );
Return newnode;
}
 
// Insert e into the list as the first element
Public position insertfirst (Object E ){
Numelem ++;
Dlnode newnode = new dlnode (E, header, header. getnext ());
Header. getnext (). setprev (newnode );
Header. setnext (newnode );
Return newnode;
}

// Insert e into the list as the last element
Public position insertlast (Object E ){
Numelem ++;
Dlnode newnode = new dlnode (E, trailer. getprev (), trailer );
If (null = trailer. getprev () system. Out. println ("!!! Prev of trailer is null !!! ");
Trailer. getprev (). setnext (newnode );
Trailer. setprev (newnode );
Return newnode;
}

// Delete the element at the given position and return it
Public object remove (position P)
Throws exceptionpositioninvalid {
Dlnode v = checkposition (P );
Numelem --;
Dlnode vprev = V. getprev ();
Dlnode vnext = V. getnext ();
Vprev. setnext (vnext );
Vnext. setprev (vprev );
Object velem = V. getelem ();
// Extract the location (node) from the list so that the system can reclaim the occupied space.
V. setnext (null );
V. setprev (null );
Return velem;
}

// Delete the first element and return it
Public object removefirst ()
{Return remove (header. getnext ());}

// Delete the last element and return it
Public object removelast ()
{Return remove (trailer. getprev ());}

// Replace the element at the given position with the new element and return the replaced element.
Public object Replace (position P, object OBJ)
Throws exceptionpositioninvalid {
Dlnode v = checkposition (P );
Object oldelem = V. getelem ();
V. setelem (OBJ );
Return oldelem;
}

// Position iterator
Public iterator positions ()
{Return New iteratorposition (this );}

// Element iterator
Public iterator elements ()
{Return New iteratorelement (this );}
}

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.