Analysis of linear Table Principles and simple implementation, analysis of linear principles

Source: Internet
Author: User

Analysis of linear Table Principles and simple implementation, analysis of linear principles

I. Linear table

Principle: finite sequence of zero or multiple similar data elements

Schematic:

Features:

1. orderliness

2. Limited

3. Elements of the same type

4. The first element has no precursor, And the last element has no successor. The middle element has a precursor and a successor.

A linear table is a logical data structure. There are two physical implementation methods: sequential implementation and linked list implementation.

 

2. array-based linear table sequence implementation

Principle: The linear table data elements are stored in sequence using sequential storage units with a segment of addresses.

Schematic:

Algorithm principle:

1. initialize a fixed-length array space elementData [], and store size storage elements.

2. quick access to elements through Indexes

3. insert and delete elements through array Replication

Summary:

1. You do not need to add additional storage space for the logical relationship between elements in the table.

2. quick access to any element in the table

3. insert and delete arrays (that is, moving a large number of elements)

4. When the length of a linear table is large, it needs to be expanded frequently, resulting in storage space fragmentation.

 

Implementation Code:

Interface Definition:

1 package online. jfree. base; 2 3/** 4 * author: Guo LiXiao 5 * date: 6 */7 8 public interface LineList <E> {9 10/** 11 * Whether lineList is blank 12 * @ return13 */14 boolean isEmpty (); 15 16/** 17 * clear lineList18 */19 void clear (); 20 21/** 22 * get the specified position element 23 * @ param index24 * @ return25 */26 E get (int index ); 27 28/** 29 * obtain the first occurrence position of an element 30 * @ param e31 * @ return32 */33 int indexOf (E ); 34 35/** 36 * determine whether lineList contains the specified Element 37 * @ param e38 * @ return39 */40 boolean contains (E ); 41 42/** 43 * Set Data at the specified location, if the data already exists, it will overwrite the original data 44 * @ param index45 * @ param e46 * @ return47 */48 E set (int index, E e ); 49 50/** 51 * remove the specified position element 52 * @ param index53 * @ return54 */55 E remove (int index ); 56 57/** 58 * insert element 59 * @ param e60 * @ return61 */62 E add (E e) at the end of lineList ); 63 64/** 65 * Insert the element 66 * @ param index67 * @ param e68 * @ return69 */70 E add (int index, E e) behind the index ); 71 72/** 73 * return lineList length 74 * @ return75 */76 int size (); 77 78 79 80}View Code

Algorithm Implementation:

1 package online. jfree. base; 2 3/** 4 * author: Guo LiXiao 5 * date: 6 */7 8 public class OrderedLineList <E> implements LineList <E> {9 10 private static final int INIT_CAPACITY = 10; 11 12 private transient E [] elementData; 13 14 private transient int elementLength; 15 16 private int size; 17 18 public OrderedLineList () {19 this (0); 20} 21 22 public OrderedLineList (int initCa Pacity) {23 init (initCapacity); 24} 25 26 private void init (int initCapacity) {27 if (initCapacity> = 0) {28 this. elementData = (E []) new Object [initCapacity]; 29 this. elementLength = initCapacity; 30} else {31 throw new IllegalArgumentException ("Illegal Capacity:" + 32 initCapacity); 33} 34 this. size = 0; 35} 36 37/** 38 * resizing 39 */40 private void dilatation () {41 int oldCapacity = thi S. elementLength; 42 int newCapacity = oldCapacity; 43 if (oldCapacity <= this. size) {44 newCapacity = oldCapacity + INIT_CAPACITY; 45} else if (oldCapacity-INIT_CAPACITY> this. size) {46 newCapacity = oldCapacity-INIT_CAPACITY; 47} 48 if (oldCapacity! = NewCapacity) {49 E [] newElementData = (E []) new Object [newCapacity]; 50 System. arraycopy (elementData, 0, newElementData, 0, oldCapacity); 51 this. elementLength = newCapacity; 52 this. elementData = newElementData; 53} 54} 55 56/** 57 * Check List index out of bounds 58 * @ param index 59 */60 private void checkCapacity (int index) {61 if (index> this. size-1 | index <0) 62 throw new IndexOutOfBoundsException (new StringBuffer ("[index :"). append (index ). append ("], [size :"). append (size ). append ("]"). toString (); 63} 64 65 @ Override 66 public boolean isEmpty () {67 return this. size = 0; 68} 69 70 @ Override 71 public void clear () {72 this. init (0); 73} 74 75 @ Override 76 public E get (int index) {77 this. checkCapacity (index); 78 return this. elementData [index]; 79} 80 81 @ Override 82 public int indexOf (E e) {83 for (int I = 0; I <this. size; I ++) {84 if (e = null & elementData [I] = null | e. equals (elementData [I]) {85 return I; 86} 87} 88 return-1; 89} 90 91 @ Override 92 public boolean contains (E e) {93 return this. indexOf (e)> 0; 94} 95 96 @ Override 97 public E set (int index, E e) {98 this. checkCapacity (index); 99 this. dilatation (); 100 E oldElement = this. elementData [index]; 101 this. elementData [index] = e; 102 return oldElement; 103} 104 105 @ Override106 public E remove (int index) {107 this. dilatation (); 108 E e = elementData [index]; 109 if (index = size-1) elementData [index] = null; 110 else {111 int length = size-index-1; 112 System. arraycopy (elementData, index + 1, elementData, index, length); 113} 114 size --; 115 return e; 116} 117 118 @ Override119 public E add (E e) {120 return this. add (size, e); 121} 122 123 @ Override124 public E add (int index, E e) {125 this. dilatation (); 126 if (index = size) elementData [index] = e; 127 else {128 index ++; 129 int lastLength = size-index; 130 E [] lastElementData = (E []) new Object [lastLength]; 131 System. arraycopy (elementData, index, lastElementData, 0, lastLength); 132 elementData [index] = e; 133 System. arraycopy (lastElementData, 0, elementData, index + 1, lastLength); 134} 135 size ++; 136 return e; 137} 138 139 @ Override140 public int size () {141 return this. size; 142} 143 144}View Code

 

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.