【Java数据结构实现二】-- 线性表 -- 顺序表+单链表+双向链表(增删改查遍历等)

参考视频: 【黑马程序员】2020最新数据结构与算法教程(求职面试必备)
参考leetcode学习资料: 图解算法数据结构

注意 目录结构呦!!!按本文目录 在src文件夹下 创建项目和文件,直接粘代码即可运行

算法和数据结构简述+排序的笔记

【Java数据结构笔记一】-- 数据结构与算法概述–【时间复杂度+空间复杂度】
【Java数据结构笔记三】-- 线性表

Mywrite

linear

SequenceList(顺序表)
package Mywrite.linear;

import java.util.Iterator;

// 线性表——顺序表——基本实现
public class SequenceList<T> implements Iterable<T> {
    // 测试
    public static void main(String[] args) {
        // 创建顺序表对象
        SequenceList<String> SL = new SequenceList<>(10);
        // 测试插入
        SL.insert("会的传教士这部分");
        SL.insert("要命");
        SL.insert("zhaznmusi");
        SL.insert(1,"我的对我的");

        for(String s: SL){
            System.out.println(s);
        }
        // 测试获取
        String getResult = SL.get(1);
        System.out.println("获取1处的索引值为:"+getResult);
        // 测试删除
        String removeResult = SL.remove(0);
        System.out.println("删除的元素是" + removeResult);
        // 清空测试,以及返回长
        SL.clear();
        System.out.println("清空后的线性表中得到元素个数为"+SL.length());
    }

    // 存储元素的数组
    private T[] eles;
    // 记录当前顺序表中的元素个数
    private int N;

    //构造方法
    public SequenceList(int capacity) {
        // 初始化数据
        this.eles = (T[]) new Object[capacity];
        // 初始化长度
        this.N = 0;
    }

    // 将一个线性表置为空表
    public void clear() {
        this.N = 0;
    }

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

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

    //获取指定位置的元素
    public T get(int i) {
        return eles[i];
    }

    // 向线性表中添加元素t
    public void insert(T t) {
        eles[N++] = t;
    }

    //在i元素处插入元素t
    public void insert(int i, T t) {
        if (N == eles.length) {
            resize(2*eles.length);
        }
        // 先把i索引的元素及其后面的元素依次后移一位
        for (int index = N; index > i; index--) {
            eles[index] = eles[index - 1];
        }
        //将t元素放到i索引处即可
        eles[i] = t;

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

    // 删除指定位置i处的元素,并返回该元素
    public T remove(int i) {
        //  记录索引i处的值
        T current = eles[i];
        for (int index = i; index < N - 1; index++) {
            eles[index] = eles[index + 1];
        }
        // 元素个数 -1
        N--;
        if(N<eles.length/4) {
            resize(eles.length/2);
        }
        return current;
    }

    //查找t元素第一次出现的位置
    public int indexOf(T t) {
        for (int index = 0; index < N; index++) {
            if (eles[index].equals(t)) {
                return index;
            }
        }
        return -1;
    }

    //根据参数newSize , 重置eles的大小
    public void resize(int newSize) {
        // 定义一个临时数组,指向原数组
        T[] temp = eles;
        // 创建新数组
        eles = (T[]) new Object[newSize];
        // 把原数组的数据拷贝到新数组即可
        for (int i = 0; i < N; i++) {
            eles[i] = temp[i];
        }
    }


    //提供便利
    @Override
    public Iterator<T> iterator() {
        // 内部类不能遍历
        return new SIterator();
    }

    private class SIterator implements Iterator {
        private int cursor;

        public SIterator() {
            this.cursor = 0;
        }

        @Override
        public boolean hasNext() {
            return cursor < N;
        }

        //获取当前元素的下一个方法
        @Override
        public Object next() {
            return eles[cursor++];
        }
    }
}

LinkList(单链表)
package Mywrite.linear;

import java.util.Iterator;


public class LinkList<T> implements Iterable<T>{
    public static void main(String[] args) {
        // 创建顺序表对象
        LinkList<String> SL = new LinkList<>();
        // 测试插入
        SL.insert("会的传教士这部分");
        SL.insert("要命");
        SL.insert("zhaznmusi");
        SL.insert(1,"我的对我的");

        for(String s: SL){
            System.out.println(s);
        }
        // 测试获取
        String getResult = SL.get(1);
        System.out.println("获取1处的索引值为:"+getResult);
        // 测试删除
        String removeResult = SL.remove(0);
        System.out.println("删除的元素是" + removeResult);
        // 清空测试,以及返回长
        SL.clear();
        System.out.println("清空后的线性表中得到元素个数为"+SL.length());
    }

    
    //记录头结点
    private Node head;
    //记录链表的长度
    private 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){
        // 通过循环,从头结点开始往后找,依次找i次,就可以找出对应的元素
        Node n = head.next;
        for(int index = 0; index<i; index++){
            n = n.next;
        }
        return n.item;
    }

    // 向链表中添加元素t
    public void insert(T t){
        //找到链表中的最后一个结点
        Node n = head;
        while(n.next != null){
            n = n.next;
        }

        //创建新的节点
        Node newNode = new Node(t, null);
        n.next = newNode;

        // 元素的个数夹一
        N++;
    }

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

        Node curr = pre.next;
        Node newNode = new Node(t,curr);
        pre.next = newNode;

        N++;
    }


    // 删除指定位置i出的元素,并返回被删除的元素
    public T remove(int i){
        Node pre = head;
        for(int index = 0; index<=i-1; index++){
            pre = pre.next;
        }

        Node curr = pre.next;
        pre.next = curr.next;
        N--;
        return curr.item;
    }

    // 查找元素t在链表中第一次出现的位置
    public int search(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;
    }


    @Override
    public Iterator<T> 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 Object next() {
            n = n.next;
            return n.item;
        }
    }
}
TwoWayLinkList(双向链表)
package Mywrite.linear;

//import org.w3c.dom.Node;


import java.util.Iterator;

public class TwoWayLinkList<T> implements Iterable<T>{
    public static void main(String[] args) {
        // 创建双向链表对象
        TwoWayLinkList<String> sl= new TwoWayLinkList<>();
        // 测试插入
        sl.insert("姚明");
        sl.insert("科比");
        sl.insert("麦迪");
        sl.insert(1,"詹姆斯");
        for(String s: sl){
            System.out.println(s);
        }
        System.out.println("-----------------");

        // 测试获取
        String getResult = sl.get(1);
        System.out.println("获取索引1处的结果为:"+getResult);
        // 测试删除
        String removeResult = sl.get(0);
        System.out.println("删除的元素:" + removeResult);



        System.out.println("---------------------");
        System.out.println("第一个元素是"+sl.getFirst());
        System.out.println("最后一个元素是:" + sl.getLast());

        // 测试清空
        sl.clear();
        System.out.println("清空后的线性表中的元素个数为:"+sl.length());
    }

    //首结点
    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.head.item = 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;
        }
        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++;
    }
    // 获取指定位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 index = 0; index<N; index++){
            n = n.next;
            if(n.next.equals(t)){
                return index;
            }
        }
        return -1;
    }

    // 删除位置i处的元素,并返回该元素
    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;
        }
    }
}


请添加图片描述

要点赞+关注+收藏呦👉




笔记在博主本人的博客上也有奥!全套!!!

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值