栈和队列的基本操作

news/2025/2/7 4:39:20/

(一)实验类型:设计性

(二)实验目的:

      1.掌握栈和队列的抽象数据类型。

2.掌握实现栈和队列的各种操作的算法。

3.理解栈与递归的关系。

4. 掌握队列的链式存贮结构及基本操作,深入了解链队列的基本特性,以便在实际问题背景下灵活运用它们。

(三)实验内容:

1. 栈和队列的数据结构定义;

2. 栈的建立、初始化、判空、入栈、出栈等操作。

  3. 队列的建立、初始化、判空、入队、出队等操作。

//1. 栈和队列的数据结构定义:// 栈的数据结构定义
typedef struct {int top;             // 栈顶指针int capacity;        // 栈容量int* array;          // 栈数据
} Stack;// 队列的数据结构定义
typedef struct {int front;           // 队首指针int rear;            // 队尾指针int capacity;        // 队列容量int* array;          // 队列数据
} Queue;//在C / C++中,数组名本质上是一个指向数组首元素的指针。
//因此,当使用 int* array; 声明一个指针时,array指向的是一个int类型的内存空间,
//它可以被用来存储整数数据,也可以被当做数组使用。//2. 栈的建立、初始化、判空、入栈、出栈等操作:// 创建一个新的栈
Stack * createStack(int capacity) {Stack* stack = (Stack*)malloc(sizeof(Stack));stack->capacity = capacity;stack->top = -1;stack->array = (int*)malloc(capacity * sizeof(int));//需要内存的访问,要访问容量才能设置合理的内存空间return stack;
}// 判断栈是否为空
int isStackEmpty(Stack* stack) {return stack->top == -1;
}// 判断栈是否已满
int isStackFull(Stack* stack) {return stack->top == stack->capacity - 1;
}
//从-1开始,自然要-1// 入栈
void push(Stack* stack, int item) {if (isStackFull(stack)) {printf("栈已满,无法入栈。\n");return;}stack->array[++stack->top] = item;// stack->top是一个整体,表示栈顶//然后才是++,表示插入下一个位置printf("%d 入栈成功。\n", item);
}// 出栈
int pop(Stack* stack) {if (isStackEmpty(stack)) {printf("栈为空,无法出栈。\n");return -1;}int item = stack->array[stack->top--];//只需要把Top数减一即可printf("%d 出栈成功。\n", item);return item;
}// 清空栈
void clearStack(Stack* stack) {stack->top = -1;
}// 销毁栈
void destroyStack(Stack* stack) {free(stack->array);free(stack);
}
```//3. 队列的建立、初始化、判空、入队、出队等操作:// 创建一个新的队列
Queue * createQueue(int capacity) {Queue* queue = (Queue*)malloc(sizeof(Queue));queue->capacity = capacity;queue->front = queue->rear = -1;queue->array = (int*)malloc(capacity * sizeof(int));return queue;
}// 判断队列是否为空
int isQueueEmpty(Queue* queue) {return queue->front == -1;
}// 判断队列是否已满
int isQueueFull(Queue* queue) {return (queue->rear + 1) % queue->capacity == queue->front;}// 入队
void enqueue(Queue* queue, int item) {if (isQueueFull(queue)) {printf("队列已满,无法入队。\n");return;}if (isQueueEmpty(queue)) {queue->front = queue->rear = 0;}else {queue->rear = (queue->rear + 1) % queue->capacity;//分配一个位置给新的数据}queue->array[queue->rear] = item;printf("%d 入队成功。\n", item);
}// 出队
int dequeue(Queue* queue) {if (isQueueEmpty(queue)) {printf("队列为空,无法出队。\n");return -1;}int item = queue->array[queue->front];//队头出队if (queue->front == queue->rear) //相等时,说明队列中仅有一个元素,所以出队完该元素后就直接设置为队空即可{queue->front = queue->rear = -1;}else {queue->front = (queue->front + 1) % queue->capacity;//直接替换成下一个front}printf("%d 出队成功。\n", item);return item;
}// 清空队列
void clearQueue(Queue* queue) {queue->front = queue->rear = -1;
}// 销毁队列
void destroyQueue(Queue* queue) {free(queue->array);free(queue);
}


http://www.ppmy.cn/news/1139862.html

相关文章

机器学习笔记 - 两个静态手势识别的简单示例

一、关于手势识别 手势识别方法通常分为两类:静态或动态。 静态手势是那些只需要在分类器的输入处处理单个图像的手势,这种方法的优点是计算成本较低。动态手势需要处理图像序列和更复杂的手势识别方法。 进一步了解可以参考下面链接。 静态手势识别和动态手势识别的区别和技…

JSON文件读写

1、依赖文件 #include <QFile> #include <QJsonDocument> #include <QJsonObject> #include <QDebug> #include <QStringList>2、头文件 bool ReadJsonFile(const QString& filePath""); bool WriteJsonFile(const QString&…

2023牛客暑假多校第五场(补题向题解:C,E)

当时只做出来4个题&#xff0c;被两个学弟的队伍压了一题&#xff0c;可能是因为两个中等题都偏向构造和猜结论这种&#xff0c;我们队伍不太擅长&#xff0c;需要加强这方面的训练。 C Cheeeeen the Cute Cat&#xff08;图论&#xff09; 说实话不看题解&#xff0c;还是很…

网络相关的基础知识整理

一、历史 1.1 早期阿帕网特点⭐⭐⭐ 没有纠错功能不能互联不同类型的计算机和不同类型的操作系统 1. 2 TCP/IP协议 点击【此处】跳转&#x1f517; TCP&#xff1a;用来检测网络传输中差错的传输控制协议IP&#xff1a;专门负责对不同网络进行互联的互联网协议&#xff08…

Leetcode.188 买卖股票的最佳时机 IV

题目链接 Leetcode.188 买卖股票的最佳时机 IV hard 题目描述 给你一个整数数组 p r i c e s prices prices 和一个整数 k k k &#xff0c;其中 p r i c e s [ i ] prices[i] prices[i] 是某支给定的股票在第 i i i 天的价格。 设计一个算法来计算你所能获取的最大利润。…

阿里云服务器ECS经济型e实例租用价格表

阿里云服务器e系列优惠价格&#xff1a;2核2G配置182元一年、2核4G配置365元一年、2核8G配置522元一年&#xff0c;e系列即ECS经济型e实例&#xff0c;CPU采用Intel Xeon Platinum可扩展处理器&#xff0c;系统盘支持ESSD Entry云盘&#xff08;推荐&#xff09;、ESSD云盘、ES…

Android12 OTA编译差分包报错问题

前言 在Ubuntu 20.04.4 LTS系统中编译Android12 OTA差分包的时候提示如下报错log: Warning: releasetools script should be invoked as hermetic Python executable -- build and run ota_from_target_files directly. Traceback (most recent call last):File "./bu…

文件上传笔记

一、上传的简单绕过&#xff1a; 1、若是上传的文件只在前端的代码中进行了过滤&#xff1a; &#xff08;1&#xff09;可以直接在开发者工具中删除相关代码&#xff1a; &#xff08;2&#xff09;也可以通过 burpsuite 绕过: 上传时&#xff0c;先提前修改 php 文件的后缀…