java实现静态链表

很多高级语言不具备指针的概念,这时在实现链表的功能时就可以使用静态链表来完成。
静态链表就是一个使用数组来描述链表的方法,使用数组的下标作为游标来代替指针的概念。每一个节点除了储存数据外还必须有一个字段用来储存下一个节点的下表。这样同样能够实现链表方便插入和删除的特点。
下面是使用java来实现静态链表的代码示例

import java.util.Scanner;

public class StaticLinkList {
    private static Note []notes = new Note[10000];
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //假设由终端输入集合元素,先输入集合A的元素创建加入链表la,然后输入集合B的元素,查找链表la
        //如果la中存在元素与输入的元素相同则删除la中的该元素,如果没有相同则将此元素插入到la之后
        initLinkList();
        int head = malloc();
        int tail = head;
        int n = sc.nextInt();           //集合A有n个元素
        for ( int i = 0; i < n; i ++) {
            int a = sc.nextInt();
            int p = malloc();
            notes[p].data = a;
            notes[p].cursor = 0;
            notes[tail].cursor = p;
            tail = p;
        }
        int m = sc.nextInt();
        for ( int i = 0; i < m; i ++) {
            int a = sc.nextInt();
            int j = notes[head].cursor;
            int b = head;
            while ( j != notes[tail].cursor && a != notes[j].data) {
                b = j;
                j = notes[j].cursor;
            }
            if (j == notes[tail].cursor) {
                int p = malloc();
                notes[p].data = a;
                notes[p].cursor = notes[tail].cursor;
                notes[tail].cursor = p;
            } else {
                notes[b].cursor = notes[j].cursor;
                free(j);
                if (j == tail) {
                    tail = b;
                }
            }
        }
        show(head);
    }
    private static  void initLinkList() {
        //创建未使用节点的链表
        for (int i = 0; i < notes.length - 1; i ++) {
            notes[i] = new Note(i+1);
        }
        notes[notes.length-1] = new Note(0);
    }
    private static  int malloc() {
        //分配节点
        if (notes[0].cursor == 0) {
            //除头节点外都已经使用完毕
            return -1;
        }
        int i = notes[0].cursor;
        notes[0].cursor = notes[i].cursor;
        return i;
    }
    private static void free(int i) {
        //回收i节点
        notes[i].cursor = notes[0].cursor;
        notes[0].cursor = i;
    }
    private static  void show(int head) {
        int i = notes[head].cursor;
        System.out.print("[");
        while ( i != 0) {
            System.out.print(notes[i].data + ",");
            i = notes[i].cursor;
        }
        System.out.println("]");
    }
}
class Note {
    public int data;
    public int cursor;
    public Note(int cursor) {
        this.cursor = cursor;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

M FS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值