首页 技术 正文
技术 2022年11月9日
0 收藏 815 点赞 3,494 浏览 4332 个字

使用java实现单链表—-(java中的引用就是指针)转载自:https://www.cnblogs.com/zhongyimeng/p/9945332.html

?

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 //一直以为java中没有指针,其实java的引用就是指针,只不过堆栈中的引用储存了在堆中的地址,可以看做java中的指针。<br>public class sibgleLink<E> {    // 结点内部类    private class Node {        private Object data;        private Node next = null;         public Node() {            data = null;        }         // 带数据的构造函数        public Node(E data) {            this.data = data;        }     }     private Node head; // 头引用(指针)    private Node rear; // 尾引用(指针)    private Node point; // 临时引用(指针)    private int length = 1; // 链表长度     // 带参数的构造函数    public sibgleLink(E e) {        head = new Node();        head.data = e;        rear = head;        length = 1;    }     // 尾插法    public void add(E elem) {        point = new Node(elem);        rear.next = point;        rear = point;        length++;     }     // 遍历节点    public void traverse() {        point = head; // 移动临时引用到头结点        if (head != null)            System.out.print("[" + head.data + "]");        while (point.next != null) {            System.out.print("->[" + point.next.data + "]");            point = point.next;        }        System.out.println();    }     // 返回长度    public int length() {        return this.length;    }     // 清除    public boolean clear() {        while (head.next.next != null) {             head.next = head.next.next;        }         head.next = null;        rear = head;        point = null;        length = 1;        return true;     }     // 插入元素    public boolean insert(int x, E data) {        // 工作节点        point = head;        int wz = 1;        if (x == 1) {            Node n = new Node(data);            n.next = head;            head = n;            length++;            return true;         }         if (x < 1 || x > this.length) {            return false;         } else {             while (point.next != null && wz < x - 1) {                point = point.next;                wz++;            }             // 放入一个节点            Node n = new Node(data);            n.next = point.next;            point.next = n;            length++;             return true;         }     }     // 删除    public boolean del(int x) {        point = head;        int wz = 1;        if (x < 0 || x > length) {            return false;         } else if (x == length) {            point = head;            while (point.next != null) {                point = rear;                point.next = null;                length--;             }         } else {             while (point.next != null && wz < x - 1) {                point = point.next;                wz++;            }            Node d = point.next;            point.next = point.next.next;            d = null;             return true;         }         return false;     }     // 更改    public boolean change(int x, E data) {        point = head;        int wz = 1;        if (x < 0 || x > length) {            return false;         } else {             while (point.next != null && wz < x) {                point = point.next;                wz++;            }            point.data = data;             return true;         }     }     // 移动指针    private Node movePoint(int position) {        if (position < 0)            return head;        if (position > length)            return rear;         if (position >= 0 && position <= length) {            point = head;            while (point != null) {                if (position == 0)                    break;                position--;                point = point.next;            }        }         return point;     }     public E find(int position) {        if (position >= 0 && position < length) {            Node tmp = movePoint(position);            return (E) tmp.next.data;        }        return null;    }     // test    public static void main(String[] args) {         sibgleLink<Integer> si = new sibgleLink<Integer>(0);        si.add(5);        si.add(6);        si.insert(2, 2);        si.traverse();        si.del(3);        si.traverse();        si.change(3, 77);        si.traverse();        System.out.println(si.length());     } }

  结果:

?

1234 [0]->[2]->[5]->[6][0]->[2]->[6][0]->[2]->[77]4
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,497
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,910
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,744
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,497
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,135
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,298