目录

LinkedList和单双链表。

java中提供了双向链表的动态数据结构 --- LinkedList,它同时也实现了List接口,可以当作普通的列表来使用。也可以自定义实现链表。

单向链表:一个节点=本节点数据+下个节点地址

给定两个有序链表的头指针head1和head2,打印两个链表的公共部分。

java 复制代码
public class Lee {
    private Node head1;
    private Node head2;
    Lee(){
        this.head1= new Node(0);
        this.head2= new Node(0);
    }

    public void  insert1(int data){
        Node newNode = new Node(data);
        Node curNode = head1;
        while (curNode.next!=null){
            curNode=curNode.next;
        }
        curNode.next=newNode;
    }

    public void  insert2(int data){
        Node newNode = new Node(data);
        Node curNode = head2;
        while (curNode.next!=null){
            curNode=curNode.next;
        }
        curNode.next=newNode;
    }

     static class Node{
        public int value;
        public Node next;
         Node(int data){
            this.value=data;
            this.next=null;
        }
    }

    public void Plink(Node head1,Node head2){
        while (head1!=null&&head2!=null){
            if (head1.value<head2.value)
                head1=head1.next;
            else if (head1.value>head2.value)
                head2=head2.next;
            else{
                System.out.println(head1.value+" ");
                head1=head1.next;
                head2=head2.next;
            }

        }
    }

    public static void main(String[] args) {
        Lee lee = new Lee();
        lee.insert1(1);
        lee.insert1(3);
        lee.insert1(4);

        lee.insert2(4);
        lee.insert2(5);
        lee.insert2(6);
        lee.Plink(lee.head1, lee.head2);
    }
}

双向链表:一个节点=上个节点地址+本节点数据+下个节点地址

如:定义两个函数,实现在双向链表的头部及尾部插入节点

java 复制代码
public class Lee {
    private Node head;
    Lee(){
        this.head= new Node(0);
    }

    public void  insertHead(int data){
        Node newNode = new Node(data);
        newNode.next=head;
        head.pre=newNode;
        head=newNode;
    }

    public void  insertTail(int data){
        Node newNode = new Node(data);
        Node current = head;
        while (current.next!=null){
            current=current.next;
        }
        current.next=newNode;
        newNode.pre=current;
    }

    public void printList(Node head) {
        Node current = head;

        // 从头到尾打印链表
        while (current != null) {
            System.out.print(current.value + " -> ");
            current = current.next;
        }
        System.out.println("null"); // 表示链表结尾
    }


    static class Node{
        public int value;
        public Node pre;
        public Node next;
         Node(int data){
            this.value=data;
            this.pre=null;
            this.next=null;
        }
    }

    public static void main(String[] args) {
        Lee lee = new Lee();
        lee.insertTail(2);
        lee.insertTail(3);
        lee.insertTail(4);
        lee.insertHead(4);

        lee.printList(lee.head);
    }
}
本文是转载文章,点击查看原文
如有侵权,请联系 xyy@jishuzhan.net 删除
相关推荐
阿巴~阿巴~23 分钟前
二分 —— 基本算法刷题路程
数据结构·c++·算法
maomi_952633 分钟前
《线性表、顺序表与链表》教案(C语言版本)
c语言·数据结构·链表
Run_Teenage43 分钟前
C语言 数据结构 【队列】动态模拟实现
数据结构
蒙奇D索大1 小时前
【数据结构】邻接矩阵完全指南:原理、实现与稠密图优化技巧
c语言·数据结构·考研·图论·改行学it
半桔1 小时前
哈希表(开散列)的实现
数据结构·c++·面试·散列表·哈希
牛奶咖啡.8543 小时前
树和图论(详细整理,简单易懂!)
数据结构·c++·算法·深度优先·图论
2401_881244404 小时前
416. 分割等和子集
数据结构·算法
butterfly_onfly4 小时前
C# 多线程并发编程基础
数据结构·算法·c#
python_chai7 小时前
Python核心数据结构详解:元组、集合与字典
java·数据结构·python
爱编程的王小美7 小时前
8876行详解数据结构,从入门到入坟
数据结构