アルゴリズムとデータ構造(2)線状

リニアテーブル

定義

n个元素组成的有限的有序的一组数据,且都是相同的数据类型,个数n记为长度;
概念:
首节点,尾节点,空表,前驱,后继,直接前驱,直接后继-->顾名思义

コードの実装

  1. 達成するために、順次ストレージ構造に依存しています
  2. ストレージを実現するために鎖構造に依存しています

2つのストレージ構造の比較

格納順序の
利点:
大規模1つの記憶密度、記憶空間の高い利用;
欠点:
1.便利な挿入及び削除、移動する必要のメモリ位置、
チェーン店:
上記とは逆に利点と欠点は、原則から分析することができる、無反復;

達成するための構造

インタフェース:

public interface List {

    void clear();

    Boolean isEmpty();

    int length();

    Object get(int index) throws Exception;

    void insert(Object obj, int index) throws Exception;

    void remove(int index) throws Exception;

    int indexOf(Object obj);
}

インタフェース:

public class LinearList implements List {
   private Object[] listElem;
   private int curLength;

   public LinearList(int maxSize) {
       this.curLength = 0;
       this.listElem = new Object[maxSize];
   }

   @Override
   public void clear() {
       Arrays.fill(listElem,null);
       curLength = 0;
   }

   @Override
   public Boolean isEmpty() {
       return curLength == 0;
   }

   @Override
   public int length() {
       return curLength;
   }

   @Override
   public Object get(int index) throws Exception {
       if (index < 0 || index > curLength - 1)
           throw new Exception("out Of Inedx Exception");
       return listElem[index];
   }

   @Override
   public void insert(Object obj, int index) throws Exception {
       if (curLength == listElem.length)
           throw new Exception("full size , can't insert");
       if (index < 0 || index > curLength)
           throw new Exception("out of index exception");
       for (int i = index; i < curLength - 1; i++)
           listElem[i] = listElem[i - 1];
       listElem[index] = obj;
       curLength++;

   }

   @Override
   public void remove(int index) throws Exception {
       if (index < 0 || index > curLength)
           throw new Exception("out of index exception");
       for (int i = index; i < curLength - 1; i++)
           listElem[i] = listElem[i + 1];
       curLength--;
   }

   @Override
   public int indexOf(Object obj) {
       int j = 0;
       while (j < curLength && !listElem[j].equals(obj))
           j++;
       if (j < curLength) return j;
       else return -1;
   }
}

配列リスト

この時点で、回避消耗メモリ、リニア形の元の長さのない長さにするために最初の長さ、拡張必要が考慮される場合、JavaのArrayListの部分、すなわち、テーブル線形公式のArrayList、音符の機能コード補完。
知っているソースコードを参照してください。

  1. 初期容量10
    private static final int DEFAULT_CAPACITY = 10;
  1. クロス寛容アルゴリズム、1.5倍の拡大。
    private void grow(int minCapacity) {
       // overflow-conscious code
       int oldCapacity = elementData.length;
       int newCapacity = oldCapacity + (oldCapacity >> 1);
       if (newCapacity - minCapacity < 0)
           newCapacity = minCapacity;
       if (newCapacity - MAX_ARRAY_SIZE > 0)
           newCapacity = hugeCapacity(minCapacity);
       // minCapacity is usually close to size, so this is a win:
       elementData = Arrays.copyOf(elementData, newCapacity);
   }
// ***重点行
       int newCapacity = oldCapacity + (oldCapacity >> 1);
  1. パン字追加します。

達成するためのチェーン

用任意的存储单元来存储线性表中的数据元素-->链式线性表
链表-->任意存储单元

そして、リスト:
LinkedListのは
、上述したListインタフェースとのインタフェース。

public class Link implements List {
    private Node head;
    private int size;

    public Link() {
        this.size = 0;
        this.head = new Node(null);
    }

    private class Node {
        private Object data;
        private Node next;

        public Node(Object data) {
            this.data = data;
        }
    }

    @Override
    public void clear() {
        head = null;
        size = 0;
    }

    @Override
    public Boolean isEmpty() {
        return size == 0;
    }

    @Override
    public int length() {
        return size;
    }

    @Override
    public Object get(int index) throws Exception {
        Node indexNode = head;
        if (index < 0 || index > size - 1)
            throw new Exception("out of index exception");
        while (index != 0) {
            indexNode = indexNode.next;
            index--;
        }
        return indexNode.data;
    }

    @Override
    public void insert(Object obj, int index) throws Exception {
        if (index < 0 || index > size - 1)
            throw new Exception("out of index exception");
        Node insertNode = new Node(obj);
        Node indexNode = head;
        while (index != 0) {
            indexNode = indexNode.next;
            index--;
        }
        insertNode.next = indexNode.next;
        indexNode.next = insertNode;
        size++;
    }

    @Override
    public void remove(int index) throws Exception {
        if (index == 0)
            head = head.next;
        else {
            Node indexNode = head;
            while (index != 1) {
                indexNode = indexNode.next;
                index--;
            }
            indexNode.next = indexNode.next.next;
        }
        size = size - (size == 0 ? 0 : 1);
    }

    @Override
    public int indexOf(Object obj) {
        int j = 0;
        Node indexNode = head;
        while (j < size || !indexNode.data.equals(obj)) {
            j++;
            indexNode = indexNode.next;
        }
        if (j < size)
            return j;
        else
            return -1;
    }
}

そして、LinkedListの中のjava。

公開された17元の記事 ウォンの賞賛0 ビュー372

おすすめ

転載: blog.csdn.net/qq_32193775/article/details/103833277