Java data structure-static linked list of linear tables

Source: Internet
Author: User

Definition of static linked list:
A node consists of a one-dimensional array and a pointer field that is used to hold data elements, while pointers (also called cursors) inside the pointer field are used to point to the array subscript of the next node. Such a list is called a static linked list.
The first and last position of an array in a linked list requires special processing and no data is stored. A pointer to the node of the first position (that is, the array 0 subscript) is used to hold the array subscript for the first node of the alternate list. The pointer to the node of the last position (that is, the array length MaxSize-1 subscript) is used to hold an array subscript that points to the first Data element with a numeric value, similar to the head node of a single linked list.

Example diagram of a static linked list:

Here is a excerpt from the "Big talk data Structure" example, to explain the static data link list.

The following describes the insert and delete operations for a static list:
Here I drew a picture, a simple description, I believe it should be easy to understand, as follows:

An example diagram of the same deletion is as follows:

The following is the code I implemented in Java, the main function is to implement only insert and delete operations:

Package com.phn.datestructure;/** * @author Pan Hainan * @Email [email protected] * @TODO static linked list * @date July 19, 2015 */public    Class Fostaticlist<e> {//static linked list length private int size;    The capacity of the static list, the actual capacity is capacity-2:capacity-head node-alternate head node =capacity-2 private int capacity;    The head node of the alternate list is private fostaticnode<e> backnode= null;    The position of the alternate list in the array, the default is the first one, which is 0 private int backnodeindex = 0;    The head node of a static linked list, that is, the head node of the data element chain of a static linked list, private fostaticnode<e> headernode = null;    Static list of stored data elements of the array private fostaticnode<e>[] fosn = null;    Default capacity private static final int defualt_capacity = 100;    Public Fostaticlist () {this (defualt_capacity); }/** * @TODO with the parameter constructor, used to initialize the static linked list of assignments, and to set the associated capacity and size * @param initialcapacity static list initialization capacity */public FOSTATICLI        St (int initialcapacity) {this.init (initialcapacity);        This.setcapacity (initialcapacity);    This.setsize ();   }/** * @TODO initialize the assignment static list and set the head node of the static list and the head node of the alternate list * @param initialcapacity  */private void init (int initialcapacity) {//determine if the given initialization parameter is valid if (Initialcapacity < 0) {T        Hrow New RuntimeException ("Array size error:" + initialcapacity);        } fosn = new fostaticnode[initialcapacity]; Assign a static list to the internal e=null, while the cursor is set to i+1 for (int i = 0;i<initialcapacity-1;i++) {Fosn[i] = new fostaticnode<            E> ();        Fosn[i].setcursor (i+1);        } Fosn[initialcapacity-1] = new fostaticnode<e> ();        Set the head node of the static list to the array subscript of the alternate list, that is, the cursor of the initialCapacity-1 node is 0 fosn[initialcapacity-1].setcursor (backnodeindex);        Set the head node of the static list to Headernode This.setheadernode (Fosn[initialcapacity-1]);    Set the head node of the alternate list to Backnode This.setbacknode (Fosn[backnodeindex]); }/** * @TODO static chain footer interpolation Add data element * @param e data element to be added * @return true */public boolean Add (e e) {//        Check the capacity of the list, and carry out the corresponding expansion this.ensurecapacity (size); Assigns a cursor to the alternate Link header node (that is, the first position of the alternate list) to oldbacknodecursor save int olDbacknodecursor = This.backNode.getCursor ();            if (size==0) {/* If the static linked list does not have a data element at present (that is, before the element is added), the cursor of the head node of the static list is pointed to the "position of the cursor corresponding to the alternate chain header node", i.e. the position of the first element of the alternate list */        This.headerNode.setCursor (Oldbacknodecursor);            }else{//assigns the cursor of the head node of the static list to the tempnodecursor int tempnodecursor = This.headerNode.getCursor ();            The following loop is used to find the last element node of the static linked list (data element chain) lastnode fostaticnode<e> lastnode = new fostaticnode<e> ();                while (tempnodecursor!=0) {lastnode= this.fosn[tempnodecursor];            Tempnodecursor= this.fosn[tempnodecursor].getcursor ();        }//sets the value of the Lastnode node's pointer cursor to the first position of the alternate list lastnode.setcursor (oldbacknodecursor);        }//Set the data element for the first position of the alternate list to be e This.fosn[oldbacknodecursor].sete (e); Gets the cursor that is pointed to in the first position of the alternate list and assigns it to newbacknodecursor (the cursor pointed to by the new alternate chain header node) to save, int newbacknodecursor = this.fosn[oldbacknodecurs        Or].getcursor (); Sets the first position of the alternate list (that is, the last one that is currently the chain of data elements for a static linked list)elements) to the head node position of the alternate list (default 0 position) This.fosn[oldbacknodecursor].setcursor (Backnodeindex);        Sets the cursor to which the alternate link header node points to the cursor newbacknodecursor this.backNode.setCursor (newbacknodecursor) to which the new alternate link header node points;        Linked list length plus 1 this.size++;    return true;    }/** * @TODO Delete the index element in the static list based on the index provided * @param index element in static linked list * @return true or false */        public boolean remove (int index) {//Determines whether the given element position is valid; This.capacity-2 indicates the maximum length that a static list can reach: capacity-head node-Alternate head node =capacity-2            if (index<1 | | index>this.capacity-2) {SYSTEM.OUT.PRINTLN ("element not present at this location");        return false;        }//Declare variable preremovecursor to be used as the subscript for the array of data elements to be deleted, or as a cursor to the previous node of the data element that will be deleted.        int preremovecursor = This.headerNode.getCursor (); The following loop is used to find the previous node of the deleted data element Preremovenode and the cursor to the previous node of the data element that will be deleted preremovecursor fostaticnode<e> Preremovenode = NE        W fostaticnode<e> ();        int tempcount = 0; while (tempcount!=index-1) {Preremovenode = tHis.fosn[preremovecursor];            Preremovecursor = Preremovenode.getcursor ();        tempcount++;        }//Declare the variable oldbacknodecursor as the cursor of the header node of the alternate list and assign the value to save.        int oldbacknodecursor = This.backNode.getCursor ();        Sets the head node of the alternate list to point to a cursor that is the subscript preremovecursor this.backNode.setCursor (preremovecursor) of the array of data elements to be deleted;        Assign the cursor to the data element that you want to delete to removecursor and save int removecursor = This.fosn[preremovecursor].getcursor ();        Sets the cursor pointing to the data element to be deleted to the cursor oldbacknodecursor this.fosn[preremovecursor].setcursor (oldbacknodecursor) of the header node of the standby list;        Set the data element to be deleted to NULL, that is, delete this.fosn[preremovecursor].sete (null);        Sets the cursor that points to the previous node of the data element that will be deleted to the cursor removecursor preremovenode.setcursor (removecursor) to which the data element will be deleted;        length minus 1 this.size--;    return true;        } @Override Public String toString () {stringbuffer sb = new StringBuffer ("[");        int currentcursor = This.headerNode.getCursor ();         if (Currentcursor!=backnodeindex) {   Sb.append (currentcursor+ "" +this.fosn[currentcursor]);            Currentcursor = This.fosn[currentcursor].getcursor ();                while (Currentcursor!=backnodeindex) {sb.append ("," +currentcursor+ "" +this.fosn[currentcursor] ");            Currentcursor = This.fosn[currentcursor].getcursor ();    }} return Sb.append ("]"). ToString ();     }/** * @TODO determine if the capacity of the static list exceeds, and the corresponding expansion operation.     * Note: The main thing here is to save the cursor at the last position of the current static list array, save the cursor pointing to the head node, and assign the cursor to the head node of the new static list. * @param currentsize Current length */private void ensurecapacity (int currentsize) {if (currentsize = = this.capacity-            2) {int oldcapacity = this.capacity;            Here I was in accordance with the expansion of ArrayList to carry out, expanding about 1.5 times times or so.            This.capacity = (this.capacity * 3)/2 + 1;            fostaticnode<e>[] NewData = new Fostaticnode[this.capacity];            for (int i = 0; i < oldCapacity-1; i++) {newdata[i] = This.fosn[i]; } newdata[capACITY-1] = new fostaticnode<e> ();            Newdata[capacity-1].setcursor (This.fosn[oldcapacity-1].getcursor ());                for (int i = oldcapacity-1;i<this.capacity-1;i++) {Newdata[i] = new fostaticnode<e> ();            Newdata[i].setcursor (i+1);        } this.fosn = NewData;    }}/** * @return the size */public int size () {return size;    } private void SetSize () {this.size=0;        }/** * @param backnode the Backnode to set */private void Setbacknode (fostaticnode<e> backnode) {    This.backnode = Backnode; }/** * @param headernode the Headernode to set */private void Setheadernode (fostaticnode<e> headerno    DE) {this.headernode = Headernode;  }/** * @param capacity the capacity to set */private void setcapacity (int capacity) {This.capacity    = capacity; }}

In the example diagram of a delete, please contact my code to do
First, write a test method.

public static void main(String[] args) {    FOStaticList<String> fosl = new FOStaticList<String>(6);    fosl.add("元素1");    System.out.println(fosl);    fosl.add("元素2");    fosl.add("元素3");    System.out.println(fosl);    fosl.add("元素4");    System.out.println(fosl);    System.out.println(fosl.size());    fosl.remove(2);    System.out.println(fosl);}

To run the test method, the combination should be better understood.

Finally, the advantages and disadvantages of static linked list.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java data structure-static linked list of linear tables

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.