Sequential linear table code implementation method

  • 2020-05-19 05:16:55
  • OfStack

1. Use 1 array to realize basic operations such as adding and deleting elements in 1 sequential linear table


package com.ietree.basic.datastructure.Sequence;

import java.util.Arrays;

/**
 *  Sequential linear table 
 * 
 * @param <T>
 * @author Dylan
 */
public class SequenceList<T> {

 private final int DEFAULT_SIZE = 16;
 //  Save the length of the array 
 private int capacity;
 //  define 1 Two arrays are used to hold the elements of a sequential linear table 
 private Object[] elementData;
 //  Saves the current number of elements in the order table 
 private int size = 0;
 
 //  Creates an ordered linear table with the default array length 
 public SequenceList() {
  capacity = DEFAULT_SIZE;
  elementData = new Object[capacity];
 }
 
 //  In order to 1 Create a sequential linear table of initialization elements 
 public SequenceList(T element) {
  this();
  elementData[0] = element;
  size++;
 }
 
 /**
  *  Creates a sequential linear table with an array of specified length 
  * @param element  Specifies the order of order in a linear table 1 An element 
  * @param initSize  Specifies the length of the underlying array of the ordered linear table 
  */
 public SequenceList(T element, int initSize) {
  capacity = 1;
  //  the capacity Set to greater than initSize The smallest 2 the n To the power 
  while (capacity < initSize) {
   capacity <<= 1;
  }
  elementData = new Object[capacity];
  elementData[0] = element;
  size++;
 }
 
 //  Gets the size of the sequential linear table 
 public int length() {
  return size;
 }
 
 //  Gets the index in the sequential linear table as i The element 
 public T get(int i) {
  if (i < 0 || i > size - 1) {
   throw new IndexOutOfBoundsException(" Linear table indexes cross the line ");
  }
  return (T) elementData[i];
 }
 
 //  Finds the index of a specified element in a sequential linear table 
 public int locate(T element) {
  for (int i = 0; i < size; i++) {
   if (elementData[i].equals(element)) {
    return i;
   }
  }
  return -1;
 }
 
 //  Inserts into a sequential linear table at the specified position 1 An element 
 public void insert(T element, int index) {
  if (index < 0 || index > size) {
   throw new IndexOutOfBoundsException(" Linear table indexes cross the line ");
  }
  ensureCapacity(size + 1);
  //  Moves all elements after the specified index backward 1 " 
  System.arraycopy(elementData, index, elementData, index + 1, size - index);
  elementData[index] = element;
  size++;
 }
 
 //  Before inserting an element, you need to make sure that the length of the sequential linear table is greater than the length of the sequential linear table after insertion 
 private void ensureCapacity(int minCapacity) {
  
  //  If the original length of the array is less than the required length 
  if (minCapacity > capacity) {
   //  keep capacity * 2 Until the capacity Is greater than minCapacity
   while (capacity < minCapacity) {
    capacity <<= 1;
   }
   elementData = Arrays.copyOf(elementData, capacity);
  }
 }
 
 //  Add at the beginning of the linear order table 1 An element 
 public void add(T element) {
  insert(element, size);
 }
 
 //  Deletes an element at the specified index in a sequential linear table 
 public T delete(int index) {
  if (index < 0 || index > size - 1) {
   throw new IndexOutOfBoundsException(" Linear table indexes cross the line ");
  }
  T oldValue = (T) elementData[index];
  int numMoved = size - index - 1;
  if (numMoved > 0) {
   System.arraycopy(elementData, index + 1, elementData, index, numMoved);
  }
  //  To empty the last 1 An element 
  elementData[--size] = null;
  return oldValue;
 }
 
 //  Delete the last in the sequential linear table 1 An element 
 public T remove() {
  return delete(size - 1);
 }
 
 //  Determines if the sequential linear table is empty 
 public boolean empty() {
  return size == 0;
 }
 
 //  Empty the linear table 
 public void clear() {
  Arrays.fill(elementData, null);
  size = 0;
 }

 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();
  }
 }

}

Test the basic operation of simulated linear table:


package com.ietree.basic.datastructure.Sequence;

/**
 *  The test class 
 * 
 * @author Dylan
 */
public class SequenceListTest {
 
 public static void main(String[] args) {
  SequenceList<String> list = new SequenceList<String>();
  list.add("aaa");
  list.add("bbb");
  list.add("ccc");
  list.add("ddd");
  list.insert("eee", 1);
  System.out.println(list);
  list.delete(2);
  System.out.println(list);
  System.out.println("ccc Position in the sequential linear table: " + list.locate("ccc"));
 }
}

Program output:


[aaa,eee,bbb,ccc,dd]
[aaa,eee,ccc,dd]

The position of ccc in the sequential linear table: 2


Related articles: