数据结构奇妙旅程之线性表

线性表(Linear List)是数据结构中的一种基本类型,它代表了具有相同类型数据元素的一个有限序列。线性表中的数据元素之间是一对一的关系,即除了第一个元素外,每个元素有且仅有一个前驱元素;除了最后一个元素外,每个元素有且仅有一个后继元素。数据元素间的线性关系是一对一的关系。

线性表在Java中可以通过多种方式实现,最常见的是使用数组(Array)和链表(LinkedList)。下面分别介绍这两种实现方式及其代码。

1. 数组实现的线性表

数组是线性表的一种顺序存储结构,它使用一段地址连续的存储单元依次存储线性表的数据元素。

java 复制代码
public class ArrayList<T> {
    private T[] elements;
    private int size;
    private static final int DEFAULT_CAPACITY = 10;

    @SuppressWarnings("unchecked")
    public ArrayList() {
        this.elements = (T[]) new Object[DEFAULT_CAPACITY];
        this.size = 0;
    }

    public void add(T element) {
        if (size == elements.length) {
            resize();
        }
        elements[size++] = element;
    }

    private void resize() {
        T[] newElements = (T[]) new Object[elements.length * 2];
        System.arraycopy(elements, 0, newElements, 0, size);
        elements = newElements;
    }

    public T get(int index) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
        }
        return elements[index];
    }

    public int size() {
        return size;
    }

    // 其他方法,如删除、修改等...
}

这个简单的ArrayList类使用泛型T来存储任意类型的元素,并维护一个数组elements来存储数据。add方法用于向线性表中添加元素,如果当前数组已满,则通过resize方法扩大数组容量。get方法用于获取指定索引位置的元素。size方法返回线性表中元素的数量。

2. 链表实现的线性表

链表是线性表的链式存储结构,用一组任意的存储单元来存放线性表的元素。为了表示每个数据元素与其直接后继数据元素之间的逻辑关系,对数据元素除了存放其本身的信息外,还需存放一个指示其直接后继的信息(即直接后继的存储位置)。

java 复制代码
public class Node<T> {
    T data;
    Node<T> next;

    public Node(T data) {
        this.data = data;
        this.next = null;
    }
}

public class LinkedList<T> {
    private Node<T> head;
    private int size;

    public LinkedList() {
        this.head = null;
        this.size = 0;
    }

    public void add(T element) {
        Node<T> newNode = new Node<>(element);
        if (head == null) {
            head = newNode;
        } else {
            Node<T> current = head;
            while (current.next != null) {
                current = current.next;
            }
            current.next = newNode;
        }
        size++;
    }

    public T get(int index) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
        }
        Node<T> current = head;
        for (int i = 0; i < index; i++) {
            current = current.next;
        }
        return current.data;
    }

    public int size() {
        return size;
    }

    // 其他方法,如删除、修改等...
}

在这个LinkedList类中,我们定义了一个内部类Node来存储数据和指向下一个节点的引用。add方法用于向链表的末尾添加新元素,而get方法则通过遍历链表来找到指定索引位置的元素。size方法返回链表中元素的数量。

使用示例

java 复制代码
public class Main {
    public static void main(String[] args) {
        // 使用数组实现的线性表
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("Element 1");
        arrayList.add("Element 2");
        
相关推荐
瓦特what?4 分钟前
关于C++的#include的超超超详细讲解
java·开发语言·数据结构·c++·算法·信息可视化·数据挖掘
重生之我是Java开发战士1 小时前
【数据结构】深入理解单链表与通讯录项目实现
数据结构·链表
tanxiaomi1 小时前
数据库索引视角:对比二叉树到红黑树再到B树
数据结构·数据库·b树
lifallen1 小时前
JCTools 无锁并发队列基础:ConcurrentCircularArrayQueue
java·开发语言·数据结构·算法
欧哈东哥2 小时前
【C++】标准库中用于组合多个值的数据结构pair、tuple、array...
java·数据结构·c++
野生的编程萌新7 小时前
从冒泡到快速排序:探索经典排序算法的奥秘(二)
c语言·开发语言·数据结构·c++·算法·排序算法
花开富贵ii8 小时前
代码随想录算法训练营四十三天|图论part01
java·数据结构·算法·深度优先·图论
code小毛孩9 小时前
leetcode hot100数组:缺失的第一个正数
数据结构·算法·leetcode
艾伦~耶格尔16 小时前
【数据结构进阶】
java·开发语言·数据结构·学习·面试
闪电麦坤9518 小时前
数据结构:N个节点的二叉树有多少种(Number of Binary Trees Using N Nodes)
数据结构·二叉树·