From here, we will be explaining the Java data structure, is it ready? Let ' s go~~
the data structure model in Java can be divided into several parts:
1. Linear structure
2. Tree-shaped structure
3. Graphic or mesh structure
In the next few, we will explain these data structures separately, mainly through the way of Java code to explain the corresponding structure.
Today's explanation is: Java linear structure
The linear structure of Java data structures
When it comes to linear structures, we can be divided into three categories according to the way they are implemented:
1) linear table of sequential structure
2) linear table of chain structure
3) Stack and queue linear table
1. Linear table of sequential structure
The so-called sequential storage, refers to the two elements in the physical storage address and the logical storage address is consistent, logically adjacent to the two elements, they are stored in the physical address
is also adjacent. The typical application for a JDK is the implementation class ArrayList and vector for the list interface (two of them differ in whether they are thread-synchronized).
If you are interested in viewing their source code, let's take an array as an example to imitate them ...
Package Com.yonyou.test;import java.util.arrays;/** * Test class * @author Little Hao * @ Creation date 2015-3-20 */public class Test {public stat IC void Main (string[] args) {sequencelist<string> list=new sequencelist<string> (); SYSTEM.OUT.PRINTLN ("The initialization length of the linear table is:" +list.length ()); List.add ("Hello"); List.add ("World"); List.add ("The Peace of the World"); SYSTEM.OUT.PRINTLN ("the element in the current list is:" +list); List.remove (); SYSTEM.OUT.PRINTLN ("the element in the current list is:" +list); System.out.println ("The current element linear table is empty:" +list.empty ());}} /** * Create a linear list * Note that this class is thread insecure, do not use in multi-threaded * @author Small Hao * @ Created date 2015-3-20 * @param <T> */class sequencelist<t>{// The default initialization length of the array, private int default_size = 10;//, saves the length of the array. private int capacity;//defines an array to hold elements of the sequential linear table private object[] elementdata;//the current number of elements in the Save order table private int size = 0;//Create empty with default array length Sequential linear table Public sequencelist () {capacity = Default_size;elementdata = new Object[capacity];} Creates a sequential linear table with an initialization element public sequencelist (T element) {this (); elementdata[0] = element;size++;} /** * Create a sequential linear table with an array of specified lengths * @param element specifies the first element of the sequential linear table * @param initThe size specifies the length of the underlying array in the sequential linear table */public sequencelist (T element, int initsize) {capacity = 1;//sets the capacity to the smallest 2 of the n-th square while ( Capacity < Initsize) {capacity <<= 1;} Elementdata = new Object[capacity];elementdata[0] = element;size++;} /** * Get the size of the sequential linear table * @return */public int Length () {return size;} /** * Gets the element in the sequential linear table indexed to I * @param i * @return */@SuppressWarnings ("unchecked") public T get (int i) {if (I < 0 | | i > SI ze-1) {throw new indexoutofboundsexception ("linear table index out of Bounds");} Return (T) elementdata[i];} /** * Index of the specified element in the lookup order Linear table * @param element * @return */public int Locate (T element) {for (int i = 0; i < size; i++) {if (E Lementdata[i].equals (Element)) {return i;}} return-1;} /** * Inserts an element into the specified position of the sequential linear table. * @param element * @param index */private void Insert (T element, int index) {if (Index < 0 | | index > size) {throw NE W indexoutofboundsexception ("Linear table index out of Bounds");} Ensurecapacity (size + 1);//move all elements in the index back one cell at a time. System.arraycopy (Elementdata, index, Elementdata, index + 1, size-index); ELEMENTDATa[index] = element;size++;} /** * Adds an element at the beginning of the linear order table. * @param element */public void Add (T element) {Insert (element, size);} /** * Expansion of the underlying array length, very cumbersome and poor performance * @param mincapacity */private void ensurecapacity (int mincapacity) {//if the original length of the array is less than the current required length if (mi Ncapacity > Capacity) {//will continue to capacity * 2 until capacity is greater than mincapacity while (capacity < mincapacity) {capacity < <= 1;} Elementdata = arrays.copyof (elementdata, capacity);}} /** * Delete the element at the specified index in the sequential linear table * @param index * @return */@SuppressWarnings ("unchecked") private T Delete (int index) {if (Index < 0 | | Index > Size-1) {throw new indexoutofboundsexception ("linear table index out of Bounds");} T oldValue = (t) elementdata[index];int nummoved = size-index-1;if (nummoved > 0) {system.arraycopy (Elementdata, Ind Ex+1, Elementdata, Index, nummoved);} Empty the last element elementdata[--size] = Null;return oldValue;} /** * Delete the last element in the sequential linear table * @return */public T Remove () {return Delete (size-1);} /** * Determine if the order Linear table is empty table * @return */public Boolean empty () {return size = = 0;} /** * Empty Linear table */pubLIC void Clear () {//assigns all elements of the underlying array to Nullarrays.fill (Elementdata, null); size = 0;} /** * Rewrite tostring */public String ToString () {if (size = = 0) {return ' [] ';} Else{stringbuilder sb = new StringBuilder ("["); for (int i = 0; i < size; i++) {sb.append (elementdata[i].tostring () + ",");} int len = Sb.length (); return Sb.delete (Len-2, Len). Append ("]"). toString ();}}
The specific explanation here will not say, or that sentence, if interested can view the relevant source code.
2. Linear table of chain structure
the linear table of the chained storage structure is relative to the sequential structure of the linear table. For a chain-stored linear table, the physical address and logical address of each element are indeterminate.
That is, logically adjacent to the two elements, they are physically not necessarily adjacent to each other.
The typical application in Java's JDK is the LinkedList under the list interface (thread is unsafe, not used in multithreading), and if interested can see the relevant source code.
This is just a simple imitation.
The first imitation is a single-linked list:
Package com.yonyou.test;/** * Test class * @author Little Hao * @ Creation date 2015-3-20 */public class Test {public static void main (string[] Ar GS) {linklist<string> list=new linklist<string> (); SYSTEM.OUT.PRINTLN ("The initialization length of the linear table is:" +list.length ()); List.add ("Hello"); List.add ("World"); List.add ("The Peace of the World"); SYSTEM.OUT.PRINTLN ("the element in the current list is:" +list); List.remove (); SYSTEM.OUT.PRINTLN ("the element in the current list is:" +list); System.out.println ("The current element linear table is empty:" +list.empty ());}} /** * Create a chain linear table * Note that this class is thread insecure, do not use in multi-threaded * @author Small Hao * @ Created date 2015-3-20 * @param <T> */class linklist<t>{/** definition An internal class Node,node instance represents the node of the linked list. * * @author Little Hao * @ Creation date 2015-3-20 */private class node{//save node's data private T data;//reference to next node private node next;//parameterless constructor @s Uppresswarnings ("unused") public node () {}//Initializes all the properties of the constructor public node (T data, node next) {This.data = Data;this.next = Next;} }//Save the list's head node private node header;//Save the list's tail node private node tail;//Save the list of nodes that are already contained in private int size;/** * Create empty list */public Linkli St () {//Empty list, header and tail are nullheader = Null;tail = null;} /** * To create a linked list with a specified data element that has only one element * @param element */public linklist (T element) {Header = new node (element, null);//Only one node, head Er, tail all point to the node tail = header;size++;} /** * Returns the length of the linked list * @return */public int Length () {return size;} /** * Gets the element indexed in the chain-linear table at index * @param index * @return */public T get (int index) {return Getnodebyindex (index). Data;} Gets the node at the specified location, based on index indices, private node Getnodebyindex (int index) {if (Index < 0 | | index > SIZE-1) {throw new INDEXOUTOFB Oundsexception ("linear table index out of Bounds");} Starting from the header node, node current = header;for (int i = 0; i < size && current! = null; i++, current = Current.next) {if (i = = index) {return current;}} return null;} /** * Find the index of the specified element in a chain-linear table * @param element * @return */public int Locate (T Element) {//Start searching node current = header;for (int i = 0; I < size && current! = NULL; i++, current = Current.next) {if (Current.data.equals (Element)) {return i;}} return-1;} /** inserts an element into the specified position of the linear chain table. * * @param element * @param index */public void InserT (t element, int index) {if (Index < 0 | | index > size) {throw new indexoutofboundsexception ("linear table index out of Bounds");} If the empty list if (header = = null) {add (element);} else{//when index is 0 o'clock, that is, insert if at the end of the list (index = = 0) {Addatheader (element);} else{//gets the previous node of the insertion point, nodes prev = Getnodebyindex (index-1);//Let Prev's next point to the new node,//Let the next reference of the new node point to the next node of the original prev. Prev.next = new Node (element, prev.next); size++;}}} /** * Use the tail interpolation method to add a new node for the linked list. * @param element */public void Add (T Element) {//If the linked list is also an empty list if (header = = null) {Header = new node (element, null);//Only one node , header, tail all point to the node tail = header;} else{//creates a new node, NewNode = new nodes (element, NULL), or//lets the tail node's next point to the new node Tail.next = newnode;//with the newly node as the new tail node tail = newNode;} size++;} /** adds a new node to the list using the head interpolation method. * * @param element */private void Addatheader (T Element) {//Create new node, let next of new node point to the original header//and take new node as new Headerheader (element, header);//If an empty list before insertion is (tail = = null) {tail = header;} size++;} /** * Delete the element at the specified index in a chain-linear table * @param index * @return */public T Delete (int index) {if (Index < 0 | | index> size-1) {throw new indexoutofboundsexception ("linear table index out of Bounds");} Node del = null;//If the header node is deleted if (index = = 0) {del = Header;header = Header.next;} else{//gets the previous node of the delete point, nodes prev = Getnodebyindex (index-1);//Gets the node that will be deleted del = prev.next;//let the next node of the deleted node point to the node that is being deleted. Prev.next = del.next;//Assigns the next reference of the deleted node to null.del.next = null;} Size--;return Del.data;} /** * Delete the last element in a chain-linear table * @return */public T Remove () {return Delete (size-1);} /** * Determine if the chain linear table is empty table * @return */public Boolean empty () {return size = = 0;} /** * Empty linear table */public void Clear () {//header, tail assigned to Nullheader = Null;tail = Null;size = 0;} /** * Overrides the ToString Method */public String ToString () {//list is empty linked list when if (empty ()) {return "[]";} Else{stringbuilder sb = new StringBuilder ("["); for (Node current = header; Current! = NULL; Current = Current.next) {sb.append (current.data.toString () + ",");} int len = Sb.length (); return Sb.delete (Len-2, Len). Append ("]"). toString ();}}
followed by a circular list (the next point of the tail node of the linked list points to the head node)
The specific code will not write, interested can write their own look, it is not difficult ... The main thing is to pay attention to setting up and maintaining the following statement
Tail.next=header;
Finally, the contents of the doubly linked list (as opposed to a single linked list, each node has not only a next, but also a pre that points to the previous element)
Please refer to the following code for details:
Package com.yonyou.test;/** * Test class * @author Little Hao * @ Creation date 2015-3-20 */public class Test {public static void main (string[] Ar GS) {doublelinklist<string> list=new doublelinklist<string> (); SYSTEM.OUT.PRINTLN ("The initialization length of the linear table is:" +list.length ()); List.add ("Hello"); List.add ("World"); List.add ("The Peace of the World"); SYSTEM.OUT.PRINTLN ("the element in the current list is:" +list); List.remove (); SYSTEM.OUT.PRINTLN ("the element in the current list is:" +list); System.out.println ("The current element linear table is empty:" +list.empty ());}} /** * Create a doubly-linked linear table * Note that this class is thread insecure, do not use * @author Small Hao * @ creation Date 2015-3-20 * @param <T> */class DOUBLELINKLIST<T&G T {//define an inner class Node,node instance that represents the node of the linked list. Private class node{//Save the node's data private T data;//reference to the previous node private node prev;//reference to the next node private node next;//parameterless constructor public N Ode () {}//Initializes all the properties of the constructor public node (T data, Node prev, node next) {This.data = Data;this.prev = Prev;this.next = Next;}} Save the linked list's head node private node header;//Save the list's tail node private node tail;//Save the list of nodes that already contain the number of private int size;/** * Create empty list */public Double Linklist () {//Empty list, header and tail are all Nullheader= Null;tail = null;} /** * Creates a linked list with the specified data element, which has only one element * @param elements */public doublelinklist (T element) {header = new Node (element, NULL, NULL) ;//Only one node, header, tail all points to the node tail = header;size++;} /** * Returns the length of the linked list * @return */public int Length () {return size;} /** * Gets the element indexed in the chain-linear table at index * @param index * @return */public T get (int index) {return Getnodebyindex (index). Data;} /** Gets the node at the specified position according to the index, * @param index * @return */private node getnodebyindex (int index) {if (Index < 0 | | index > size-1) {throw new indexoutofboundsexception ("linear table index out of Bounds");} if (index <= SIZE/2) {//Starting from header node, node current = header;for (int i = 0; I <= size/2 && current! = NULL ; i++, current = Current.next) {if (i = = index) {return current;}}} else{//from the tail node to search for node current = tail;for (int i = size-1; i > SIZE/2 && current! = null; i++, current = Current.prev) {if (i = = index) {return current;}}} return null;} /** * Find the index of the specified element in a chain-linear table * @param element * @return */public int Locate (T Element) {//FromThe head node starts searching for node current = header;for (int i = 0; i < size && current! = null; i++, current = Current.next) {if (CU Rrent.data.equals (Element)) {return i;}} return-1;} /** * Inserts an element into the specified position of the linear chain table. * @param element * @param index */public void Insert (T element, int index) {if (Index < 0 | | index > size) {throw new Indexoutofboundsexception ("linear table index out of Bounds");} If the empty list if (header = = null) {add (element);} else{//when index is 0 o'clock, that is, insert if at the end of the list (index = = 0) {Addatheader (element);} else{//gets the previous node of the insertion point, nodes prev = Getnodebyindex (index-1);//Gets the insertion point node next = prev.next;//Let the next reference of the new node point to the next node, and the Prev reference points to the Prev Node NewNode = new node (element, Prev, next);//Let Prev's next point to the new nodes. Prev.next = newnode;//Let Prev's next node prev point to the new node Next.prev = newnode;size++;}}} /** * Use the tail interpolation method to add a new node for the linked list. * @param element */public void Add (T Element) {//If the linked list is also an empty list if (header = = null) {Header = new Node (element, NULL, NULL);// Only one node, header, tail all point to the node tail = header;} else{//Create a new node, the pre reference of the new node points to the original tail node NewNode = new node (element, tail, NULL);//Let next of the tail node point to the new node Tail.next = newnode;//as a new tail node tail = newNode;} size++;} /** * Add a new node to the list using the head interpolation method. * @param element */public void Addatheader (T Element) {//Create new node, let next of the new node point to the original header//and take the new node as the new Headerheader Ement, NULL, header),//If the empty list is before insert if (tail = = null) {tail = header;} size++;} /** * Delete the element at the specified index in a chain-linear table * @param index * @return */public T Delete (int index) {if (Index < 0 | | index > SIZE-1) {thro W New indexoutofboundsexception ("linear table index out of Bounds");} Node del = null;//If the header node is deleted if (index = = 0) {del = Header;header = header.next;//releases the prev reference of the new header node Header.prev = Nu ll;} else{//gets the previous node of the delete point, nodes prev = Getnodebyindex (index-1);//Gets the node that will be deleted del = prev.next;//let the next node of the deleted node point to the node that is being deleted. Prev.next = del.next;//The prev of the next node of the deleted node points to the Prev node. if (del.next! = null) {Del.next.prev = prev;} Assign the Prev, next reference of the deleted node to Null.del.prev = Null;del.next = null;} Size--;return Del.data;} /** * Delete the last element in a chain-linear table * @return */public T Remove () {return Delete (size-1);} /** * Determine if the chain linear table is empty linked list * @return */public Boolean Empty () {return size = = 0;} /** * Empty linear table */public void Clear () {//assigns all elements of the underlying array to Nullheader = Null;tail = Null;size = 0;} /** * Overrides the ToString Method */public String ToString () {//list is empty linked list when if (empty ()) {return "[]";} Else{stringbuilder sb = new StringBuilder ("["); for (Node current = header; Current! = NULL; Current = Current.next) {sb.append (current.data.toString () + ",");} int len = Sb.length (); return Sb.delete (Len-2, Len). Append ("]"). toString ();}} Public String reversetostring () {//list is empty linked list when if (empty ()) {return "[]";} Else{stringbuilder sb = new StringBuilder ("["); for (Node current = tail; Current! = NULL; Current = Current.prev) {sb.append (current.data.toString () + ",");} int len = Sb.length (); return Sb.delete (Len-2, Len). Append ("]"). toString ();}}
Due to space limitations, for the stack and queue in a linear structure, please see the next blog post ~ ~ ~
Linear table of Java data structures