从今天起开始学习算法---环形链表

体会:环形链表在只有一个头结点的时候也会成环,尾插需要注意将需要插入的结点的next设置为头结点以保证成环。

1.链表不像环形队列一样有固定长度以及栈顶与栈底,所以没有取模这样容易绕混的操作。
2.对于判空,抓住头结点独自成环的特点即可。
3.尾插注意使最后的结点的next指向头结点。
4.环形列表的优点在于指定从某个结点开始遍历都可以走完全链。

代码实现记录:

package com.cclx.controller;

class  Node{
    public int data;

    public Node next;

    public Node(int data){
        this.data = data;
    }
}

public class LinkedListDemo {

    //头结点
    private Node head;

    /*
    创建链表 独自成环
     */
    public void createList(){
        this.head = new Node(0);
        this.head.next = this.head;
    }

    /*
    判断是否为空
     */
    public boolean isEmpty(){

        if(this.head.next == this.head){
            return true;
        }
        return false;
    }

    /*
    插入结点
     */
    public void insertNode(Node newNode){

        //辅助指针
        Node temp = this.head;

        while(temp.next !=this.head){
            temp = temp.next;
        }

        newNode.next = this.head;
        temp.next = newNode;
    }

    /*
    根据目标数据删除结点
     */
    public void deleteNode(int targetData){

        if(isEmpty()){
            System.out.println("已无结点可删除");
            return;
        }

        boolean finded = false;

        //辅助指针
        Node temp = this.head;

        while(temp.next != this.head){

            if(temp.next.data == targetData){
                finded = true;
                break;
            }
            temp = temp.next;
        }

        if(finded){
            temp.next = temp.next.next;
        }
    }

    /*
    从指定结点处开始查找目标结点是否存在
     */
    public void findTargetNode(Node orgin,int target){

        if(isEmpty()){
            System.out.println("链表为空");
            return;
        }

        Node temp = orgin;

        while (temp.next != orgin){

            temp = temp.next;
            System.out.println("目标:  "+target +", 本节点为 "+temp.data);

            if(temp.data == target){
                System.out.println("找到了!");
                return;
            }
        }

        System.out.println("未找到目标结点");

    }

    /*
    遍历list
     */
    public void foreachList(){
        Node node = this.head;

        if(isEmpty()){
            System.out.println("链表为空!");
            return;
        }

        while (node.next!=this.head){
            node = node.next;
            System.out.print("  "+node.data+"   ");
        }

        System.out.println("");

    }

    public static void main(String[] args) {

        //获取链表
        LinkedListDemo list = new LinkedListDemo();

        //创建链表
        list.createList();

        //遍历
        list.foreachList();
        //插入 1 2 3
        list.insertNode(new Node(1));

        list.insertNode(new Node(2));

        list.insertNode(new Node(3));

        //遍历
        list.foreachList();

        //删除2
        list.deleteNode(2);
        list.foreachList();

        //最后插入的结点的下一个结点还是头结点
        System.out.println(list.head);
        System.out.println(list.head.next.next.next);

        list.insertNode(new Node(2));
        list.foreachList();

        //目前为 1 3 2 我们试试从2这个结点开始找到1
        list.findTargetNode(list.head.next.next.next,1);

        list.findTargetNode(list.head.next.next.next,5);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值