Java实现链表结构

尾插法单项链表及遍历:
import java.util.Random;
public class mylinked {
    public static void main(String[] args) {
        mylinked mylinked=new mylinked();
      mylinked.print(mylinked.creat(6));
    }

 //        分别存储下一个节点地址和数据
    public mylinked next;
    public int data;
    public mylinked header,tailer,newcode;
    public  mylinked creat (int len) {
        Random random=new Random();
//        定义头尾和新节点,且初值为空
        mylinked header,tailer,newcode;
        header=tailer=newcode=null;
        for (int i = 0; i <len; i++) {
//            初始化新节点
            newcode=new mylinked();
//            赋值
            newcode.data=random.nextInt(100);
            if(header==null){
                header=tailer=newcode;
            }else{
                tailer.next=newcode;
                tailer=newcode;
            }

        }
    return header;
    }

    public void print(mylinked header){
        while(header!=null){
            System.out.println(header.data);
            header=header.next;
        }


    }

}

 头插法单向链表结构:

import java.util.Random;
public class mylink {

    //创建链表类
    class MLink {
        //当Mlink实例化后形成一个链表的节点对象
        //节点对象中的属性
        //节点存储的属性
        private int data;
        //存储下一个链表节点地址的属性
        private MLink next;
        /**
         * 功能:创建一个链表,并返回头节点的地址
         *
         * @param len 创建链表数据的长度
         * @return 返回头节点的地址
         */
        public MLink create(int len) {
            MLink header = null;
            //创建随机对象
            Random ra = new Random();
            //生成指定长度的随机数
            for (int i = 0; i < len; i++) {
                //创建随机数
                int num = ra.nextInt(100);
                //创建一个节点对象
                MLink temp = new MLink();
                //存储数据
                temp.data = num;
                //是否第一次创建链表节点
                if (header == null)
                    header = temp;
                else {
                    temp.next = header;
                    header = temp;
                }
            }
            return header;
        }
    }
}

双向链表及遍历:

public class mylinked {
    public static void main(String[] args) {
        mylinked mylinked=new mylinked();
        mylinked.print(mylinked.creat(6));
    }

//        分别存储下一个节点地址和数据
    public mylinked next;
    private mylinked last;
    public int data;
    public mylinked header,tailer,newcode;
    public  mylinked creat (int len) {
        Random random=new Random();
//        定义头尾和新节点,且初值为空
        mylinked header,tailer,newcode;
        header=tailer=newcode=null;
        for (int i = 0; i <len; i++) {
//            初始化新节点
            newcode=new mylinked();
//            赋值
            newcode.data=random.nextInt(100);
            if(header==null){
                header=tailer=newcode;
            }else{
//                新节点的上一个等于尾节点,尾节点的下一个值等于新节点
                newcode.last=tailer;
                tailer.next=newcode;
                tailer=newcode;
            }
        }
    return header;
    }
    public void print(mylinked header){
        while(header!=null){
            System.out.println(header.data);
            header=header.next;
        }


    }

}

 

评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一条coding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值