zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    转:Windows 7下安装CentOS双系统
    STL学习总结之<迭代器>
    转:linux静态库与动态库
    指向类成员和成员函数的指针
    STL学习总结之<仿函数>
    转:Linux Crontab 定时任务 命令详解
    转: 解决 Redhat 出现”This system is not registered with RHN”更新
    IOS 判断设备屏幕尺寸、分辨率
    IOS 文件管理共通函数整理
    IOS 编译ffmpeg For SDK6.1,模拟器、armv7、armv7s均可使用
  • 原文地址:https://www.cnblogs.com/xiaoruirui/p/10811630.html
Copyright © 2011-2022 走看看