线性表之单链表

基于Java实现单链表的demo

package com.test.list;

import java.util.Iterator;

//基于Java实现单链表的demo
public class LinkedListDemo<T> implements Iterable {
    //头节点
    private Node head;
    //链表的长度
    private int L;

    //构造方法,初始化链表
    public LinkedListDemo() {
        head = new Node(null, null);
        L = 0;
    }

    //清空链表
    public void clear() {
        this.head.next = null;
        this.head.item = null;
        L = 0;
    }

    //获取链表长度
    public int getLength() {
        return L;
    }

    //判断链表是否为空
    public boolean isEmpty() {
        return L == 0;
    }

    //获取指定位置的元素值
    public T get(int i){
        Node n = head.next;
        for (int j = 0; j < i; j++) {
            n = n.next;
        }
        return n.item;
    }

    //向链表添加元素,默认添加到最后的位置
    public void insert(T t){
        Node n = head;
        while(n.next!=null){
            n = n.next;
        }
        Node newNode = new Node(t, null);
        n.next = newNode;
        L++;
    }

    //向链表指定位置添加元素
    public void insert(T t,int i){
        if(i<0 || i>= L){
            throw new RuntimeException("位置不合法!!!");
        }
        Node n = head;
        for (int j = 0; j <= i-1; j++) {
            n = n.next;
        }
        Node pre = n;
        Node current = n.next;
        Node newNode = new Node(t, current);
        pre.next = newNode;
        L++;
    }

    //删除指定位置的元素,并返回
    public T remove(int i){
        if(i<0 || i>= L){
            throw new RuntimeException("位置不合法!!!");
        }
        Node n = head;
        for (int j = 0; j <= i-1; j++) {
            n = n.next;
        }
        Node pre = n;
        Node current = n.next;
        Node currentNext = n.next.next;
        pre.next = currentNext;
        current.next = null;
        L--;
        return current.item;
    }

    //根据目标值,获取链表的第一次出现的下标
    public int indexOfTarget(T t){
        if(t == null){
            throw new RuntimeException("参数不合法!");
        }
        Node n = head.next;
        for (int i = 0; i < L-1; i++) {
            if(n.item.equals(t)){
                return i;
            }
            n = n.next;
        }
        return -1;
    }

    //打印链表
    public void printData(){
        Node n = head.next;
        for (int i = 0; i < L; i++) {
            System.out.print("[" + i + "]=" + n.item + " ");
            n = n.next;
        }
        System.out.println();
    }

    //节点类,用内部类演示
    private class Node {
        //节点存储的数据
        T item;
        //节点存储的下一个节点的地址
        Node next;

        //构造方法
        public Node(T item, Node next) {
            this.item = item;
            this.next = next;
        }
    }

    @Override
    public Iterator iterator() {
        return new LIterator();
    }

    private class LIterator implements Iterator{
        private Node n;

        public LIterator(){
            this.n = head;
        }

        @Override
        public boolean hasNext() {
            return n.next!=null;
        }

        @Override
        public T next() {
            n = n.next;
            return n.item;
        }
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值