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

线性表(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");
        
相关推荐
ヾ慈城3 小时前
【数据结构 - 二叉树】
c语言·数据结构·算法·链表
闲仁人3 小时前
数据结构预科
数据结构
卡戎-caryon3 小时前
【项目实践】贪吃蛇
c语言·数据结构·算法
计算机平台作业答案讲解3 小时前
QT实现GIF动图显示(小白版,可直接copy使用)
服务器·开发语言·数据结构·数据库·c++·qt·动态规划
冲鸭嘟嘟可3 小时前
【数据结构】使用C语言 从零实现一个栈的数据结构
c语言·数据结构·算法
tq025 小时前
Java数据结构-树的面试题
数据结构·面试·职场和发展
Jules_wwy5 小时前
查找——数据结构与算法 总结7
数据结构·查找
bigbigli_大李5 小时前
C++基础21 二维数组及相关问题详解
数据结构·c++·算法
卡戎-caryon7 小时前
【数据结构】06.栈&&队列
c语言·数据结构·算法·链表
2401_858286118 小时前
12.【C语言】创建函数
c语言·开发语言·数据结构