📜  数据结构介绍(1)

📅  最后修改于: 2023-12-03 15:40:01.600000             🧑  作者: Mango

数据结构介绍

在计算机科学中,数据结构是指数据元素以及它们之间的关系和操作。数据结构是程序设计的基础,能够大幅提高程序的效率和可读性。常见的数据结构包括:数组,链表,栈,队列,树,图等等。

数组

数组是一种常见的数据结构,其中相同数据类型的元素按线性序列排列。数组具有以下特点:

  • 数组长度固定,不可改变。
  • 数组中的元素可以通过下标访问,下标从0开始。
  • 可以在O(1)时间内访问和修改数组中的任意元素。
  • 插入和删除操作比较耗时,需要O(n)的时间复杂度。

数组的代码片段:

int[] arr = new int[10]; // 声明一个长度为10的整型数组
arr[0] = 1; // 将第一个元素设置为1
int x = arr[0]; // 获取第一个元素的值
链表

链表是一种常见的数据结构,其中每个元素包含两个字段:一个是存储数据的变量,另一个是指向下一个元素的指针。链表具有以下特点:

  • 链表长度不固定,可以根据需要进行扩展和收缩。
  • 链表不满足随机访问,只能从头开始遍历,访问某个元素需要O(n)时间复杂度。
  • 插入和删除操作比较方便,只需要修改指针的指向即可。

单向链表的代码片段:

class Node {
    int val;
    Node next;
    public Node(int val) {
        this.val = val;
        this.next = null;
    }
}

Node head = new Node(1); // 创建链表的头节点
Node node1 = new Node(2); // 创建第一个节点
Node node2 = new Node(3); // 创建第二个节点

head.next = node1;
node1.next = node2;

// 遍历链表
Node curr = head;
while (curr != null) {
    System.out.println(curr.val);
    curr = curr.next;
}
栈和队列

队列是两种重要的数据结构,它们都可以通过数组或链表实现:

  • 栈:后进先出(LIFO)的数据结构,类似于装满盘子的操作。
  • 队列:先进先出(FIFO)的数据结构,类似于排队的操作。

栈和队列的代码片段:

// 栈的实现
class Stack {
    int[] arr;
    int top;
    public Stack(int size) {
        arr = new int[size];
        top = -1;
    }
    public void push(int x) {
        arr[++top] = x;
    }
    public int pop() {
        return arr[top--];
    }
    public boolean isEmpty() {
        return top == -1;
    }
}

Stack st = new Stack(10);
st.push(1);
st.push(2);
st.push(3);
while (!st.isEmpty()) {
    System.out.println(st.pop());
}

// 队列的实现
class Queue {
    int[] arr;
    int size;
    int front;
    int rear;
    public Queue(int size) {
        arr = new int[size];
        this.size = size;
        front = 0;
        rear = 0;
    }
    public void enqueue(int x) {
        arr[rear++] = x;
    }
    public int dequeue() {
        return arr[front++];
    }
    public boolean isEmpty() {
        return front == rear;
    }
}

Queue q = new Queue(10);
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
while (!q.isEmpty()) {
    System.out.println(q.dequeue());
}
树和图

是比较复杂的数据结构,它们都可以通过链表实现:

  • 树:每个元素只有一个父节点,但可以有多个子节点。
  • 图:由一组节点和一组连接这些节点的边组成。

树和图的代码片段:

// 树的实现
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    public TreeNode(int val) {
        this.val = val;
        left = null;
        right = null;
    }
}

TreeNode root = new TreeNode(1);
TreeNode node1 = new TreeNode(2);
TreeNode node2 = new TreeNode(3);
root.left = node1;
root.right = node2;

// 遍历树
void inorderTraversal(TreeNode root) {
    if (root == null) {
        return;
    }
    inorderTraversal(root.left);
    System.out.println(root.val);
    inorderTraversal(root.right);
}

inorderTraversal(root);

// 图的实现
class Graph {
    int[][] adjMatrix;
    public Graph(int size) {
        adjMatrix = new int[size][size];
    }
    public void addEdge(int i, int j) {
        adjMatrix[i][j] = 1;
        adjMatrix[j][i] = 1;
    }
}

Graph g = new Graph(4);
g.addEdge(0, 1);
g.addEdge(1, 2);
g.addEdge(2, 3);

以上介绍了常见的数据结构及其代码片段。不同的数据结构适用于不同的场景,程序员需要根据实际需求来选择合适的数据结构。