当前位置: 首页 > news >正文

2024.9.25 作业和思维导图

#include <iostream>
#include <stdexcept>
using namespace std;class My_stack
{
private:int * data; //栈空间int capacity;int top;    //栈顶元素的下标
protected:public:/******************成员函数*************///构造函数My_stack(int c = 10):capacity(c),top(-1){data = new int[capacity];cout<<"构造函数"<<endl;}//析构函数~My_stack(){delete []data;cout<<"析构函数"<<endl;}//operator= 赋值给容器适配器My_stack & operator=(const My_stack &R){this->data = R.data;this->top = R.top;return *this;}/*************元素访问******************///My_top   访问栈顶元素int My_top(){cout<<"My_top   访问栈顶元素"<<endl;if (this->top < 0){throw std::underflow_error("栈为空"); // 栈空}return data[this->top];}//show_top   显示栈顶元素void show_top(){cout<<"show_top   显示栈顶元素"<<endl;if (top < 0){throw std::underflow_error("栈为空"); // 栈空}cout<<data[top]<<endl;}void show(){cout<<"show   显示栈内所有元素"<<endl;if (this->top < 0){throw std::underflow_error("栈为空"); // 栈空}for(int i =0;i<=top;i++){cout<<data[i]<<" ";}cout<<endl;}/****************容量******************///emptybool empty() const{return top < 0; // 检查栈是否为空}//sizeint size() const{return top + 1; // 返回栈的大小}/***************修改器******************///pushvoid push(int value){if (top >= capacity - 1){throw std::overflow_error("栈溢出"); // 栈满}data[++top] = value; // 入栈}//popvoid pop(){if (top < 0){throw std::underflow_error("栈为空"); // 栈空}--top; // 出栈}//swapvoid swap(My_stack &other){//如果容量不足就报错if(this->top>other.capacity||other.top>this->capacity){throw std::underflow_error("栈数据交换失败"); // 栈空}for(int i = 0;i<=top;i++){int temp = this->data[i];this->data[i] = other.data[i];other.data[i] = temp;}int temp = this->top;this->top = other.top;other.top = temp;}};int main()
{My_stack s1;s1.push(10);s1.push(4);s1.push(6);s1.push(8);s1.My_top();s1.show_top();s1.show();return 0;
}

队列

#include <iostream>
#include <stdexcept>
using namespace std;class My_queue
{
private:int* data;      // 队列空间int capacity;   // 队列容量int front;      // 队头元素的下标int rear;       // 队尾元素的下标int count;      // 当前元素数量public:/******************成员函数*************/// 构造函数My_queue(int c = 10) : capacity(c), front(0), rear(-1), count(0){data = new int[capacity];cout << "构造函数" << endl;}// 析构函数~My_queue(){delete[] data;cout << "析构函数" << endl;}// 赋值运算符重载My_queue& operator=(const My_queue& R){if (this != &R){delete[] data; // 释放旧内存capacity = R.capacity;front = R.front;rear = R.rear;count = R.count;data = new int[capacity];for (int i = 0; i < count; i++){data[i] = R.data[(front + i) % capacity];}}return *this;}/*************元素访问******************/// 队头元素int My_front(){cout << "My_front   访问队头元素" << endl;if (count == 0){throw std::underflow_error("队列为空"); // 队列空}return data[front];}// 显示队头元素void show_front(){cout << "show_front   显示队头元素" << endl;if (count == 0){throw std::underflow_error("队列为空"); // 队列空}cout << data[front] << endl;}// 显示所有元素void show(){cout << "show   显示队列内所有元素" << endl;if (count == 0){throw std::underflow_error("队列为空"); // 队列空}for (int i = 0; i < count; i++){cout << data[(front + i) % capacity] << " ";}cout << endl;}/****************容量******************/// emptybool empty() const{return count == 0; // 检查队列是否为空}// sizeint size() const{return count; // 返回队列的大小}/***************修改器******************/// 入队void enqueue(int value){if (count >= capacity){throw std::overflow_error("队列溢出"); // 队列满}rear = (rear + 1) % capacity; // 循环队列data[rear] = value; // 入队count++;}// 出队void dequeue(){if (count == 0){throw std::underflow_error("队列为空"); // 队列空}front = (front + 1) % capacity; // 循环队列count--;}
};int main()
{My_queue q;q.enqueue(10);q.enqueue(4);q.enqueue(6);q.enqueue(8);q.My_front();q.show_front();q.show();q.dequeue(); // 出队q.show(); // 显示队列内容return 0;
}

 


http://www.mrgr.cn/news/36126.html

相关文章:

  • 线程安全的数据结构使用起来一定线程安全吗?
  • 将ipad作为数位板使用教程/出现延迟拖拽怎么办?
  • MySql 从入门到入门
  • 【笔记篇】一篇文章搞定Spring框架
  • WordPress LearnPress插件 SQL注入复现(CVE-2024-8522)
  • 每天分享一个FPGA开源代码(6)- 浮点数运算
  • 回归阅读第一本:《瓦尔纳宝典》
  • windows GetUserNameEx api使用c++
  • RTE 大会报名丨AI 时代新基建:云边端架构和 AI Infra ,RTE2024 技术专场第二弹!
  • visio 2021入门直通车(一天全搞定)
  • 二维环境下TDOA的MATLAB仿真代码(4个锚节点)
  • project modules模块消失,只显示module,不显示project
  • 深入理解包管理工具
  • 【Golang】Go语言中如何面向对象?
  • 二分查找及变体
  • shell配置文件介绍
  • 一个简单的个人博客管理平台适合新手学习(最底下有github链接)
  • 【多线程】面试高频考点!JUC常见类的详细总结,建议收藏!
  • add normal user to docker group
  • 信息安全工程师(17)密码体制分类