双向链表各项功能(JAVA语言)实现

双向链表各项功能(JAVA语言)实现

结点API设计:
类名: Node
构造方法:Node(T t,Node pre,Node next):创建Node对象
成员变量: T item:存储数据
Node pre:指向上一个结点
Node next:指向下一个结点

**双向链表的API设计
类名:TwoWayLinkList;

构造方法:TwoWayLinkList :创建TwoWayLinkList 对象;

成员方法:
public void clear();//清空链表
:public int length();//获取链表长度
:public boolean isEmpty();//判断链表是否为空
public T getFirst();//获取第一个元素
public T getLast();//获取最后一个元素
public void insert(T t);//插入元素t
public void insert(int i,T t);//向指定位置i处插入元素t
public T get(int i);//获取指定位置i处的元素
public int indexOf(T t);//找到元素t在链表第一次出现的位置
public T remove(int i);//删除位置i处的元素,并返回该元素

成员内部类:private class Node:结点类;

成员变量:
private Node first:记录首结点;
private Node last:记录尾结点;
private int N;记录链表的长度;

******代码实现:

public class TwoWayLinkList<T>{
    //首结点
    private Node head;
    //最后一个结点
    private Node last;
    //链表的长度
    private int N;

    //结点类
    private class Node{
        public Node(T item,Node pre,Node next){
            this.item = item;
            this.pre = pre;
            this.next = next;
        }
        //存储数据
        public T item;
        //指向上一个结点
        public Node pre;
        //指向下一个结点
        public Node 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;
    }

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

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

    //插入元素t
    public void insert(T t){
        if(isEmpty()){
            //如果链表为空:
            //创建新的结点
            Node newNode = new Node(t, head, null);

            //让新结点成为尾结点
            last = newNode;

            //让头结点指向尾结点
            head.next = last;
        }else {
            //如果链表不为空:
            Node oldLast = last;

            //创建新结点
            Node newNode = new Node(t, oldLast, null);

            //让当前的尾结点指向新结点
            oldLast.next = newNode;

            //让新结点成为尾结点
            last = newNode;
        }

        //元素个数+1
        N++;
    }

    //向指定位置i处插入元素t
    public void insert(int i,T t){
        //找到i位置的前一个结点
        Node pre = head;
        for(int index = 0; index < i-1; index++){
            pre = pre.next;
        }

        //找到i位置的结点
        Node curr = pre.next;

        //创建新结点
        Node newNode = new Node(t, pre, curr);

        //把原来i位置的前一个结点的下一个结点连接新结点
        pre.next = newNode;

        //把i位置的前一个结点的下一个结点变为新结点
        curr.pre = newNode;

        //元素个数+1;
        N++;
    }

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

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

    //删除位置i处的元素,并返回该元素
    public T remove(int i){
        //找到i位置的前一个结点
        Node pre = head;
        for(int index = 0; index < i-1; index++){
            pre = pre.next;
        }

        //找到i位置的结点
        Node curr = pre.next;

        //找到i位置的下一个结点
        Node nextNode = curr.next;

        //删除i位置的结点
        pre.next = nextNode;
        nextNode.pre = pre;

        //参数的个数-1
        N--;

        return curr.item;
    }
}
发布了22 篇原创文章 · 获赞 21 · 访问量 1388

猜你喜欢

转载自blog.csdn.net/qq_43751200/article/details/104583876