码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【重拾C语言】六、批量数据组织(四)线性表—栈和队列


    目录

    前言

    六、批量数据组织——数组

    6.1~3 数组基础知识

    6.4 线性表——分类与检索

    6.5~7 数组初值;字符串、字符数组、字符串数组;类型定义 typedef

    6.8 线性表—栈和队列

    6.8.1 栈(Stack)

    全局变量

    isEmpty()

    isFull()

    push()

    pop()

    测试

    6.8.2 队列(Queue)

    全局变量

    isEmpty()

    isFull()

    enqueue()

    dequeue()

    测试


    前言

            本文介绍了C语言使用数组实现栈和队列,及其相关操作

    六、批量数据组织——数组

    6.1~3 数组基础知识

    【重拾C语言】六、批量数据组织(一)数组(数组类型、声明与操作、多维数组;典例:杨辉三角、矩阵乘积、消去法)-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/m0_63834988/article/details/133580645?spm=1001.2014.3001.5502

    6.4 线性表——分类与检索

    【重拾C语言】六、批量数据组织(二)线性表——分类与检索(主元排序、冒泡排序、插入排序、顺序检索、对半检索)_QomolangmaH的博客-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/m0_63834988/article/details/133620693?spm=1001.2014.3001.5501

    6.5~7 数组初值;字符串、字符数组、字符串数组;类型定义 typedef

    【重拾C语言】六、批量数据组织(三)数组初值;字符串、字符数组、字符串数组;类型定义 typedef_QomolangmaH的博客-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/m0_63834988/article/details/133660998?spm=1001.2014.3001.5501

    6.8 线性表—栈和队列

            栈(Stack)和队列(Queue)是常用的线性数据结构。在C语言中,可以使用数组或链表来实现栈和队列。

    6.8.1 栈(Stack)

    • 栈是一种后进先出(Last-In-First-Out,LIFO)的数据结构。
    • 使用数组实现栈时,可以使用一个整数变量来表示栈顶指针(top),指向栈顶元素的位置。
    • 初始化栈时,将栈顶指针设置为-1,表示栈为空。
    • 入栈操作(Push)将元素添加到栈顶,栈顶指针加1。
    • 出栈操作(Pop)从栈顶移除元素,栈顶指针减1。
    • 可以使用数组来存储栈的元素。
    全局变量
    1. #define MAX_SIZE 100
    2. int stack[MAX_SIZE];
    3. int top = -1;
    • 定义了一个常量 MAX_SIZE,它表示栈的最大容量。
    • 声明了一个整型数组 stack,用于存储栈中的元素。
    • 声明了一个整型变量 top,用于表示栈顶的索引,默认值为 -1,表示栈为空。

    isEmpty()

            检查栈是否为空。如果栈为空,返回值为 1,否则返回值为 0。

    1. int isEmpty() {
    2. return top == -1;
    3. }
    isFull()

            检查栈是否已满。如果栈已满,返回值为 1,否则返回值为 0。

    1. int isFull() {
    2. return top == MAX_SIZE - 1;
    3. }

    push()

            将元素压入栈中。首先检查栈是否已满,如果已满则打印提示信息并返回,否则将 data 压入栈顶,然后将 top 值加 1。

    1. void push(int data) {
    2. if (isFull()) {
    3. printf("Stack is full. Cannot push element.\n");
    4. return;
    5. }
    6. stack[++top] = data;
    7. }
    pop()

      从栈中弹出并返回栈顶元素。首先检查栈是否为空,如果为空则打印提示信息并返回 -1,否则将栈顶元素返回,然后将 top 值减 1。

    1. int pop() {
    2. if (isEmpty()) {
    3. printf("Stack is empty. Cannot pop element.\n");
    4. return -1;
    5. }
    6. return stack[top--];
    7. }
    测试
    1. #include
    2. #define MAX_SIZE 100
    3. int stack[MAX_SIZE];
    4. int top = -1;
    5. int isEmpty() {
    6. return top == -1;
    7. }
    8. int isFull() {
    9. return top == MAX_SIZE - 1;
    10. }
    11. void push(int data) {
    12. if (isFull()) {
    13. printf("Stack is full. Cannot push element.\n");
    14. return;
    15. }
    16. stack[++top] = data;
    17. }
    18. int pop() {
    19. if (isEmpty()) {
    20. printf("Stack is empty. Cannot pop element.\n");
    21. return -1;
    22. }
    23. return stack[top--];
    24. }
    25. int main() {
    26. push(10);
    27. push(20);
    28. push(30);
    29. printf("Popped element: %d\n", pop());
    30. printf("Popped element: %d\n", pop());
    31. return 0;
    32. }
    • 调用 push(10) 将元素 10 压入栈中。
    • 调用 push(20) 将元素 20 压入栈中。
    • 调用 push(30) 将元素 30 压入栈中。
    • 调用 pop() 弹出栈顶元素,并将其打印出来。
    • 再次调用 pop() 弹出栈顶元素,并将其打印出来。

    6.8.2 队列(Queue)

    • 队列是一种先进先出(First-In-First-Out,FIFO)的数据结构。
    • 使用数组实现队列时,需要两个整数变量来表示队列的头部指针(front)和尾部指针(rear)。
    • 初始化队列时,将头部指针和尾部指针都设置为-1,表示队列为空。
    • 入队操作(Enqueue)将元素添加到队列尾部,尾部指针加1。
    • 出队操作(Dequeue)从队列头部移除元素,头部指针加1。
    全局变量
    1. #define MAX_SIZE 100
    2. int queue[MAX_SIZE];
    3. int front = -1;
    4. int rear = -1;
    • 定义了一个常量 MAX_SIZE,它表示队列的最大容量。
    • 声明了一个整型数组 queue,用于存储队列中的元素。
    • 声明了两个整型变量 front 和 rear,分别表示队列的前端和后端的索引,默认值均为 -1,表示队列为空。
    isEmpty()

            检查队列是否为空。如果队列为空,返回值为 1,否则返回值为 0。

    1. int isEmpty() {
    2. return front == -1;
    3. }
    isFull()

            检查队列是否已满。如果队列已满,返回值为 1,否则返回值为 0。

    1. int isFull() {
    2. return (rear + 1) % MAX_SIZE == front;
    3. }
    enqueue()

            将元素入队。首先检查队列是否已满,如果已满则打印提示信息并返回,否则根据队列的循环性质更新 rear 的值,并将 data 存储到相应位置。

    1. void enqueue(int data) {
    2. if (isFull()) {
    3. printf("Queue is full. Cannot enqueue element.\n");
    4. return;
    5. }
    6. if (isEmpty()) {
    7. front = 0;
    8. }
    9. rear = (rear + 1) % MAX_SIZE;
    10. queue[rear] = data;
    11. }
    dequeue()

       用于从队列中出队并返回队首元素。首先检查队列是否为空,如果为空则打印提示信息并返回 -1,否则取出队首元素并根据队列的循环性质更新 front 和 rear 的值。

    1. int dequeue() {
    2. if (isEmpty()) {
    3. printf("Queue is empty. Cannot dequeue element.\n");
    4. return -1;
    5. }
    6. int data = queue[front];
    7. if (front == rear) {
    8. front = -1;
    9. rear = -1;
    10. } else {
    11. front = (front + 1) % MAX_SIZE;
    12. }
    13. return data;
    14. }

    测试
    1. #include
    2. #define MAX_SIZE 100
    3. int queue[MAX_SIZE];
    4. int front = -1;
    5. int rear = -1;
    6. int isEmpty() {
    7. return front == -1;
    8. }
    9. int isFull() {
    10. return (rear + 1) % MAX_SIZE == front;
    11. }
    12. void enqueue(int data) {
    13. if (isFull()) {
    14. printf("Queue is full. Cannot enqueue element.\n");
    15. return;
    16. }
    17. if (isEmpty()) {
    18. front = 0;
    19. }
    20. rear = (rear + 1) % MAX_SIZE;
    21. queue[rear] = data;
    22. }
    23. int dequeue() {
    24. if (isEmpty()) {
    25. printf("Queue is empty. Cannot dequeue element.\n");
    26. return -1;
    27. }
    28. int data = queue[front];
    29. if (front == rear) {
    30. front = -1;
    31. rear = -1;
    32. } else {
    33. front = (front + 1) % MAX_SIZE;
    34. }
    35. return data;
    36. }
    37. int main() {
    38. enqueue(10);
    39. enqueue(20);
    40. enqueue(30);
    41. printf("Dequeued element: %d\n", dequeue());
    42. printf("Dequeued element: %d\n", dequeue());
    43. return 0;
    44. }
    • 调用 enqueue(10) 将元素 10 入队。
    • 调用 enqueue(20) 将元素 20 入队。
    • 调用 enqueue(30) 将元素 30 入队。
    • 调用 dequeue() 出队并打印出队的元素。
    • 再次调用 dequeue() 出队并打印出队的元素。

  • 相关阅读:
    网页视频F12倍速看
    【C++】基础知识点回顾 下:auto关键字、范围内的for循环
    CANoe创建仿真工程
    将来已来-SoftwareDemo软件测试种子的埋下去-第二次重大情绪点的触及到
    画画水族馆的应用特色及功能
    1600*C. Good Subarrays(找规律&&前缀和)
    hadoop_虚拟机linux环境部署全教程
    HTML+CSS+JS鲜花商城网页设计期末课程大作业 web前端开发技术 web课程设计 网页规划与设计
    如何在 HarmonyOS 对数据库进行备份,恢复与加密
    Dapr 官方文档中文翻译 v1.5 版本正式发布
  • 原文地址:https://blog.csdn.net/m0_63834988/article/details/133688931
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号