【“栈、队列”的应用】408数据结构代码

news2024/9/21 4:37:01

王道数据结构强化课——【“栈、队列”的应用】代码,持续更新
在这里插入图片描述

链式存储栈(单链表实现),并基于上述定义,栈顶在链头,实现“出栈、入栈、判空、判满”四个基本操作

#include <stdio.h>
#include <stdlib.h>

// 定义链表节点
struct Node {
    int data;
    struct Node* next;
};

// 定义栈结构
struct Stack {
    struct Node* top; // 栈顶指针
};

// 初始化栈
void initStack(struct Stack* stack) {
    stack->top = NULL;
}

// 入栈操作
void push(struct Stack* stack, int value) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    if (newNode == NULL) {
        printf("内存分配失败,无法执行入栈操作\n");
        return;
    }
    newNode->data = value;
    newNode->next = stack->top;
    stack->top = newNode;
}

// 出栈操作
int pop(struct Stack* stack) {
    if (stack->top == NULL) {
        printf("栈为空,无法执行出栈操作\n");
        return -1; // 返回一个错误值
    }
    struct Node* temp = stack->top;
    int poppedValue = temp->data;
    stack->top = temp->next;
    free(temp);
    return poppedValue;
}

// 判空操作
int isEmpty(struct Stack* stack) {
    return (stack->top == NULL);
}

// 判满操作(对于链式存储的栈,通常不会满,所以返回0表示不满)
int isFull(struct Stack* stack) {
    return 0;
}

// 释放栈内存
void freeStack(struct Stack* stack) {
    while (stack->top != NULL) {
        struct Node* temp = stack->top;
        stack->top = temp->next;
        free(temp);
    }
}

int main() {
    struct Stack stack;
    initStack(&stack);

    // 入栈操作
    push(&stack, 1);
    push(&stack, 2);
    push(&stack, 3);

    // 出栈操作
    printf("出栈操作: %d\n", pop(&stack));

    // 判空操作
    printf("栈是否为空: %s\n", isEmpty(&stack) ? "是" : "否");

    // 判满操作
    printf("栈是否满: %s\n", isFull(&stack) ? "是" : "否");

    // 释放栈内存
    freeStack(&stack);

    return 0;
}

链式存储栈(双向链表实现)

#include <stdio.h>
#include <stdlib.h>

// 定义链表节点
struct Node {
    int data;
    struct Node* next;
    struct Node* prev;
};

// 定义栈结构
struct Stack {
    struct Node* top; // 栈顶指针,链尾
};

// 初始化栈
void initStack(struct Stack* stack) {
    stack->top = NULL;
}

// 入栈操作
void push(struct Stack* stack, int value) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    if (newNode == NULL) {
        printf("内存分配失败,无法执行入栈操作\n");
        return;
    }
    newNode->data = value;
    newNode->next = NULL;

    if (stack->top == NULL) {
        newNode->prev = NULL;
        stack->top = newNode;
    } else {
        newNode->prev = stack->top;
        stack->top->next = newNode;
        stack->top = newNode;
    }
}

// 出栈操作
int pop(struct Stack* stack) {
    if (stack->top == NULL) {
        printf("栈为空,无法执行出栈操作\n");
        return -1; // 返回一个错误值
    }
    struct Node* temp = stack->top;
    int poppedValue = temp->data;

    if (stack->top->prev != NULL) {
        stack->top = stack->top->prev;
        stack->top->next = NULL;
    } else {
        stack->top = NULL;
    }

    free(temp);
    return poppedValue;
}

// 判空操作
int isEmpty(struct Stack* stack) {
    return (stack->top == NULL);
}

// 判满操作(对于链式存储的栈,通常不会满,所以返回0表示不满)
int isFull(struct Stack* stack) {
    return 0;
}

// 释放栈内存
void freeStack(struct Stack* stack) {
    while (stack->top != NULL) {
        struct Node* temp = stack->top;
        stack->top = temp->prev;
        free(temp);
    }
}

int main() {
    struct Stack stack;
    initStack(&stack);

    // 入栈操作
    push(&stack, 1);
    push(&stack, 2);
    push(&stack, 3);

    // 出栈操作
    printf("出栈操作: %d\n", pop(&stack));

    // 判空操作
    printf("栈是否为空: %s\n", isEmpty(&stack) ? "是" : "否");

    // 判满操作
    printf("栈是否满: %s\n", isFull(&stack) ? "是" : "否");

    // 释放栈内存
    freeStack(&stack);

    return 0;
}

顺序存储的队列(数组实现)

#include <stdio.h>
#include <stdlib.h>

#define MAX_QUEUE_SIZE 10 // 队列的最大容量

// 定义队列结构
struct Queue {
    int front, rear; // 前后指针
    int data[MAX_QUEUE_SIZE];
};

// 初始化队列
void initQueue(struct Queue* queue) {
    queue->front = -1;
    queue->rear = -1;
}

// 判空操作
int isEmpty(struct Queue* queue) {
    return (queue->front == -1);
}

// 判满操作
int isFull(struct Queue* queue) {
    return ((queue->rear + 1) % MAX_QUEUE_SIZE == queue->front);
}

// 入队操作
void enqueue(struct Queue* queue, int value) {
    if (isFull(queue)) {
        printf("队列已满,无法执行入队操作\n");
        return;
    }
    
    if (isEmpty(queue)) {
        queue->front = 0;
    }

    queue->rear = (queue->rear + 1) % MAX_QUEUE_SIZE;
    queue->data[queue->rear] = value;
}

// 出队操作
int dequeue(struct Queue* queue) {
    if (isEmpty(queue)) {
        printf("队列为空,无法执行出队操作\n");
        return -1; // 返回一个错误值
    }

    int dequeuedValue = queue->data[queue->front];
    
    if (queue->front == queue->rear) {
        // 队列中只有一个元素,出队后队列为空
        queue->front = -1;
        queue->rear = -1;
    } else {
        queue->front = (queue->front + 1) % MAX_QUEUE_SIZE;
    }
    
    return dequeuedValue;
}

int main() {
    struct Queue queue;
    initQueue(&queue);

    // 入队操作
    enqueue(&queue, 1);
    enqueue(&queue, 2);
    enqueue(&queue, 3);

    // 出队操作
    printf("出队操作: %d\n", dequeue(&queue));

    // 判空操作
    printf("队列是否为空: %s\n", isEmpty(&queue) ? "是" : "否");

    // 判满操作
    printf("队列是否满: %s\n", isFull(&queue) ? "是" : "否");

    return 0;
}

链式存储队列(单链表实现)

#include <stdio.h>
#include <stdlib.h>

// 定义链表节点
struct Node {
    int data;
    struct Node* next;
};

// 定义队列结构
struct Queue {
    struct Node* front; // 队列前端
    struct Node* rear;  // 队列后端
};

// 初始化队列
void initQueue(struct Queue* queue) {
    queue->front = NULL;
    queue->rear = NULL;
}

// 判空操作
int isEmpty(struct Queue* queue) {
    return (queue->front == NULL);
}

// 判满操作(对于链式存储的队列,通常不会满,所以返回0表示不满)
int isFull(struct Queue* queue) {
    return 0;
}

// 入队操作
void enqueue(struct Queue* queue, int value) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    if (newNode == NULL) {
        printf("内存分配失败,无法执行入队操作\n");
        return;
    }
    newNode->data = value;
    newNode->next = NULL;
    
    if (isEmpty(queue)) {
        queue->front = newNode;
    } else {
        queue->rear->next = newNode;
    }
    
    queue->rear = newNode;
}

// 出队操作
int dequeue(struct Queue* queue) {
    if (isEmpty(queue)) {
        printf("队列为空,无法执行出队操作\n");
        return -1; // 返回一个错误值
    }
    
    struct Node* temp = queue->front;
    int dequeuedValue = temp->data;
    
    queue->front = temp->next;
    free(temp);
    
    if (queue->front == NULL) {
        // 如果出队后队列为空,需要更新rear指针
        queue->rear = NULL;
    }
    
    return dequeuedValue;
}

// 释放队列内存
void freeQueue(struct Queue* queue) {
    while (queue->front != NULL) {
        struct Node* temp = queue->front;
        queue->front = temp->next;
        free(temp);
    }
}

int main() {
    struct Queue queue;
    initQueue(&queue);

    // 入队操作
    enqueue(&queue, 1);
    enqueue(&queue, 2);
    enqueue(&queue, 3);

    // 出队操作
    printf("出队操作: %d\n", dequeue(&queue));

    // 判空操作
    printf("队列是否为空: %s\n", isEmpty(&queue) ? "是" : "否");

    // 判满操作
    printf("队列是否满: %s\n", isFull(&queue) ? "是" : "否");

    // 释放队列内存
    freeQueue(&queue);

    return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1061311.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

金蝶OA server_file 目录遍历漏洞

漏洞描述 金蝶OA server_file 存在目录遍历漏洞&#xff0c;攻击者通过目录遍历可以获取服务器敏感信息 漏洞影响 金蝶OA 漏洞复现 访问漏洞url&#xff1a; 漏洞POC Windows服务器&#xff1a; appmonitor/protected/selector/server_file/files?folderC://&suffi…

RabbitMQ:高效可靠的消息队列解决方案

目录 引言&#xff1a;一、RabbitMQ 介绍二、核心概念三、工作原理四、应用场景五、案例实战 引言&#xff1a; 在现代分布式系统中&#xff0c;消息队列成为了实现系统间异步通信、削峰填谷以及解耦组件的重要工具。而RabbitMQ作为一个高效可靠的消息队列解决方案&#xff0c;…

《Attention Is All You Need》论文笔记

下面是对《Attention Is All You Need》这篇论文的浅读。 参考文献&#xff1a; 李沐论文带读 HarvardNLP 《哈工大基于预训练模型的方法》 下面是对这篇论文的初步概览&#xff1a; 对Seq2Seq模型、Transformer的概括&#xff1a; 下面是蒟蒻在阅读完这篇论文后做的一…

SimpleDateFormat非线程安全及解决方法

【RAEDME】 SimpleDateFormat非线程安全&#xff0c;即在多线程环境下解析字符串为日期对象&#xff0c;或格式化日期为字符串时&#xff0c;会抛出异常&#xff1b;当然&#xff0c;这是一个老生常谈的问题&#xff1b; 本文参考了已有 SimpleDateFormat的分析文章&#xff…

本地电脑搭建Web服务器并用cpolar发布至公网访问

本地电脑搭建Web服务器并用cpolar发布至公网访问 文章目录 本地电脑搭建Web服务器并用cpolar发布至公网访问前言1. 首先在电脑安装PHPStudy、WordPress、cpolar2. 安装cpolar&#xff0c;进入Web-UI界面3. 安装wordpress4. 进入wordpress网页安装程序5. 利用cpolar建立的内网穿…

回收站里面删除的照片如何恢复?

现在拍照已经成为人们生活中的一种方式&#xff0c;照片为我们保留了许多珍贵而美好的回忆。大家通常会把重要的照片保存在硬盘里&#xff0c;但当不小心把照片移入回收站并彻底删除时&#xff0c;情况就有点糟糕了。那么&#xff0c;回收站里删除的照片还有办法恢复吗&#xf…

leetcode每日一题复盘(10.2~10.8)

leetcode 347 前k个高频元素 关键词:堆排序,优先队列,小根堆 这道题真没想出来怎么做,只能想到哈希统计数目,对优先队列还不是很熟悉,后来看了详解自己重写了一遍 主要思路是用哈希统计每个元素出现次数,再利用优先队列的性质创建小根堆(优先队列默认是从大到小排序,将是一个…

Android 中级控件

目录 一、图形定制1.1 图形Drawable1.2 形状图形1.3 状态列表图形 二、选择按钮2.1 复选框2.2 开关按钮2.3 单选按钮 三、文本输入3.1 编辑框3.2 焦点变更监听器3.3 文本变化监听器 四、对话框4.1 提醒对话框4.2 日期对话框4.3 时间对话框 一、图形定制 1.1 图形Drawable ~~~~…

国庆放假作业5

1、qt实现TCP服务器和客户端 &#xff08;1&#xff09;服务器 头文件 #ifndef MAINWINDOW_H #define MAINWINDOW_H#include <QMainWindow> #include <QTcpServer> #include <QTcpSocket> #include <QList> #include <QMessageBox> #include …

新手学习Python用哪个软件比较好?

对于新手学习Python&#xff0c;有几个常用的集成开发环境&#xff08;IDE&#xff09;可以选择。以下是一些受欢迎的选择&#xff0c;可供题主参考下载使用。 集成开发环境&#xff08;IDE&#xff09; 1. PyCharm&#xff1a; PyCharm 是一款功能强大的 Python IDE&#x…

Vmware 静态网络配置

概述 仅主机模式&#xff08;VMware1&#xff09;&#xff1a;使用host-only的方式是不能和外界通信的&#xff0c;只能够和本机的物理网卡通信 桥接&#xff08;VMnet0&#xff09;&#xff1a;使用桥接的方式使得自己的虚拟机和自己的真实机网卡在同一个网段 NAT&#xff0…

穿越时空的创新:解析云原生与Web3.0的奇妙渊源

文章目录 云原生的崛起云原生的关键特点 Web3.0的崭露头角Web3.0的关键特点 云原生与Web3.0的交汇点1. 去中心化应用程序的部署2. 安全性和可信性3. 去中心化身份管理4. 数据的分布和共享 云原生与Web3.0的未来1. 云原生的区块链支持2. Web3.0的企业应用3. 数据交互的革命4. 新…

FFmpeg:打印音/视频信息(Meta信息)

多媒体文件基本概念 多媒体文件其实是个容器在容器里面有很多流(Stream/Track)每种流是由不同的编码器编码的从流中读出的数据称为包在一个包中包含着一个或多个帧 几个重要的结构体 AVFormatContextAVStreamAVPacket FFmpeg操作流数据的基本步骤 打印音/视频信息(Meta信息…

计算机考研 | 2021年 | 计算机组成原理真题

文章目录 【计算机组成原理2021年真题43题-15分】【第一步&#xff1a;信息提取】【第二步&#xff1a;具体解答】【第三步&#xff1a;总结】 【计算机组成原理2021年真题44题-8分】【第一步&#xff1a;信息提取】【第二步&#xff1a;具体解答】 【计算机组成原理2021年真题…

JavaScript实战游戏逻辑

● 在做猜游戏之前&#xff0c;我们肯定要随机生成一个数字。那就跟之前掷色子的游戏一样 const number Math.trunc(Math.random() * 20) 1; //生成一个1-20的随机数字● 之后&#xff0c;在输入正确的时候我们肯定需要在问号上面显示出来 ● 在测试阶段&#xff0c;我们可…

Java多线程同步锁、Lock锁和等待唤醒机制及代码演示

多线程入门学习路线 线程的生命周期线程的安全问题同步代码块同步方法Lock锁生产者和消费者&#xff08;等待唤醒机制&#xff09; 线程的生命周期 问&#xff1a;sleep方法会让线程睡眠&#xff0c;睡眠时间到了之后&#xff0c;立马就会执行下面的代码吗? 解&#xff1a;sl…

安装使用TinyCore Linux的一些收获

为了学习Linux Shell编程&#xff0c;决定安装一个纯粹的Linux&#xff0c;由于电脑硬件配置较低&#xff0c;选择了最轻量化Llinux操作系统版本TinyCore Linux。 一、TinyCore Linux有三个版本 打开TinyCore Linux的下载页面 http://www.tinycorelinux.net/downloads.html&a…

跨境电商测评内幕及自养号技术教学

现在测评行业的水越来越深了。几年前的测评行业都是邮箱联系老外&#xff0c;大多是一些产品的爱好者&#xff0c;评价也很真实公正。而现在&#xff0c;大量人加入&#xff0c;还有一些中介的参与&#xff0c;及骗子中介、黑心测评买家都纷纷的涌入了市场。 我们公司专业做底…

【计算机网络】高级IO初步理解

文章目录 1. 什么是IO&#xff1f;什么是高效 IO? 2. IO的五种模型五种IO模型的概念理解同步IO与异步IO整体理解 3. 阻塞IO4. 非阻塞IOsetnonblock函数为什么非阻塞IO会读取错误&#xff1f;对错误码的进一步判断检测数据没有就绪时&#xff0c;返回做一些其他事情完整代码myt…

好奇喵 | Tor浏览器——层层剥开洋葱

前言 在之前的博客中&#xff0c;Surface Web —&#xff1e; Deep Web —&#xff1e; Dark Web&#xff0c;我们解释了表层网络、深层网络等的相关概念。 本篇博客介绍Tor浏览器&#xff0c;并且把Tor浏览器的洋葱层层剥开&#xff0c;了解其历史&#xff0c;工作原理&…