• Java单链表实现


    /**
     * 
     * 单链表基本操作
     * 
     * @author John
     *
     */
    class LinkList {
    
        private Node first;
        private int pos = 0;
    
        public LinkList() {
            this.first = null;
        }
    
        /**
         * 插入头结点
         * 
         * @param data
         */
        public void insertFirstNode(int data) {
            Node node = new Node(data);
            node.next = first;
            first = node;
        }
    
        /**
         * 删除头结点
         * 
         * @return
         */
        public Node deleteFirstNode() {
            Node tempNode = first;
            first = first.next;
            return tempNode;
        }
    
        /**
         * 在index之后的位置插入date数据
         * 
         * @param index
         * @param data
         */
        public void insertNode(int index, int data) {
            Node node = new Node(data);
            Node current = first;
            Node previous = first;
            while (pos != index) {
                previous = current;
                current = current.next;
                pos++;
            }
            node.next = current;
            previous.next = node;
            pos = 0;
        }
    
        /**
         * 根据位置删除节点
         * 
         * @param index
         * @return
         */
        public Node deleteBypos(int index) {
            Node current = first;
            Node previous = first;
            while (pos != index) {
                pos++;
                previous = current;
                current = current.next;
            }
            if (current == first) {
                first = first.next;
            } else {
                pos = 0;
                previous.next = current.next;
            }
            return current;
        }
    
        /**
         * 根据节点数据删除指定节点
         * 
         * @param data
         * @return
         */
        public Node deleteByData(int data) {
            Node current = first;
            Node previous = first;
            Node temp = null;
            while (current != null) {
                while (current.data != data) {
                    if (current.next == null) {
                        if (temp != null)
                            return temp;
                        return null;
                    }
                    previous = current;
                    current = current.next;
                }
                if (current == first) {
                    temp = current;
                    first = first.next;
                    current = first;
                    previous = first;
                } else {
                    temp = current;
                    previous.next = current.next;
                    current = current.next;
                }
            }
            return temp;
        }
    
        /**
         * 根据某一位置查找节点信息
         * 
         * @param index
         * @return
         */
        public Node findByPos(int index) {
            Node current = first;
            if (pos != index) {
                current = current.next;
                pos++;
            }
            pos = 0;
            return current;
        }
    
        /**
         * 输出所有节点信息
         */
        public void displayAllNodes() {
            Node current = first;
            String next = "";
            while (current != null) {
                System.out.print(next);
                current.display();
                current = current.next;
                next = " --> ";
            }
            System.out.println();
        }
    
        /**
         * 得到链表的长度
         * 
         * @return
         */
        public int length() {
            Node current = first;
            int lenght = 0;
            while (current != null) {
                lenght++;
                current = current.next;
            }
            return lenght;
        }
    
        /**
         * 对链表元素数据进行排序
         */
        public void sort() {
            int n = this.length();
            int temp = 0;
            Node p = first;
            if (first == null || first.next == null) {
                return;
            }
            for (int i = 1; i < n; i++) {
                p = first;
                for (int j = i; j < n; j++) {
                    if (p.data > p.next.data) {
                        temp = p.data;
                        p.data = p.next.data;
                        p.next.data = temp;
                    }
                    p = p.next;
                }
            }
        }
    
        /**
         * 反转链表
         */
        public void reverse() {
            if (first == null || first.next == null) {
                return;
            }
            Node p1 = first;
            Node p2 = p1.next;
            Node p3 = null;
            while (p2 != null) {
                p3 = p2.next;
                p2.next = p1;
                p1 = p2;
                p2 = p3;
            }
            first.next = null;
            first = p1;
        }
    
    }
    
    class Node {
        Node next;
        int data;
    
        public Node(int data) {
            this.data = data;
        }
    
        public void display() {
            System.out.print(data);
        }
    }
  • 相关阅读:
    selenium之css定位
    selenium之Xpath定位
    配置JAVA_HOME踩得坑 。。
    linux 怎么查看系统的环境变量 与设置jdk 系统环境变量
    jenkins添加环境变量 ,win 10的 环境变量如下,win7 就是不加也可以运行,不报 “python 不是内部命令 ” 的错误。
    win7 bat copy 一个文件 到另外的文件夹内,路径得用引号哦
    路由器原理
    mven入门
    局域网内和局域网间的通信
    详解DNS域名解析全过程
  • 原文地址:https://www.cnblogs.com/SaraMoring/p/5794361.html
Copyright © 2020-2023  润新知