java静态链表_用Java实现一个静态链表

什么是静态链表?

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

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

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

静态链表的节点

数据域:用于存储数据元素的值

游标:即数组下标,表示直接后继元素所在数组中的位置

public class StaticLinkedListNode {

public T data; // 数据

public int cursor; // 游标

...

}

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

备用链表

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

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

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

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

完整代码

public class StaticLinkedListNode {

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 {

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(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 list = new StaticLinkedList();

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值