Home  >  Article  >  Java  >  Detailed analysis of the principles and simple implementation methods of linear tables

Detailed analysis of the principles and simple implementation methods of linear tables

ringa_lee
ringa_leeOriginal
2017-10-15 10:43:572967browse

The editor below will bring you a brief discussion of the principles and simple implementation methods of linear tables. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor and take a look.

1. Linear table

Principle: a finite sequence of zero or more similar data elements

Schematic:

Features:

1. Orderliness

2. Finiteness

3. Elements of the same type

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

Linear table is a logical data structure. There are generally two physical implementations: sequence implementation and linked list implementation

2. Array-based linear table sequence Implementation

Principle: Use a storage unit with a continuous address to store linear table data elements in sequence.

Principle diagram:

Algorithm principle:

1. Initialize a fixed-length array space elementData[], size storage length storage Element

2. Quickly access elements through index

3. Implement insertion and deletion of elements through array copy

Summary:

1. No need to add additional storage space to express the logical relationship between elements in the table

2. Elements at any position in the table can be quickly accessed

3. Insertion and deletion require array copying (i.e., the movement of a large number of elements)

4. When the length of the linear table changes greatly, frequent expansion is required, resulting in storage space fragmentation

Implementation code:

Interface definition:


##

package online.jfree.base;

/**
 * author : Guo LiXiao
 * date : 2017-6-14 11:46
 */

public interface LineList <E>{

 /**
  * lineList 是否为空
  * @return
  */
 boolean isEmpty();

 /**
  * 清空 lineList
  */
 void clear();

 /**
  * 获取指定位置元素
  * @param index
  * @return
  */
 E get(int index);

 /**
  * 获取元素第一次出现的位置
  * @param e
  * @return
  */
 int indexOf(E e);

 /**
  * 判断 lineList是否包含指定元素
  * @param e
  * @return
  */
 boolean contains(E e);

 /**
  * 设置指定位置数据,如数据已存在 则覆盖原数据
  * @param index
  * @param e
  * @return
  */
 E set(int index, E e);

 /**
  * 移除指定位置元素
  * @param index
  * @return
  */
 E remove(int index);

 /**
  * 在lineList结尾插入元素
  * @param e
  * @return
  */
 E add(E e);

 /**
  * 在index后面插入元素
  * @param index
  * @param e
  * @return
  */
 E add(int index, E e);

 /**
  * 返回lineList长度
  * @return
  */
 int size();



}

Algorithm implementation:


package online.jfree.base;

/**
 * author : Guo LiXiao
 * date : 2017-6-15 13:44
 */

public class OrderedLineList<E> implements LineList<E> {

 private static final int INIT_CAPACITY = 10;

 private transient E[] elementData;

 private transient int elementLength;

 private int size;

 public OrderedLineList() {
  this(0);
 }

 public OrderedLineList(int initCapacity) {
  init(initCapacity);
 }

 private void init(int initCapacity) {
  if (initCapacity >= 0) {
   this.elementData = (E[]) new Object[initCapacity];
   this.elementLength = initCapacity;
  } else {
   throw new IllegalArgumentException("Illegal Capacity: " +
     initCapacity);
  }
  this.size = 0;
 }

 /**
  * 扩容
  */
 private void dilatation() {
  int oldCapacity = this.elementLength;
  int newCapacity = oldCapacity;
  if (oldCapacity <= this.size) {
   newCapacity = oldCapacity + INIT_CAPACITY;
  }else if(oldCapacity - INIT_CAPACITY > this.size){
   newCapacity = oldCapacity - INIT_CAPACITY;
  }
  if (oldCapacity != newCapacity){
   E[] newElementData = (E[]) new Object[newCapacity];
   System.arraycopy(elementData, 0, newElementData, 0, oldCapacity);
   this.elementLength = newCapacity;
   this.elementData = newElementData;
  }
 }

 /**
  * 校验列表索引越界
  * @param index
  */
 private void checkCapacity(int index){
  if (index > this.size - 1 || index < 0)
   throw new IndexOutOfBoundsException(new StringBuffer("[index : ").append(index).append("] , [size : ").append(size).append("] ").toString());
 }

 @Override
 public boolean isEmpty() {
  return this.size == 0;
 }

 @Override
 public void clear() {
  this.init(0);
 }

 @Override
 public E get(int index) {
  this.checkCapacity(index);
  return this.elementData[index];
 }

 @Override
 public int indexOf(E e) {
  for (int i = 0; i < this.size; i++){
   if (e == null && elementData[i] == null || e.equals(elementData[i])){
    return i;
   }
  }
  return -1;
 }

 @Override
 public boolean contains(E e) {
  return this.indexOf(e) > 0;
 }

 @Override
 public E set(int index, E e) {
  this.checkCapacity(index);
  this.dilatation();
  E oldElement = this.elementData[index];
  this.elementData[index] = e;
  return oldElement;
 }

 @Override
 public E remove(int index) {
  this.dilatation();
  E e = elementData[index];
  if (index == size - 1) elementData[index] = null;
  else {
   int length = size - index - 1;
   System.arraycopy(elementData, index + 1, elementData, index, length);
  }
  size --;
  return e;
 }

 @Override
 public E add(E e) {
  return this.add(size, e);
 }

 @Override
 public E add(int index, E e) {
  this.dilatation();
  if (index == size) elementData[index] = e;
  else {
   index++;
   int lastLength = size - index;
   E[] lastElementData = (E[]) new Object[lastLength];
   System.arraycopy(elementData, index, lastElementData, 0, lastLength);
   elementData[index] = e;
   System.arraycopy(lastElementData, 0, elementData, index + 1, lastLength);
  }
  size ++ ;
  return e;
 }

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

}

The above is the detailed content of Detailed analysis of the principles and simple implementation methods of linear tables. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn