java链表

Posted 橘子酱ing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java链表相关的知识,希望对你有一定的参考价值。

1、定义节点类、设置节点、遍历输出

class Node{
    private String data;
    private Node next;
    public Node(String data){
        this.data = data;
    }
    public String getData(){
        return this.data;
    }
    public void setNext(Node next) {
        this.next = next;
    }
    public Node getNext(){
        return this.next;
    }
}
public class LinkDemo1 {
    public static void main(String[] args) {
        Node root = new Node("火车头");
        Node node1 = new Node("车厢1");
        Node node2 = new Node("车厢2");
        Node node3 = new Node("车厢3");
        root.setNext(node1);
        node1.setNext(node2);
        node2.setNext(node3);
        print(root);
    }
    public static void print(Node node){
        System.out.println(node.getData());
        if(node.getNext()!=null){
            print(node.getNext());
        }
    }
}

输出结果:

火车头
车厢1
车厢2
车厢3

 2、基本操作:增加元素,打印元素,查询元素、删除元素,获取元素,修改元素,统计链表长度,判断是否为空。

class Link{
    class Node{
        private String data;
        private Node next;
        public Node (String data){
            this.data = data;
        }
        public void add(Node newNode){
            if(this.next==null){
                this.next = newNode;
            }else {
                this.next.add(newNode);
            }
        }
        public void print(){
            System.out.println(this.data);
            if (this.next!=null){
                this.next.print();
            }
        }
        public boolean isContains(String data){
            if(data.equals(this.data)){
                return true;
            }else {
                if(this.next!=null){
                    return this.next.isContains(data);
                }else {
                    return false;
                }
            }
        }
        public void deleteNode(String data){
            if(data.equals(this.data)){
                if(this.next!=null){
                    root = this.next;
                }else {
                    root=null;
                }

            }else {
                if(this.next!=null){
                    if(this.next.isContains(data)){
                        this.next = this.next.next;
                    }
                }else {
                    return;
                }
            }
        }
        public String getNode(int index){
            if(Link.this.foot++ == index){
                return this.data;
            }else {
                return this.next.getNode(index);
            }
        }
        public void modiNode(int index,String data){
            if(index == Link.this.foot++){
                this.data = data;
            }else {
                this.next.modiNode(index,data);
            }
        }
    }
    //===========以上为内部类定义========//
    private Node root;
    private int count = 0;
    private int foot = 0;
    public void addNewNode(String data){
        Node newNode = new Node(data);
        if(this.root==null){
            this.root = newNode;
        }else {
            this.root.add(newNode);
        }
        count++;
    }
    public void printNode(){
        if (this.root!=null){
           this.root.print();
        }
    }
    public int getSize(){
        return this.count;
    }
    public boolean isEmpty(){
        return this.count == 0;
    }

    public boolean isContains(String data){
        if (data == null || this.root==null){
            return false;
        }else {
            return this.root.isContains(data);
        }
    }
    public void deleteNode(String data){
        if(this.root.isContains(data)){
            this.root.deleteNode(data);
        }else {
            return;
        }
    }
    public String getNode(int index){
        foot = 0;
        if(index>this.count){
            return null;
        }else {
            return this.root.getNode(index);
        }
    }
    public void modiNode(int index,String data){
        if (index>this.count){
            return;
        }else {
            this.root.modiNode(index,data);
        }
    }

}
public class LinkDemo2 {
    public static void main(String[] args) {
        Link link = new Link();
        link.addNewNode("A");
        link.addNewNode("B");
        link.addNewNode("C");
        link.addNewNode("B");
        link.addNewNode("C");
        link.printNode();
        System.out.println("**********");
        System.out.println("元素个数:"+link.getSize());
        System.out.println("**********");
        System.out.println("是否为空:"+link.isEmpty());
        System.out.println("**********");
        System.out.println("是否包含D:"+link.isContains("D"));
        System.out.println("**********");
        link.deleteNode("B");
        link.printNode();
        System.out.println("**********");
        link.modiNode(3,"D");
        System.out.println(link.getNode(3));
    }
}

输出结果:

A
B
C
B
C
**********
元素个数:5
**********
是否为空:false
**********
是否包含D:false
**********
A
C
B
C
**********
D

3、链表变为对象数组内容取出

class Link{
    class Node{
        private String data;
        private Node next;
        public Node (String data){
            this.data = data;
        }
        public void add(Node newNode){
            if(this.next==null){
                this.next = newNode;
            }else {
                this.next.add(newNode);
            }
        }
        public void print(){
            System.out.println(this.data);
            if (this.next!=null){
                this.next.print();
            }
        }
        public boolean isContains(String data){
            if(data.equals(this.data)){
                return true;
            }else {
                if(this.next!=null){
                    return this.next.isContains(data);
                }else {
                    return false;
                }
            }
        }
        public void deleteNode(String data){
            if(data.equals(this.data)){
                if(this.next!=null){
                    root = this.next;
                }else {
                    root=null;
                }

            }else {
                if(this.next!=null){
                    if(this.next.isContains(data)){
                        this.next = this.next.next;
                    }
                }else {
                    return;
                }
            }
        }
        public String getNode(int index){
            if(Link.this.foot++ == index){
                return this.data;
            }else {
                return this.next.getNode(index);
            }
        }
        public void modiNode(int index,String data){
            if(index == Link.this.foot++){
                this.data = data;
            }else {
                this.next.modiNode(index,data);
            }
        }
        //第一次调用(Link),this=Link.root
        //第二次调用(Node),this=Link.root.next

        public void  toArrayNode(){
            Link.this.retArray[Link.this.foot++] = this.data;
            if(this.next!=null){
                this.next.toArrayNode();
            }
        }
    }
    //===========以上为内部类定义========//
    private Node root;
    private int count = 0;
    private int foot = 0;
    private String [] retArray;    //返回的数组
    public void addNewNode(String data){
        Node newNode = new Node(data);
        if(this.root==null){
            this.root = newNode;
        }else {
            this.root.add(newNode);
        }
        count++;
    }
    public void printNode(){
        if (this.root!=null){
           this.root.print();
        }
    }
    public int getSize(){
        return this.count;
    }
    public boolean isEmpty(){
        return this.count == 0;
    }

    public boolean isContains(String data){
        if (data == null || this.root==null){
            return false;
        }else {
            return this.root.isContains(data);
        }
    }
    public void deleteNode(String data){
        if(this.root.isContains(data)){
            this.root.deleteNode(data);
        }else {
            return;
        }
    }
    public String getNode(int index){
        foot = 0;
        if(index>this.count){
            return null;
        }else {
            return this.root.getNode(index);
        }
    }
    public void modiNode(int index,String data){
        if (index>this.count){
            return;
        }else {
            this.root.modiNode(index,data);
        }
    }
    public String [] toArray(){
        if(this.root == null){
            return null;
        }
        this.foot = 0;  //脚标控制
        this.retArray = new String[this.count];   //根据保存内容开辟数组
        this.root.toArrayNode();
        return this.retArray;
    }
}
public class LinkDemo2 {
    public static void main(String[] args) {
        Link link = new Link();
        link.addNewNode("A");
        link.addNewNode("B");
        link.addNewNode("C");
        link.addNewNode("B");
        link.addNewNode("C");
        link.printNode();
        System.out.println("**********");
        System.out.println("元素个数:"+link.getSize());
        System.out.println("**********");
        System.out.println("是否为空:"+link.isEmpty());
        System.out.println("**********");
        System.out.println("是否包含D:"+link.isContains("D"));
        System.out.println("**********");
        //link.deleteNode("B");
        link.printNode();
        System.out.println("**********");
        link.modiNode(3,"D");
        System.out.println(link.getNode(3));
        System.out.println("**********");
        for (String str : link.toArray()){
            System.out.println(str);
        }
    }
}

运行结果:

A
B
C
B
C
**********
元素个数:5
**********
是否为空:false
**********
是否包含D:false
**********
A
B
C
B
C
**********
D
**********
A
B
C
D
C

4、在链表中保存对象

class Pet{
    private String name;
    private double price;
    public Pet(String name,double price){
        this.name = name;
        this.price = price;
    }
    public String getInfo(){
        return "name:"+this.name+",price:"+this.price;
    }
    public boolean compare(Pet pet){
        if(this == pet){
            return true;
        }
        if (pet == null){
            return false;
        }
        if (this.name.equals(pet.name) && this.price == pet.price){
            return true;
        }
        return false;
    }
}
class Link1{
    class Node{
        private Pet data;
        private Node next;
        public Node (Pet data){
            this.data = data;
        }
        public void add(Node newNode){
            if(this.next==null){
                this.next = newNode;
            }else {
                this.next.add(newNode);
            }
        }
        public void print(){
            System.out.println(this.data);
            if (this.next!=null){
                this.next.print();
            }
        }
        public boolean isContains(Pet data){
            if(data.compare(this.data)){
                return true;
            }else {
                if(this.next!=null){
                    return this.next.isContains(data);
                }else {
                    return false;
                }
            }
        }
        public void deleteNode(Pet data){
            if(data.compare(this.data)){
                if(this.next!=null){
                    root = this.next;
                }else {
                    root=null;
                }

            }else {
                if(this.next!=null){
                    if(this.next.isContains(data)){
                        this.next = this.next.next;
                    }
                }else {
                    return;
                }
            }
        }
        public Pet getNode(int index){
            if(Link1.this.foot++ == index){
                return this.data;
            }else {
                return this.next.getNode(index);
            }
        }
        public void modiNode(int index,Pet data){
            if(index == Link1.this.foot++){
                this.data = data;
            }else {
                this.next.modiNode(index,data);
            }
        }
        //第一次调用(Link),this=Link.root
        //第二次调用(Node),this=Link.root.next

        public void  toArrayNode(){
            Link1.this.retArray[Link1.this.foot++] = this.data;
            if(this.next!=null){
                this.next.toArrayNode();
            }
        }
    }
    //===========以上为内部类定义========//
    private Node root;
    private int count = 0;
    private int foot = 0;
    private Pet [] retArray;    //返回的数组
    public void addNewNode(Pet data){
        Node newNode = new Node(data);
        if(this.root==null){
            this.root = newNode;
        }else {
            this.root.add(newNode);
        }
        count++;
    }
    public void printNode(){
        if (this.root!=null){
            this.root.print();
        }
    }
    public int getSize(){
        return this.count;
    }
    public boolean isEmpty(){
        return this.count == 0;
    }

    public boolean isContains(Pet data){
        if (data == null || this.root==null){
            return false;
        }else {
            return this.root.isContains(data);
        }
    }
    public void deleteNode(Pet data){
        if(this.root.isContains(data)){
            count--;   //这一步是为了当删除元素之后,将链表变为对象数组输出,不会产生空指针报错。
            this.root.deleteNode(data);
        }else {
            return;
        }
    }
    public Pet getNode(int index){
        foot = 0;
        if(index>this.count){
            return null;
        }else {
            return this.root.getNode(index);
        }
    }
    public void modiNode(int index,Pet data){
        if (index>this.count){
            return;
        }else {
            this.root.modiNode(index,data);
        }
    }
    public Pet [] toArray(){
        if(this.root == null){
            return null;
        }
        this.foot = 0;  //脚标控制
        this.retArray = new Pet[this.count];   //根据保存内容开辟数组
        this.root.toArrayNode();
        return this.retArray;
    }
}
public class LinkDemo3 {
    public static void main(String[] args) {
        Link1 link1 = new Link1();
        link1.addNewNode(new Pet("cat",123));
        link1.addNewNode(new Pet("dog",456));
        link1.addNewNode(new Pet("pig",789));
        System.out.println("是否為空:"+link1.isEmpty());
        System.out.println("有幾個對象:"+link1.getSize());
        System.out.println("是否包含dog:"+link1.isContains(new Pet("dog",456)));
        link1.deleteNode(new Pet("dog",456));
        System.out.println("是否包含dog:"+link1.isContains(new Pet("dog",456)));
        link1.deleteNode(new Pet("d",456));
        Pet [] pets = link1.toArray();
        for(Pet pet : pets){
            System.out.println(pet.getInfo());
        }
    }
}

运行结果:

是否為空:false
有幾個對象:3
是否包含dog:true
是否包含dog:false
name:cat,price:123.0
name:pig,price:789.0

5、在关系中使用链表

 

class Province1{
    private int pid;
    private String pname;
    private Link2 cities = new Link2();

    public Link2 getCities(){
        return this.cities;
    }
    public Province1(int pid,String pname){
        this.pid=pid;
        this.pname=pname;
    }
    public String getInfo(){
        return "省份编号"+this.pid+"省份名称"+this.pname;
    }
    public boolean compare(Province1 province){
        if (this == province){
            return true;
        }
        if (province == null){
            return false;
        }
        if(this.pid == province.pid && this.pname.equals(province.pname)){
            return true;
        }
        return false;
    }

}
class City{
    private int cid;
    private String cname;
    private Province1 province;
    public void setProvince(Province1 province){
        this.province = province;
    }
    public Province1 getProvince(){
        return this.province;
    }
    public City(int cid,String cname){
        this.cid=cid;
        this.cname=cname;
    }
    public String getInfo(){
        return "城市编号:"+this.cid+",城市名称:"+this.cname;
    }
    public boolean compare(City city){
        if(this == city){
            return true;
        }
        if (city == null){
            return false;
        }
        if (this.cid==city.cid && this.cname.equals(city.cname)){
            return true;
        }
        return false;
    }
}
class Link2{
    class Node{
        private City data;
        private Node next;
        public Node (City city){
            this.data = city;
        }
        public void add(Node newNode){
            if(this.next==null){
                this.next = newNode;
            }else {
                this.next.add(newNode);
            }
        }
        public void print(){
            System.out.println(this.data);
            if (this.next!=null){
                this.next.print();
            }
        }
        public boolean isContains(City data){
            if(data.compare(this.data)){
                return true;
            }else {
                if(this.next!=null){
                    return this.next.isContains(data);
                }else {
                    return false;
                }
            }
        }
        public void deleteNode(City data){
            if(data.compare(this.data)){
                if(this.next!=null){
                    root = this.next;
                }else {
                    root=null;
                }

            }else {
                if(this.next!=null){
                    if(this.next.isContains(data)){
                        this.next = this.next.next;
                    }
                }else {
                    return;
                }
            }
        }
        public City getNode(int index){
            if(Link2.this.foot++ == index){
                return this.data;
            }else {
                return this.next.getNode(index);
            }
        }
        public void modiNode(int index,City data){
            if(index == Link2.this.foot++){
                this.data = data;
            }else {
                this.next.modiNode(index,data);
            }
        }
        //第一次调用(Link),this=Link.root
        //第二次调用(Node),this=Link.root.next

        public void  toArrayNode(){
            Link2.this.retArray[Link2.this.foot++] = this.data;
            if(this.next!=null){
                this.next.toArrayNode();
            }
        }
    }
    //===========以上为内部类定义========//
    private Node root;
    private int count = 0;
    private int foot = 0;
    private City [] retArray;    //返回的数组
    public void addNewNode(City data){
        Node newNode = new Node(data);
        if(this.root==null){
            this.root = newNode;
        }else {
            this.root.add(newNode);
        }
        count++;
    }
    public void printNode(){
        if (this.root!=null){
            this.root.print();
        }
    }
    public int getSize(){
        return this.count;
    }
    public boolean isEmpty(){
        return this.count == 0;
    }

    public boolean isContains(City data){
        if (data == null || this.root==null){
            return false;
        }else {
            return this.root.isContains(data);
        }
    }
    public void deleteNode(City data){
        if(this.root.isContains(data)){
            count--;
            this.root.deleteNode(data);
        }else {
            return;
        }
    }
    public City getNode(int index){
        foot = 0;
        if(index>this.count){
            return null;
        }else {
            return this.root.getNode(index);
        }
    }
    public void modiNode(int index,City data){
        if (index>this.count){
            return;
        }else {
            this.root.modiNode(index,data);
        }
    }
    public City [] toArray(){
        if(this.root == null){
            return null;
        }
        this.foot = 0;  //脚标控制
        this.retArray = new City[this.count];   //根据保存内容开辟数组
        this.root.toArrayNode();
        return this.retArray;
    }
}
public class LinkDemo4 {
    public static void main(String[] args) {
        //Link2 link1 = new Link2();
        //准备对象
        Province1 province1 = new Province1(1,"jiangsu");
        City city1 = new City(1,"yangzhou");
        City city2 = new City(2,"yancheng");
        City city3 = new City(3,"huaian");
        //设置关系
        city1.setProvince(province1);
        city2.setProvince(province1);
        city3.setProvince(province1);
        province1.getCities().addNewNode(city1);
        province1.getCities().addNewNode(city2);
        province1.getCities().addNewNode(city3);
        //第二步:取出关系
        System.out.println(province1.getInfo());
        System.out.println("包含的城市数量:"+province1.getCities().getSize());
        City [] city = province1.getCities().toArray();
        System.out.println("删除前的城市信息:");
        for (int x = 0 ; x<city.length;x++){
            System.out.println(city[x].getInfo());
        }
        System.out.println("是否包含yancheng:"+province1.getCities().isContains(new City(2,"yancheng")));
        System.out.println("删除yancheng:");
        province1.getCities().deleteNode(new City(2,"yancheng"));
        City [] c = province1.getCities().toArray();
        for (City x:c){
            System.out.println(x.getInfo());
        }
    }
}

运行结果:

省份编号1省份名称jiangsu
包含的城市数量:3
删除前的城市信息:
城市编号:1,城市名称:yangzhou
城市编号:2,城市名称:yancheng
城市编号:3,城市名称:huaian
是否包含yancheng:true
删除yancheng:
城市编号:1,城市名称:yangzhou
城市编号:3,城市名称:huaian

 

以上是关于java链表的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript笔试题(js高级代码片段)

NC41 最长无重复子数组/NC133链表的奇偶重排/NC116把数字翻译成字符串/NC135 股票交易的最大收益/NC126换钱的最少货币数/NC45实现二叉树先序,中序和后序遍历(递归)(代码片段

java代码在片段活动中不起作用

java 代码片段【JAVA】

# Java 常用代码片段

# Java 常用代码片段