数据结构day05

一、栈(先进后出 FILO)

1. 核心概念

  • 栈顶:入栈、出栈都从栈顶操作(核心操作位)
  • 栈底:栈为空的标记(所有元素出栈后栈顶回到栈底)
  • 入栈(压栈):往栈里存数据;出栈(弹栈):从栈顶取数据

2. 核心代码逻辑(单链表实现)

c

运行

复制代码
// 1. 定义栈结构体
struct stack {
    int data;          // 存储数据
    struct stack *stacktop; // 记录栈顶(优化:不用每次遍历找栈顶)
    struct stack *next;     // 链表指针
};

// 2. 初始化栈
struct stack *stack_init() {
    struct stack *head = malloc(sizeof(struct stack));
    head->stacktop = head; // 初始栈顶指向头结点
    head->next = NULL;
    return head;
}

// 3. 入栈(直接在栈顶后加节点,更新栈顶)
void push(int newdata, struct stack *head) {
    struct stack *newnode = malloc(sizeof(struct stack));
    newnode->data = newdata;
    newnode->next = NULL;
    head->stacktop->next = newnode; // 新节点接在栈顶后
    head->stacktop = newnode;       // 更新栈顶
}

// 4. 出栈(删除栈顶,更新栈顶)
int pop(struct stack *head) {
    if (head->next == NULL) { // 栈空判断
        printf("栈空,无法出栈!\n");
        return -1;
    }
    int temp = head->stacktop->data; // 备份栈顶数据
    // 找栈顶前一个节点
    struct stack *p = head;
    while (p->next != head->stacktop) p = p->next;
    // 删除栈顶
    free(head->stacktop);
    p->next = NULL;
    head->stacktop = p; // 更新栈顶
    return temp;
}

3. 典型应用

  • 进制转换(如十进制转 2/8/16 进制):余数入栈,出栈拼接结果(栈的先进后出适配进制转换逻辑)

二、队列(先进先出 FIFO)

1. 核心概念

  • 队首:出队操作位(固定是头结点的下一个)
  • 队尾:入队操作位(入队后更新队尾)
  • 入队:往队尾加数据;出队:从队首取数据

2. 核心代码逻辑(单链表实现)

c

运行

复制代码
// 1. 定义队列结构体
struct queue {
    int data;            // 存储数据
    struct queue *queuetail; // 记录队尾(优化:不用遍历找队尾)
    struct queue *next;      // 链表指针
};

// 2. 初始化队列
struct queue *queue_init() {
    struct queue *head = malloc(sizeof(struct queue));
    head->queuetail = head; // 初始队尾指向头结点
    head->next = NULL;
    return head;
}

// 3. 入队(队尾加节点,更新队尾)
void in_queue(int newdata, struct queue *head) {
    struct queue *newnode = malloc(sizeof(struct queue));
    newnode->data = newdata;
    newnode->next = NULL;
    head->queuetail->next = newnode; // 新节点接在队尾后
    head->queuetail = newnode;       // 更新队尾
}

// 4. 出队(队首删节点)
int out_queue(struct queue *head) {
    if (head->next == NULL) { // 队空判断
        printf("队空,无法出队!\n");
        return -1;
    }
    struct queue *p = head->next; // 指向队首
    int temp = p->data;           // 备份队首数据
    head->next = p->next;         // 队首后移
    free(p);
    return temp;
}

三、快速排序

1. 核心思想

  • 选基准数(默认第一个数),一轮排序后:基准数左边都更小,右边都更大
  • 递归处理基准数左边、右边的子数组,直到子数组长度为 1(有序)

2. 核心代码逻辑

c

运行

复制代码
void sort(int a[], int left, int right) {
    if (left > right) return; // 递归终止条件
    int tem = a[left]; // 基准数
    int j = left, k = right;
    // 找交换位置:j从左找比基准大的,k从右找比基准小的
    while (j != k) {
        while (a[k] >= tem && j != k) k--; // 右→左找小数
        while (a[j] <= tem && j != k) j++; // 左→右找大数
        if (j != k) { // 交换找到的两个数
            int t = a[j];
            a[j] = a[k];
            a[k] = t;
        }
    }
    // 基准数归位(j/k重叠位置是基准数的正确位置)
    a[left] = a[j];
    a[j] = tem;
    // 递归排序左、右子数组
    sort(a, left, j-1);
    sort(a, j+1, right);
}

四、通用易错点 & 小技巧

  1. 内存管理:malloc 申请空间后,不用时记得 free(避免内存泄漏)
  2. 判空逻辑:栈 / 队列判空用 head->next == NULL(比单纯返回 - 1 更通用)
  3. 进制转换:16 进制中≥10 的数转字母:result+55(10→A,11→B...)
  4. 递归终止:快速排序必须加 left > right,否则死循环

五、核心口诀

  • 栈:先进后出,栈顶操作,入栈更栈顶,出栈删栈顶
  • 队列:先进先出,队尾入、队首出,入队更队尾,出队移队首
  • 快排:选基准,分左右,递归排,基准归位就有序
相关推荐
小妖66620 小时前
js 实现快速排序算法
数据结构·算法·排序算法
独好紫罗兰1 天前
对python的再认识-基于数据结构进行-a003-列表-排序
开发语言·数据结构·python
wuhen_n1 天前
JavaScript内置数据结构
开发语言·前端·javascript·数据结构
2401_841495641 天前
【LeetCode刷题】二叉树的层序遍历
数据结构·python·算法·leetcode·二叉树··队列
独好紫罗兰1 天前
对python的再认识-基于数据结构进行-a002-列表-列表推导式
开发语言·数据结构·python
2401_841495641 天前
【LeetCode刷题】二叉树的直径
数据结构·python·算法·leetcode·二叉树··递归
数智工坊1 天前
【数据结构-树与二叉树】4.5 线索二叉树
数据结构
数智工坊1 天前
【数据结构-树与二叉树】4.3 二叉树的存储结构
数据结构
独好紫罗兰1 天前
对python的再认识-基于数据结构进行-a004-列表-实用事务
开发语言·数据结构·python
铉铉这波能秀1 天前
LeetCode Hot100数据结构背景知识之列表(List)Python2026新版
数据结构·leetcode·list