数据结构--链表

单向链表

HeroNode节点类

package 链表;

public class HeroNode {
    public int no;
    public String name;
    public String nickname;
    public HeroNode next;//指向下一个节点

    public HeroNode(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}

链表类

package 链表;

//单向链表管理节点
public class SingleLinkedList {

    //定义一个头节点
    private HeroNode head=new HeroNode(0,"","");

    //添加节点到单向链表最后
    public void add(HeroNode heroNode){

        HeroNode temp=head;
        //定义一个临时变量找到链表的最后位置
        while(temp.next!=null){
            temp=temp.next;
        }
        //退出循环时即找到了最后的位置
        temp.next=heroNode;
    }

    //添加节点到链表指定编号位置
    public void addByOrder(HeroNode heroNode){
        HeroNode temp=head;
        //boolean flag=true;
        while(true){
            //temp是最后一个位置,不能插入
            if(temp.next==null){
                break;
            }else if(temp.next.no>heroNode.no){
                heroNode.next=temp.next ;
                temp.next=heroNode;
                break;
            } else if (temp.next.no==heroNode.no){
                System.out.println("插入的编号"+heroNode.no+"已经存在,插入不了!");
                break;
            }
            temp=temp.next;
        }
    }

    //修改节点
    public void update(HeroNode newHeroNode){
         //首先判断链表为空
        if(head.next==null){
            System.out.println("链表为空");
            return;
        }

        //根据编号找到指定节点
        HeroNode temp=head.next;
        while (true){
            if(temp==null){
                //System.out.println("没有找到节点");
                break;
            }
            if(temp.no==newHeroNode.no){
                temp.name=newHeroNode.name;
                temp.nickname=newHeroNode.nickname;
                break;
            }
            temp=temp.next;
        }
    }

    //删除节点
    public void delete(int no){
        HeroNode temp=head;
        while (true){
            if(temp.next==null){
                break;
            }
            if(temp.next.no==no){
                temp.next=temp.next.next;
                break;
            }
            temp=temp.next;
        }
    }

    //显示链表(遍历)
    public void show(){
        //首先判断链表为空
        if(head.next==null){
            System.out.println("链表为空");
            return;
        }
        HeroNode temp=head.next;
        while (true){
            if(temp==null){
                break;
            }
            System.out.println(temp);
            temp=temp.next;
        }

    }

}

测试类

package 链表;

public class SingleListDemo {
    public static void main(String[] args) {
        HeroNode hero1=new HeroNode(1,"宋江","及时雨");
        HeroNode hero2=new HeroNode(2,"卢俊义","玉麒麟");
        HeroNode hero3=new HeroNode(3,"吴用","智多星");
        HeroNode hero4=new HeroNode(4,"林冲","豹子头");

        SingleLinkedList singleLinkedList=new SingleLinkedList();
        singleLinkedList.add(hero1);
        singleLinkedList.add(hero2);
        singleLinkedList.add(hero3);
        singleLinkedList.add(hero4);
        singleLinkedList.show();

        HeroNode newheronode=new HeroNode(4,"豹子呀","是你的豹子呀~");
        singleLinkedList.update(newheronode);
        System.out.println("修改信息---------------------");
        singleLinkedList.show();


        //插入新节点到最后
        HeroNode newheronode1=new HeroNode(5,"武松","是你的老虎呀~");
        singleLinkedList.add(newheronode1);
        System.out.println("插入新节点到最后---------------------");
        singleLinkedList.show();

        //插入新节点到指定位置
        System.out.println("插入新节点到指定位置---------------------");
        HeroNode newheronode2=new HeroNode(3,"浪青","是你的燕子呀~");
        singleLinkedList.addByOrder(newheronode2);
        singleLinkedList.show();

        //删除节点
        System.out.println("删除节点---------------------");
        singleLinkedList.delete(3);
        singleLinkedList.show();

        //插入新节点到指定位置
        System.out.println("插入新节点到指定位置---------------------");
        //HeroNode newheronode2=new HeroNode(3,"浪青","是你的燕子呀~");
        singleLinkedList.addByOrder(newheronode2);
        singleLinkedList.show();
    }
}

求单向链表中有效节点的个数

//获取节点个数
    public int getLength(){
        if(head.next==null){
            return 0;
        }
        int sum=0;
        HeroNode temp=head.next;
        while (temp!=null){
            sum++;
            temp=temp.next;
        }
        return sum;
    }

查找单链表的倒数第K个节点

 //查找单链表的倒数第K个节点,就是正数第(getLength-k)个
    public HeroNode numKnode(int k){

        if(head.next==null){
            return null;
        }
        int size=getLength();
        HeroNode temp=head.next;
       if (k<=0 || k>=size){
           return null;
       }
        for (int i = 0; i < (size-k); i++) {
            temp=temp.next;
        }
        return temp;
    }

链表反转

//链表反转
    public void reverseList(HeroNode head){
        //如果链表为空或者只有一个元素节点,无需反转直接返回
        if(head.next==null || head.next.next==null){
            return;
        }

        HeroNode cur=head.next;//当前节点
        HeroNode next=null;//保存当前节点的下一个节点

        HeroNode reverseHead=new HeroNode(0,"","");

        //遍历链表,每遍历一个节点就取出放在新链表reverseHead的最前端
        while(cur!=null){
            next=cur.next;
            cur.next=reverseHead.next;//此时reverseHead.next=null;
            reverseHead.next=cur;
            cur=next;
        }
        head.next=reverseHead.next;
    }

在这里插入图片描述

逆序打印链表

使用栈。

//逆序打印
    public void reversePrint(HeroNode head){
        while(head.next==null){
            return;
        }
        Stack<HeroNode> stack=new Stack<HeroNode>();
        HeroNode cur=head.next;
        while(cur!=null){
            stack.push(cur);
            cur=cur.next;
        }
        while(stack.size()>0){
            System.out.println(stack.pop());
        }
    }

单向环形链表

使用链表实现单环形链表

创建节点

package 链表;

public class Boy {
    private int no;
    private Boy next;

    public Boy(int no) {
        this.no = no;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public Boy getNext() {
        return next;
    }

    public void setNext(Boy next) {
        this.next = next;
    }
}

创建单向环形链表

package 链表;

public class CircleSingleLinkedList {
    //当前节点
    private Boy first=null;

    //添加节点
    public void addBoy(int nums){
        if(nums<1){
            System.out.println("nums的值不正确");
            return;
        }
        //辅助指针
        Boy curBoy=null;

        for(int i=1;i<=nums;i++){
            Boy boy=new Boy(i);
            if(i==1){
                first=boy;
                first.setNext(first);
                curBoy=boy;
            }else{
                curBoy.setNext(boy);
                boy.setNext(first);
                curBoy=boy;
            }
        }
    }

    //遍历
    public void show(){
        if(first==null){
            System.out.println("无小孩节点");
            return;
        }
        Boy curBoy=first;
        while (true){
            System.out.println(curBoy.getNo());
            if(curBoy.getNext()==first){
                break;
            }
            curBoy=curBoy.getNext();
        }
    }

}

双向链表

定义节点类

package 链表;

public class HeroNode2 {
    public int no;
    public String name;
    public String nickname;
    public HeroNode2 next;
    public HeroNode2 pre;

    public HeroNode2(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode2{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}

定义双向链表类

package 链表;

public class DoubleLinkedList {

    //初始化一个头节点
    private HeroNode2 head=new HeroNode2(0,"","");

    //返回头节点
    public HeroNode2 getHead(){
        return head;
    }

    //遍历
    public void show(){
        if(head.next==null){
            System.out.println("双向链表为空!");
            return;
        }
        HeroNode2 temp=head.next;
        while(temp!=null){
            System.out.println(temp);
            temp=temp.next;
        }

    }

    //添加节点,默认加在最后
    public void add(HeroNode2 heronode){
        HeroNode2 temp=head;
        while (temp.next!=null){
            temp=temp.next;
        }
        temp.next=heronode;
        heronode.pre=temp;
    }

    //添加节点到指定位置
    public void addByOrder(HeroNode2 heroNode){
        HeroNode2 temp=head;
        //boolean flag=true;
        while(true){
            //temp是最后一个位置,不能插入
            if(temp.next==null){
                break;
            }else if(temp.next.no>heroNode.no){
                heroNode.next=temp.next ;
                temp.next.pre=heroNode;
                temp.next=heroNode;
                heroNode.pre=temp;
                break;
            } else if (temp.next.no==heroNode.no){
                System.out.println("插入的编号"+heroNode.no+"已经存在,插入不了!");
                break;
            }
            temp=temp.next;
        }
    }

    //修改节点
    public void update(HeroNode2 newHeroNode){
        //首先判断链表为空
        if(head.next==null){
            System.out.println("链表为空");
            return;
        }

        //根据编号找到指定节点
        HeroNode2 temp=head.next;
        while (true){
            if(temp==null){
                //System.out.println("没有找到节点");
                break;
            }
            if(temp.no==newHeroNode.no){
                temp.name=newHeroNode.name;
                temp.nickname=newHeroNode.nickname;
                break;
            }
            temp=temp.next;
        }
    }

    //删除节点
    public void delete(int no){
        //指针
        HeroNode2 temp=head.next;
        while (true){
            if(temp==null){
                break;
            }
            if(temp.no==no){
                //如果是最后一个节点
                if(temp.next==null){
                    temp.pre.next=null;
                }
                //不是最后一个节点
                temp.pre.next=temp.next;
                temp.next.pre=temp.pre;
                break;
            }
            temp=temp.next;
        }
    }

}

测试

package 链表;

public class DoubleListDemo {
    public static void main(String[] args) {
        HeroNode2 hero1=new HeroNode2(1,"宋江","及时雨");
        HeroNode2 hero2=new HeroNode2(2,"卢俊义","玉麒麟");
        HeroNode2 hero3=new HeroNode2(3,"吴用","智多星");
        HeroNode2 hero4=new HeroNode2(4,"林冲","豹子头");

        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();

        doubleLinkedList.add(hero1);
        doubleLinkedList.add(hero2);
        doubleLinkedList.add(hero3);
        doubleLinkedList.add(hero4);

        //遍历
        System.out.println("遍历");
        doubleLinkedList.show();
        System.out.println("======================");
        HeroNode2 newheronode=new HeroNode2(4,"豹子呀","是你的豹子呀~");
        doubleLinkedList.update(newheronode);
        System.out.println("修改信息---------------------");
        doubleLinkedList.show();


        //插入新节点到最后
        HeroNode2 newheronode1=new HeroNode2(5,"武松","是你的老虎呀~");
        doubleLinkedList.add(newheronode1);
        System.out.println("插入新节点到最后---------------------");
        doubleLinkedList.show();

        //删除节点
        System.out.println("删除节点---------------------");
        doubleLinkedList.delete(3);
        doubleLinkedList.show();

        //插入新节点到指定位置
        System.out.println("插入新节点到指定位置---------------------");
        HeroNode2 newheronode3=new HeroNode2(3,"浪青","是你的燕子呀~");
        doubleLinkedList.addByOrder(newheronode3);
        doubleLinkedList.show();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值