首页 > 其他分享 >双向链表

双向链表

时间:2022-10-29 17:22:54浏览次数:49  
标签:hero temp next 链表 doubleLikedlist num 双向 public

双向链表

package com.doubleLikedlist;

import com.doubleLikedlist.hero;

public class doubleLikedlist {
    private hero headnode=new hero(001,"");
    public void add(hero node)
    {
        hero temp=headnode;
        while(true)
        {
            if(temp.next==null)
            {
                break;
            }
            temp=temp.next;
        }
        temp.next=node;
        node.pre=temp;
    }
    public void show()
    {
        hero temp=headnode.next;
        if(temp==null)
        {
            System.out.println("链表为空");
            return;
        }
        while(true)
        {
            if(temp==null)
            {
                System.out.println("链表遍历完");
                break;
            }
            System.out.println(temp);
            temp=temp.next;
        }
    }
    public void update(hero updatenode)
    {
        boolean flag=false;
        hero tmep=headnode.next;
        if(headnode.next==null)
        {
            System.out.println("链表为空");
            return;
        }
        while(true)
        {
            if(tmep==null)
            {
                break;
            }
            else if(tmep.num== updatenode.num)
            {
                flag=true;
                break;
            }
            tmep=tmep.next;
        }
        if(flag)
        {
            tmep.name= updatenode.name;
        }
        else
        {
            System.out.println("没有找到");
        }
    }
    public void delete(int num)
    {
        boolean flag=false;
        hero temp=headnode.next;
        if(headnode.next==null)
        {
            return;
        }
        while(true)
        {
            if(temp==null)
            {
                System.out.println("没有找到");
                break;
            }
            else if (temp.num==num) {
                flag=true;
                break;
            }
            temp=temp.next;
        }
        if(flag)
        {
            temp.pre.next=temp.next;
            if(temp.next!=null)
            {
                temp.next.pre=temp.pre;
            }

        }
        else
        {
            System.out.println("没有找到节点");
        }

    }
    public void byorder(hero node)
    {
        boolean flag=false;
        hero temp=headnode;
        while(true)
        {
            if(temp.next==null)
            {
                break;
            } else if (temp.next.num>node.num) {
                break;
            }
            else if(temp.next.num== node.num)
            {
                flag=true;
            }
            temp=temp.next;
        }
        if(flag)
        {
            System.out.println("重复");
        }
        else
        {
            if(temp.next!=null)
            {
                temp.next.pre=node;
            }
            node.pre=temp;
            node.next=temp.next;
            temp.next=node;


        }
    }
}

测试

package com.doubleLikedlist;
import com.doubleLikedlist.hero;

public class test {
    public static void main(String[] args) {
        doubleLikedlist doubleLikedlist = new doubleLikedlist();
        hero hero1 = new hero(001,"宋江");
        hero hero2 = new hero(002,"李逵");
        hero hero3 = new hero(003,"张飞");
        hero hero5 = new hero(005,"赵云");
        hero hero6 = new hero(005,"张三");
        doubleLikedlist.byorder(hero1);
        doubleLikedlist.byorder(hero3);
        doubleLikedlist.byorder(hero5);
        doubleLikedlist.byorder(hero2);
        doubleLikedlist.byorder(hero6);
        doubleLikedlist.show();
    }
}

结构体

package com.doubleLikedlist;

class hero {
    public int num;
    public String name;
    public hero next;
    public hero pre;
    public hero(int num, String name) {
        this.num = num;
        this.name = name;
    }
    @Override
    public String toString() {
        return "com.singleLikedlist.hero{" +
                "num=" + num +
                ", name='" + name + '\'' +
                '}';
    }
}

标签:hero,temp,next,链表,doubleLikedlist,num,双向,public
From: https://www.cnblogs.com/jinnice/p/16839172.html

相关文章