Java实现单双链表的基本操作

Posted yuwenS.

tags:

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

链表

链表是一种物理存储上不连续,但是逻辑上是连续有序的存储结构。链表由一系列结点组成(链表每一个元素成为节点)组成,节点可以动态生成。单链表 的节点由两部分组成,一个是存储数据的数据域,一个是存储下一个节点的地址的指针域。双链表 的节点由三部分组成,比单链表多了一个存储上一个节点地址的指针域。

图解链表

单链表

双链表

代码实现

单链表

节点类的实现

//节点类
public class GoodsNode 
    public int id;
    public String name;
    public double price;
    public GoodsNode next;  //指向下一节点

    public GoodsNode(int id, String name, double price) 
        this.id = id;
        this.name = name;
        this.price = price;
    
    @Override
    public String toString() 
        return "GoodsNode" +
                "id=" + id +
                ", name='" + name + '\\'' +
                ", price=" + price +
                '';
    

基本操作的实现

public class DLinkedList 
    //头节点
    private  GoodsNode node = new GoodsNode(0,"",0.0);
    
    //往链表里添加节点
    public void add(GoodsNode goodsNode)
        GoodsNode temp = node; //定义一个临时节点等于头节点
        while (true) //循环查找节点
            if (temp.next == null)
                break;  //没有下一个节点时跳出循环
            
            temp = temp.next; //将下一个节点的赋值给临时节点
        
        temp.next = goodsNode; //添加节点
    

    //按照id值插入节点,id从小到大添加
    public void insert(GoodsNode goodsNode)
        GoodsNode temp = node;
        boolean flag = false;
        while (true)
            if (temp.next == null)
                break;
            
            if (temp.next.id > goodsNode.id)
                break;
            else if (temp.next.id == goodsNode.id)
                flag = true;
                break;
            
            temp = temp.next;
        
        if (flag)
            System.out.println("不能添加重复元素");
        else 
            goodsNode.next = temp.next;
            temp.next = goodsNode;
        
    

    //修改节点
    public void update(GoodsNode goodsNode)
         // 如果链表为空
        if (node.next == null)
            System.out.println("链表为空");
            return;
        
        GoodsNode temp = node.next;
        boolean flag = false;
        while (true)
            if (temp == null)
                break;
            
            if (temp.id == goodsNode.id)
                flag = true;
                break;
            
            temp = temp.next;
        
        if (flag)
            temp.name = goodsNode.name;
            temp.id = goodsNode.id;
        else 
            System.out.println("没有找到目标节点");
        
    

    //删除节点
    public void delete(int id)
        GoodsNode temp = node;
        boolean flag = false;
        while (true)
            if (temp.next == null)
                break;
            
            if (temp.next.id == id)
                flag = true;
                break;
            
            temp = temp.next;
        
        if (flag)
            temp.next = temp.next.next;
        else 
            System.out.println("未找到要删除的节点");
        
    

    //查看链表中每一个节点元素
    public void list()
        if (node.next == null)
            System.out.println("空链表");
            return;
        
        GoodsNode temp = node.next;
        while (true)
            if (temp == null)
                break;
            
            System.out.println(temp);
            temp = temp.next;
        
    

    //统计节点个数,不算头节点
    public int getLength()
        if (node.next == null)
            System.out.println("空链表");
            return 0;
        
        GoodsNode temp = node.next;
        int length = 0;
        while (temp != null)
            length++;
            temp = temp.next;
        
        return length;
    

基本操作的测试

public static void main(String[] args) 
        DLinkedList linkedList = new DLinkedList();
        System.out.println("==================顺序添加=====================");
        GoodsNode goodsNode = new GoodsNode(1,"耐克",599.0);
        GoodsNode goodsNode1 = new GoodsNode(2,"李宁",699.0);
        GoodsNode goodsNode2 = new GoodsNode(3,"乔丹",499.0);
        GoodsNode goodsNode3 = new GoodsNode(4,"阿迪",599.0);
        linkedList.add(goodsNode);
        linkedList.add(goodsNode1);
        linkedList.add(goodsNode3);
        linkedList.list();
        System.out.println("===================随机插入==========================");
        linkedList.insert(goodsNode2);
        linkedList.insert(goodsNode1); //插入相同的节点
        linkedList.update(new GoodsNode(1,"xiao",299.9)); //修改节点
        linkedList.delete(1); //删除节点
        linkedList.list();
    

运行结果

双链表

节点类的实现

//节点类
public class BookNode 
    public int id;
    public String name;
    public double price;
    public BookNode next; //指向下一个节点
    public BookNode pre; //指向上一个节点
    public BookNode(int id, String name, double price) 
        this.id = id;
        this.name = name;
        this.price = price;
    
    @Override
    public String toString() 
        return "BookNode" +
                "id=" + id +
                ", name='" + name + '\\'' +
                ", price=" + price +
                '';
    

基本操作的实现

public class SLinkedList 
    //头节点
    private BookNode head = new BookNode(0,"",0.0);
    
    //添加节点 尾部添加
    public void addList(BookNode bookNode)
        BookNode temp = head;
        while (true)
            //链表为空
            if (temp.next == null)
                break;
            
            temp = temp.next;
        
        temp.next = bookNode;
        bookNode.pre = temp;
    

    //修改节点
    public void update(BookNode bookNode)
        //是否是空链表
        if (head.next == null)
            System.out.println("空链表");
            return;
        
        BookNode temp = head.next;
        boolean flag = false;
        while (true)
            if (temp == null)
                break;
            
            if (temp.id == bookNode.id)
                flag = true;
                break;
            
            temp = temp.next;
        
        if (flag)
            temp.name = bookNode.name;
            temp.price = bookNode.price;
        else 
            System.out.println("未找点节点");
        
    

    //删除节点
    public void delNode(int id)
        BookNode temp = head;
        boolean flag = false;
        while (true)
            if (temp.next == null)
                break;
            
            if (temp.next.id == id)
                flag = true;
                break;
            
            temp = temp.next;
        
        if (flag)
            temp.next = temp.next.next;
            if (temp.next.next != null)
                temp.next.next.pre = temp;
            
        else 
            System.out.println("未找到节点");
        
    

    //插入节点
    public void insert(BookNode bookNode)
        BookNode temp = head;
        boolean flag = false;
        while (true)
            if (temp.next == null)
                break;
            
            if (temp.next.id > bookNode.id)
                break;
            else if (temp.next.id == bookNode.id)
                flag = true;
                break;
            
            temp = temp.next;
        
        if (flag)
            System.out.println("不能插入重复元素");
        else 
            bookNode.next = temp.next;
            temp.next = bookNode;
            bookNode.pre = temp;
            temp.next.pre = bookNode;
        
    

    //查看所以节点值
    public void list()
        if (head.next == null)
            System.out.println("空链表");
            return;
        
        BookNode temp = head.next;
        while (temp != null)
            System.out.println(temp);
            temp = temp.next;
        
    

基本操作的测试

public static void main(String[] args) 
        BookNode bookNode = new BookNode(1,"c语言",39.9);
        BookNode bookNode2 = new BookNode(2,"java",49.9);
        BookNode bookNode3 = new BookNode(3,"c++",59.9);
        BookNode bookNode4 = new BookNode(4,"linux",49.9);
        SLinkedList linkedList = new SLinkedList();
        System.out.println("==================顺序添加=====================");
        linkedList.addList(bookNode);
        linkedList.addList(bookNode2);
        linkedList.addList(bookNode4);
        linkedList.list();
        System.out.println("===================随机插入==========================");
        linkedList.insert(bookNode3);
        linkedList.insert(bookNode2);  //添加重复节点
        linkedList.delNode(1); //删除节点
        linkedList.update(new BookNode(2,"java进阶",69.9));
        linkedList.list();
    

运行结果

以上是关于Java实现单双链表的基本操作的主要内容,如果未能解决你的问题,请参考以下文章

数据结构[双链表的实现,以及双链表和单链表之间的比较,链表和顺序表的优劣]

JAVA 链表操作:单连表和双链表

单链接表的实现之从尾部插入节点

双链表的结构和插入节点

深度解析数组单链表和双链表

有序的双链表的实现