• JAVA学习日报 7.30


    今天我们来研究一下单链表,这个链表当初在上C++时就折磨了我好一阵,但实际上真正理解了也不特别难。、

    先定义一个结点类:

    package natsuyasumi;
    public class Node {
        public int data;
        public Node next;
    
        public Node(int data) {
            this.data = data;
        }
    
        public Node(int data, Node next) {
            this.data = data;
            this.next = next;
        }
    
        public Node() {}
        public void show() {
            System.out.println(data + "
    ");
        }
    }

    再定义一个单链表类:

    public class LinkList {
    
        public Node frist;
        public Node last;
        public int size = 0;
        public LinkList() {
            this.frist = null;
        }
        public boolean isis() {
            return size == 0;
        }
        public void addfrist(int data) {
            Node L = new Link(data);
            L.next = frist;
            frist = L;
            size++;
        }
        public void addlast(int data) {
    
            if (frist == null) {
                frist = new Node(data);
                last = frist;
            } else {
                Node newL = new Node(data);
                last.next = newL;
                last = newL;
            }
            size++;
        }
        public Link removefrist() {
            Node d = frist;
            frist = d.next;
            size--;
            return d;
        }
        public void dellast() {
            Dell(size - 1);
        }
        public void displayAllLink() {
            Node cure = frist;
            while (cure != null) {
                cure.display();
                cure = cure.next;
            }
            System.out.println("长度" + size);
        }
        public Node getData(int index) {
            if (index < 0 && index > size - 1) {
                throw new IndexOutOfBoundsException("越界");
            }
            Node count = frist;
            for (int i = 0; i < size && count != null; i++, count = count.next) {
                if (i == index) {
                    return count;
                }
            }
            return null;
        }
        public int selectIndex(int element) {
            Node current = frist;
            for (int i = 0; i < size && current != null; i++, current = current.next) {
                if (current.data == element) {
                    return i;
                }
            }
            return -1;
        }
        public void delMax() {
            Node cu = frist;
            Node cc = new Link(0);
            for (int i = 0; i < size && cu != null; i++, cu = cu.next) {
                if (cu.data > cc.data) {
                    cc.data = cu.data;
                }
            }
            int data = cc.data;
            int number = selectIndex(data);
            Dell(number);
        }
        public void delMin() {
            Node cu = frist;
            Node cc = new Link(0);
            for (int i = 0; i < size && cu != null; i++, cu = cu.next) {
                if (cu.data < cc.data) {
                    cc.data = cu.data;
                }
            }
            int data = cc.data;
            int number = selectIndex(data);
            Dell(number);
        }
        public void clear() {
            frist = null;
            last = null;
            size = 0;
        }
    }

    测试结果如下:

  • 相关阅读:
    如何生成不规则形状的mask,以解决对图像不规则区域设置ROI的问题
    数字图像处理学习1
    opencv显示图像
    Calling Matlab Neural Network From Other EXE's
    这个百度文档很给力
    opencv不规则ROI——圆形ROI
    问题修改
    skinsmagic美化MFC界面
    第一次使用思维导图
    libsvm使用介绍中文版
  • 原文地址:https://www.cnblogs.com/Sakuraba/p/13452324.html
Copyright © 2020-2023  润新知