线性表(四)——链表——双向链表

概念

双向链表也叫双向表,是链表的一种,它由多个结点组成,每个节点都由一个数据域和两个指针域组成,数据域用来存储数据,其中一个指针域用来指向其后继结点,另一指针域用来指向其前驱结点。链表的头结点的数据域不存储数据,指向前驱结点的指针域为null,指向后继结点的指针域指向第一个真正存储数据的结点。

具体实现

在这里插入图片描述

package linear;

import java.util.Iterator;

public class TwoWayLinklist<T> implements Iterable<T>{

    //首结点
    private Node head;
    //尾结点
    private Node last;
    //链表的长度
    private int N;



    private class Node{
        //存储数据元素
        public T item;
        //指向上一个结点
        public Node pre;
        //指向下一个结点
        public Node next;
        public Node(T item,Node pre,Node next){
            this.item = item;
            this.pre = pre;
            this.next = next;
        }
    }

    public TwoWayLinklist(){
        //初始化头结点和尾结点
        this.head = new Node(null,null,null);
        this.last = null;
        //指定链表长度
        this.N = 0;
    }

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

    //获取链表长度
    public int length(){
        return N;
    }

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

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

    //获取第一个元素
    public T getFirst(){
        if(isEmpty()){
            return null;
        }else {
            return head.next.item;
        }
    }

    //获取最后一个元素
    public T getLast(){
        if(isEmpty()){
            return null;
        }else{
            return last.item;
        }
    }

    //插入元素t
    public void insert(T t){
        if(isEmpty()){
            Node newNode = new Node(t,head,null);
            last = newNode;
            head.next = newNode;
        }else{
            Node newNode = new Node(t,last,null);
            last.next = newNode;
            last = newNode;
        }
        N++;
    }

    //向指定位置i处插入元素t
    public void insert(int i,T t){
        Node pre = head;
        for(int index=0;index<i;index++){
            pre = pre.next;
        }
        Node curr = pre.next;
        Node newNode = new Node(t,pre,curr);
        pre.next = newNode;
        curr.pre = newNode;
        N++;
    }

    //找到元素t在链表中第一次出现的位置
    public int indexOf(T t){
        Node n = head;
        for(int index=0;n.next!=null;index++){
            n = n.next;
            if(n.item.equals(t)){
                return index;
            }
        }
        return -1;
    }

    //删除指定位置的元素
    public T remove(int i){
        Node pre = head;
        for(int index=0;index<i;index++){
            pre = pre.next;
        }
        Node curr = pre.next;
        Node nextNode = curr.next;
        pre.next = nextNode;
        nextNode.pre = pre;
        N--;
        return curr.item;
    }

    @Override
    public Iterator<T> iterator() {
        return new TIterator();
    }

    private class TIterator implements Iterator{
        private Node n;
        public TIterator(){
            this.n = head;
        }
        @Override
        public boolean hasNext() {
            return n.next!=null;
        }

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

package test;

import linear.TwoWayLinklist;

public class TwoWayLinkListTest {
    public static void main(String[] args) {
        //创建顺序表对象
        TwoWayLinklist<String> s1 = new TwoWayLinklist<>();
        //测试插入
        s1.insert("小红");
        s1.insert("小兰");
        s1.insert("小黄");
        s1.insert(1,"小绿");

        //遍历元素
        for(String s : s1){
            System.out.println(s);
        }
        System.out.println(s1.length());
        System.out.println("-------------------------------------");

        //获取元素
        String getResult = s1.get(1);
        System.out.println("索引1处的值是" + getResult);
        //测试删除
//        String removeResult = s1.remove(0);
//        System.out.println("删除的元素是" + removeResult);
        //测试清空
        //s1.clear();
        //输出元素个数
        //System.out.println("清空后的线性表元素个数是" + s1.length());

        System.out.println("第一个元素" + s1.getFirst());
        System.out.println("最后一个元素" + s1.getLast());

        System.out.println("小兰第一次出现的位置" + s1.indexOf("小兰"));
    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值