Java 静态链表 用Java实现一个静态链表的方法步骤

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Java 静态链表 用Java实现一个静态链表的方法步骤

孙华栋   2021-04-21 我要评论

什么是静态链表?

对于线性链表,也可用一维数组来进行描述。这种描述方法便于在没有指针类型的高级程序设计语言中使用链表结构。

用数组描述的链表,即称为静态链表。

在C语言中,静态链表的表现形式即为结构体数组,结构体变量包括数据域data和游标CUR。

静态链表的节点

数据域:用于存储数据元素的值
游标:即数组下标,表示直接后继元素所在数组中的位置

public class StaticLinkedListNode<T> { 
  public T data; // 数据
  public int cursor; // 游标
  ...
}

注:通常静态链表会将第一个数据元素放到数组下标为1(即a[1])的位置中。

备用链表

静态链表中,除了数据本身通过游标组成链表外,还需要有一条连接各个空闲位置的链表,称为备用链表。

作用:回收数组中未使用或者之前使用过(现在不用)的存储空间,留待后期使用。即静态链表使用数组申请的物理空间中,存在两个链表,一条连接数据,另一条连接数组中为使用的空间。

注:通常备用链表的表头位于数组下标为0(a[0])的位置,而数据链表的表头位于数组下标为1(a[1])的位置。

静态链表中设置备用链表的好处是,可以清楚地知道数组中是否有空闲位置,以便数据链表添加新数据时使用。比如,若静态链表中数组下标为 0 的位置上存有数据,则证明数组已满。

完整代码

public class StaticLinkedListNode<T> {
  public T data;
  private int cursor;

  public StaticLinkedListNode(T data, int cursor) {
    this.cursor = cursor;
  }

  public T getData() {
    return data;
  }

  public void setData(T data) {
    this.data = data;
  }

  public int getCursor() {
    return cursor;
  }

  public void setCursor(int cursor) {
    this.cursor = cursor;
  }
}

public class StaticLinkedList<T> {
  StaticLinkedListNode[] nodes;
  private static final int MAX_SIZE = 100;
  private int length;

  public StaticLinkedList() {
    nodes = new StaticLinkedListNode[MAX_SIZE];
    for (int i = 0; i < MAX_SIZE; i++) {
      nodes[i] = new StaticLinkedListNode<T>(null, i + 1);
    }
    nodes[MAX_SIZE - 1].setCursor(0);
    this.length = 0;
  }

  // 链表实际长度
  public int Length() {
    return length;
  }

  // 判断使用数组是否为空
  public boolean isEmpty() {
    return length == 0;
  }

  // 判断备用链表是否为空
  public boolean isFull() {
    return length == MAX_SIZE;
  }

  // 插入一个节点
  public boolean addTo(T data, int index) {
    if (isFull() || index > MAX_SIZE || index < -1 || data == null)
      return false;
    if (index == 0) {
      insert(data);
      return true;
    }
    if (index > Length()) {
      index = Length();
    }
    // 获取第一个使用节点的下标
    int firstUse = nodes[MAX_SIZE - 1].getCursor();
    // 获取备用数组第一个节点的下标
    int firstNull = nodes[0].getCursor();
    for (int i = 1; i < index; i++) {
      firstUse = nodes[firstUse].getCursor();
    }
    // 获取目标节点的后续节点
    int nextUse = nodes[firstUse].getCursor();
    int nextNull = nodes[firstNull].getCursor();
    nodes[0].setCursor(nextNull);
    nodes[firstUse].setCursor(firstNull);
    nodes[firstNull].setCursor(nextUse);
    nodes[firstNull].setData(data);
    length++;
    return true;
  }

  public void insert(T data) {
    int t = nodes[MAX_SIZE - 1].getCursor();
    int firstNull = nodes[0].getCursor();
    nodes[MAX_SIZE - 1].setCursor(firstNull);
    nodes[0].setCursor(nodes[firstNull].getCursor());
    nodes[firstNull].setCursor(t);
    nodes[firstNull].setData(data);
    length++;
  }

  public void print() {
    int first = nodes[MAX_SIZE - 1].getCursor();
    System.out.println("链表:");
    for (int i = first; i != 0; ) {
      System.out.print(nodes[i].getData() + " ");
      i = nodes[i].getCursor();
    }
  }

  // 删除指定节点
  public boolean delete(T data) {
    if (isEmpty()) {
      return false;
    }
    int temp = MAX_SIZE - 1;
    while (temp != 0) {
      if (nodes[nodes[temp].getCursor()].getData() == data) {
        int p = nodes[temp].getCursor();
        nodes[temp].setCursor(nodes[p].getCursor());
        nodes[p].setCursor(nodes[0].getCursor());
        nodes[0].setCursor(p);
        nodes[p].setData(null);
        length--;
        return true;
      }
      temp = nodes[temp].getCursor();
    }
    return false;
  }

  // 删除所有节点
  public boolean deleteAll() {
    if (isEmpty()) {
      return true;
    }
    for (int i = 0; i < MAX_SIZE - 1; i++) {
      nodes[i].setCursor(i + 1);
      nodes[i].setData(null);
    }
    nodes[MAX_SIZE - 1].setCursor(0);
    nodes[MAX_SIZE - 1].setData(null);
    length = 0;
    return true;
  }

  public void printAll() {
    System.out.println("链表:");
    for (int i = 0; i < MAX_SIZE; i++) {
      System.out.print("[" + i + " " + nodes[i].getData() + " " + nodes[i].getCursor() + "]");
      if (i % 5 == 0 && i != 0) {
        System.out.println();
      }
    }
  }

  public static void main(String[] args) {
    StaticLinkedList<String> list = new StaticLinkedList<String>();
    list.insert("A");
    list.insert("B");
    list.insert("C");
    list.insert("D");
    list.insert("E");
    list.addTo("X", 2);
    System.out.println(list.Length());
    list.print();
//    list.printAll();
//    list.deleteAll();
  }
}

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们