链表的实现(单向链表与双向链表)

1.链表

1.1 单向链表的实现

代码:

/*
* 路人假helloWorld
*/
package com.cjj.sort;

import java.util.Iterator;

public class LinkList<T> implements Iterable<T>{
    //头结点
    private Node head;
    //链表的长度
    int N;

    //结点类
    private class Node{
        //存储数据
        T item;
        //下一个结点
        Node next;

        public Node(T item,Node next){
            this.item = item;
            this.next = next;
        }
    }

    //构造方法
    public LinkList(){
        this.head = new Node(null,null);
        this.N = 0;
    }

    //清空链表
    public void clear(){
        head.next = 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 j = 0; j < i; j++){
            n = n.next;
        }
        return n.item;
    }
    //向链表末尾添加元素T
    public void add(T t){
        Node n = head;
        while(n.next != null){
            n = n.next;
        }
        Node newNode = new Node(t,null);
        n.next = newNode;
        N++;
    }
    //向指定位置i处添加元素t
    public void insert(T t,int i){
        Node n = head.next;
        int j = 0;
        while(j < i-1){
            n = n.next;
            j++;
        }
        Node newNode = new Node(t,n.next);
        n.next = newNode;
        N++;
    }
    //删除指定位置i处的元素,并返回被删除的元素
    public T remove(int i){
        Node n = head.next;
        for (int j = 0; j < i-2; j++){
            n = n.next;
        }
        Node result = n.next;
        n.next = n.next.next;
        //长度减一
        N--;
        return result.item;
    }
    //查找元素t在链表中第一次出现的位置
    public int indexOf(T t){
        Node n = head.next;
        for (int i = 0; i < N; i++){
            if (n.item.equals(t)){
                return i;
            }
            n = n.next;
        }
        return -1;
    }
    //打印所有元素
    public void show(){
        Node n = head;
        while(n.next != null){
            System.out.print(n.next.item);
            n = n.next;
        }
    }

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

    public class LIterator implements Iterator{
        Node n;

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

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

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

测试

package com.cjj.test;

import com.cjj.sort.LinkList;

public class TestLinkList {
    public static void main(String[] args) {
        LinkList<String> arr1 = new LinkList<>();
        System.out.println(arr1.isEmpty());
        arr1.add("喜羊羊");
        arr1.add("美羊羊");
        arr1.add("懒羊羊");
        arr1.add("沸羊羊");
        arr1.add("灰太狼");
        System.out.println(arr1.isEmpty());

        arr1.show();
        System.out.println(arr1.length());
        System.out.println("获取指定位置的元素:" + arr1.get(3));
//        System.out.println("获取指定位置的元素" + arr1.get(-1));
//        System.out.println("获取指定位置的元素" + arr1.get(10));

        arr1.insert("红太狼",2);
        arr1.show();

        System.out.println(arr1.remove(1));
        arr1.show();

        System.out.println(arr1.indexOf("懒羊羊"));
        System.out.println(arr1.indexOf("喜羊"));

        arr1.clear();
        arr1.show();
    }

}

测试结果

1.2 双向链表的实现

/*
*路人假helloWorld
*/
package com.cjj.sort;

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(){
        head.next = null;
        last.pre = null;
        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;
    }
    //在链表末尾添加元素
    public void add(T t){

        if (isEmpty()){
            Node newNode = new Node(t,head,null);
            last = newNode;
            head.next = last;
        }else{
            Node newNode = new Node(t,last,null);
            last.next = newNode;
            last = newNode;
        }
        N++;
    }
    //在指定位置i处插入元素
    public void insert(T t,int i){
        Node n = head;
        int j = 0;
        while(j < i){
            n = n.next;
            j++;
        }
        Node newNode = new Node(t,n,n.next);
        n.next = newNode;
        n = newNode.pre;
        N++;
    }
    //获取指定位置i处的元素
    public T get(int i){
        Node n = head.next;
        for (int j = 0; j < i; j++){
            n = n.next;
        }
        return n.item;
    }
    //查找元素t在链表中第一次出现的位置
    public int indexOf(T t){
        Node n = head.next;
        for (int i = 0; i < N; i++){
            if (n.item.equals(t)){
                return i;
            }
            n = n.next;
        }
        return -1;
    }
    //删除位置i处的元素,并返回该元素
    public T remove(int i){
        Node n = head;
        for (int j = 0; j < i-1; j++){
            n = n.next;
        }
        Node result = n.next;
        n.next = n.next.next;
        n = n.next.next.pre;
        //长度减一
        N--;
        return result.item;
    }
    //打印所有元素
    public void show(){
        Node n = head;
        while(n.next != null){
            System.out.print(n.next.item);
            n = n.next;
        }
    }

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

    public 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 com.cjj.test;

import com.cjj.sort.LinkList;
import com.cjj.sort.TwoWayLinkList;

public class TwoWayTestLinkList {
    public static void main(String[] args) {
        TwoWayLinkList<String> arr1 = new TwoWayLinkList<>();
        System.out.println(arr1.isEmpty());
        arr1.add("喜羊羊");
        arr1.add("美羊羊");
        arr1.add("懒羊羊");
        arr1.add("沸羊羊");
        arr1.add("灰太狼");
        System.out.println(arr1.isEmpty());

        arr1.show();
        System.out.println(arr1.length());
        System.out.println("获取指定位置的元素:" + arr1.get(3));
//        System.out.println("获取指定位置的元素" + arr1.get(-1));
//        System.out.println("获取指定位置的元素" + arr1.get(10));

        arr1.insert("红太狼",2);
        arr1.show();

        System.out.println(arr1.remove(1));
        arr1.show();

        System.out.println(arr1.indexOf("懒羊羊"));
        System.out.println(arr1.indexOf("喜羊"));

        arr1.clear();
        arr1.show();
    }

}

测试结果

posted @   路人假helloWorld  阅读(28)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示