
栈stack.h
- #ifndef STACK_H
- #define STACK_H
- #include
- #define MAXSIZE 128
-
- using namespace std;
- class Stack
- {
- public:
- //构造函数
- Stack();
- //析构函数
- ~Stack();
- //拷贝构造函数
- Stack(const Stack &other);
- //入栈
- bool push(int value);
- //出栈并返回栈顶元素
- int pop();
- //清空栈
- bool clear();
- //判空
- bool isEmpty();
- //判满
- bool isFull();
- //获取栈顶元素
- int stacktop();
- //求栈的大小
- int stacksize();
- //输出栈元素
- bool show();
-
- private:
- int *stackData;
- int topIndex;
- };
-
- #endif // STACK_H
stack.cpp
- #include"stack.h"
-
- //构造函数
- Stack::Stack()
- {
- // 使用动态内存分配来创建堆上的栈
- stackData = new int[MAXSIZE];
- topIndex = -1; // 初始化栈顶索引为-1,表示栈为空
- }
-
- //析构函数
- Stack::~Stack()
- {
- delete [] stackData;
- }
-
- //拷贝构造函数
- Stack::Stack(const Stack &other)
- {
- topIndex = other.topIndex;
- stackData = new int[MAXSIZE];
-
- for (int i = 0; i <= topIndex; i++) {
- stackData[i] = other.stackData[i];
- }
- }
-
- //入栈
- bool Stack::push(int value)
- {
- if(isFull()){
- cout<<"该栈已满,无法插入"<
- return false;
- }else {
- topIndex++;
- stackData[topIndex] = value;
- return true;
- }
- }
-
- //出栈并返回栈顶元素
- int Stack::pop()
- {
- if(isEmpty()){
- cout<<"该栈为空"<
- return -1;
- }else {
- int topvalue = stackData[topIndex];
- topIndex--;
- return topvalue;
- }
- }
-
- //清空栈
- bool Stack::clear()
- {
- if(isEmpty()){
- cout<<"该栈为空"<
- return false;
- }else {
- topIndex = -1;
- return true;
- }
- }
-
- //判空
- bool Stack::isEmpty()
- {
- return topIndex == -1;
- }
-
- //判满
- bool Stack::isFull()
- {
- return topIndex == MAXSIZE-1;
- }
-
- //获取栈顶元素
- int Stack::stacktop()
- {
- return stackData[topIndex];
- }
-
- //求栈的大小
- int Stack::stacksize()
- {
- return topIndex+1;
- }
-
- //输出栈元素
- bool Stack::show()
- {
- if(isEmpty()){
- cout<<"该栈为空"<
- return false;
- }else {
- for(int i=0;i<=topIndex; i++){
- cout<
" "; - }
- cout<
- return true;
- }
- }
main.cpp
- #include"stack.h"
-
- int main()
- {
- Stack s1;
- s1.push(5);
- s1.push(5);
- s1.pop();
- s1.show();
- s1.~Stack();
- return 0;
- }
循环队列 loopqueue.h
- #ifndef LOOPQUEUE_H
- #define LOOPQUEUE_H
- #include
- #define MAXSIZE 16
-
- using namespace std;
- class LoopQueue
- {
- public:
- //构造函数
- LoopQueue();
-
- //析构函数
- ~LoopQueue();
-
- //拷贝构造函数
- LoopQueue(const LoopQueue &other);
-
- //入队列
- bool push(int value);
-
- //出队列并返回队列顶元素
- int pop();
-
- //清空队列
- bool clear();
-
- //判空
- bool isEmpty();
-
- //判满
- bool isFull();
-
- //求队列的大小
- int LoopQueuesize();
-
- //输出队列元素
- bool show();
-
- private:
- int *LoopQueueData;
- int FrontIndex;
- int RearIndex;
- };
- #endif // LOOPQUEUE_H
loopqueue.cpp
- #include"LoopQueue.h"
-
- //构造函数
- LoopQueue::LoopQueue()
- {
- LoopQueueData = new int[MAXSIZE];
- FrontIndex = 0;
- RearIndex = 0;
- }
-
- //析构函数
- LoopQueue::~LoopQueue()
- {
- delete [] LoopQueueData;
- }
-
- //拷贝构造函数
- LoopQueue::LoopQueue(const LoopQueue &other)
- {
- LoopQueueData = new int[MAXSIZE];
- int i=other.FrontIndex;
- while(1)
- {
- LoopQueueData[i] = other.LoopQueueData[i];
- i++;
- if(MAXSIZE==i)
- {
- i=0;
- }
- if(i==other.RearIndex)
- {
- break;
- }
- }
- FrontIndex = other.FrontIndex;
- RearIndex = other.RearIndex;
- }
-
- //入队列
- bool LoopQueue::push(int value)
- {
- if(isFull()){
- cout<<"该队列已满"<
- return false;
- }else{
- LoopQueueData[RearIndex] = value;
- RearIndex++;
- return true;
- }
- }
-
- //出队列并返回队列顶元素
- int LoopQueue::pop()
- {
- if(isEmpty()){
- cout<<"该队列为空"<
- return -1;
- }else {
- int Frontvalue = LoopQueueData[RearIndex];
- FrontIndex++;
- return Frontvalue;
- }
- }
-
- //清空队列
- bool LoopQueue::clear()
- {
- if(isEmpty()){
- cout<<"该队列为空"<
- return false;
- }else {
- FrontIndex = -1;
- RearIndex = -1;
- return true;
- }
- }
-
- //判空
- bool LoopQueue::isEmpty()
- {
- return FrontIndex==RearIndex;
- }
-
- //判满
- bool LoopQueue::isFull()
- {
- return (RearIndex+1)%MAXSIZE==FrontIndex;
- }
-
- //求队列的大小
- int LoopQueue::LoopQueuesize()
- {
- return (RearIndex-FrontIndex+MAXSIZE)%MAXSIZE;
- }
-
- //输出队列元素
- bool LoopQueue::show()
- {
- if(isEmpty()){
- cout<<"该队列为空"<
- return false;
- }else {
- int i=FrontIndex;
- cout<<"该队列为>>";
- while(1)
- {
- cout<
" "; - i++;
- if(MAXSIZE==i)
- {
- i=0;
- }
- if(i==RearIndex)
- {
- break;
- }
- }
- cout<
- return true;
- }
- }
main.cpp
- #include"LoopQueue.h"
-
- using namespace std;
-
- int main()
- {
- LoopQueue l1;
- /*int value = -1;
- while(1){
- cout<<"请输入要插入的值>>> "<
- cin>>value;
- if(-1==value){
- break;
- }else{
- l1.push(value);
- }
- }*/
- l1.push(23);
- l1.push(44);
- l1.push(39);
- l1.pop();
- l1.show();
- cout<<"队列大小为>>"<
LoopQueuesize()< - return 0;
- }
-
相关阅读:
ESP8266 WiFi物联网智能插座—上位机和下位机通信协议
如何对用户输入进行校验
前端,CSS,背景颜色跟随轮播图片改变而改变(附源码)
提供CY系列菁染料CY3、CY5、CY5.5、CY7、CY7.5,ICG,荧光素FITC,Bodipy系列染料标记海藻酸钠Alginate
练习8:多重子查询
2022年大一学生实训作业【基于HTML+CSS制作中华传统文化传统美德网站 (6页面)】
Windows 安装 chromedriver 和 Python 调试
Java 7 生命周期结束
91. 存钱罐
【Hive SQL】统计同名路径下目录数量(基于reverse、split和substr函数)
-
原文地址:https://blog.csdn.net/mcslll/article/details/132799537