• java链表


    1什么是链表

    :链表是把每个数据分为一个类,然后通过next指针域连接起来的表,可以通过这样的方法消去数组组的0项;

    链表定义在Java中

    我们需要定义一个当前值和下一个指针

    package com.jiedada.jiegou;
    
    public class Node {
        public int var;//值域
        protected Node next;//指针域
        //构造方法
        public Node(int data) {
            this.var=data;
        }
        public void display() {
            System.out.println(var+" ");
        }
    
    }
    View Code

    链表的部分方法

    package com.jiedada.jiegou;
    
    public class ListLink {
        //定义头节点
        public Node first;
        //定义头节点位置
        private int pos=0;
        public ListLink() {
            this.first=null;
        }
        //插入一个头节点
        public void addFirstNode(int data) {
            Node node=new Node(data);
            node.next=first;
            first=node;
        }
        //删除头节点,并返回头节点
        public Node deleteFirstNode() {
            Node tempNode=first;
            first=tempNode.next;
            return tempNode;
            
        }
        //在任意位置插入节点,在INDEX后面插入
         public void add(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;
            }
        //删除任意位置的节点
        public void delete(int index) {
            Node current=first;
            Node previous=first;
            while(pos!=index) {
                previous=current;
                current=current;
                pos++;
            }
            if(current==first) {
                first=first.next;
                
            }
            else {
                previous.next=current.next;
            }
        }
        //根据节点的data删除节点(删除第一个)
        public Node deleteData(int var) {
            Node current=first;
            Node previous=first;//记住上一个节点
            while(current.var!=var) {
            if(current==null) {
                return null;
            }
            previous=current;
            current=current.next;
            }
            if(current==first) {
                first=first.next;
            }
            else {
                previous.next=current.next;
            }
            return current;
        }
        //显示信息
        public void displayAalNode() {
            Node current=first;
            while(current!=null) {
                current.display();
                current=current.next;
            }
            System.out.println();
        }
        //查找结点
        public Node find(int var) {
            Node current=first;
            while(current.var!=var) {
                if(current.next==null)
                {
                    return null;
                }
                current=current.next;
            }
            return current;
        }
    
    }
    View Code

    链表测试

    package com.jiedada.jiegou;
    
    public class test {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            //链表测试
            ListLink listlink=new ListLink();
            listlink.addFirstNode(20);
            listlink.addFirstNode(21);
            listlink.addFirstNode(22);
            //输出为22,21,20
            listlink.add(1, 23);
            listlink.add(2, 24);
            listlink.add(3, 25);
            //输出为22,23,24,25,21,20
            listlink.displayAalNode();
    
        }
    
    }
    View Code

    代码连接为:https://www.cnblogs.com/_popc/p/4025684.html

  • 相关阅读:
    python+Appium自动化:记录遇到的坑
    python+Appium自动化:Appium元素检测
    python+Appium自动化:id元素定位
    python+Appium自动化:运行第一个appium脚本
    python+Appium自动化:Capability配置简介
    python+Appium自动化:Appium-desktop界面简介
    Appium简介以及环境安装
    monkeyrunner录制和回放功能
    monkeyrunner脚本录制和回放下载
    MonkeyRunner的简介与综合实践
  • 原文地址:https://www.cnblogs.com/xiaoruirui/p/10811630.html
Copyright © 2020-2023  润新知