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

9.3C++

思维导图

作业

自己封装 栈和队列

#include <iostream>using namespace std;
class mystack
{
private:int *data;int top;
public:mystack():data(NULL),top(-1){}~mystack(){delete[] data;}mystack & operator=(const mystack &R){if(this!=&R){delete []this->data;this->data=new int[R.top+1];for(int i=0;i<=R.top;i++){*(this->data+i)=*(R.data+i);}this->top=R.top;}return *this;}bool empty(){return top==-1;}int size(){return top+1;}bool push(int e){int *temp=new int[top+2];for(int i=0;i<=top;i++){*(temp+i)=*(data+i);}top++;*(temp+top)=e;if(top>0){delete []data;}data=temp;return true;}bool pop(){*(data+top)='\0';top--;return true;}void show(){for(int i=top;i>-1;i--){cout<<"第"<<i<<"个元素为:"<<data[i]<<endl;}}
};
int main()
{mystack a;a.push(3);a.show();a.push(4);a.show();a.push(2);a.show();a.push(1);a.show();mystack b;b.push(4);b.push(7);b.show();cout<<b.size()<<endl;b=a;b.show();cout<<b.size()<<endl;return 0;
}

队列

#include <iostream>using namespace std;
class myqueue
{
private:int* data;int front;int tail;
public:myqueue():data(NULL),front(0),tail(0){}~myqueue(){delete[]data;}myqueue & operator=(const myqueue &R){delete []this->data;this->data=new int[R.tail+1];for(int i=0;i<=R.tail;i++){*(this->data+i)=*(R.data+i);}this->tail=R.tail;this->front=R.front;return *this;}int &Front(){return *(data+front);}int &Tail(){return *(data+tail-1);}bool empty(){return front==tail;}int size(){return tail-front;}bool push(int e){int *temp=new int[tail+2];for(int i=0;i<tail;i++){*(temp+i)=*(data+i);}*(temp+tail)=e;tail++;if(tail>1){delete[]data;}data=temp;return true;}bool pop(){*(data+front)='\0';front++;return true;}void show(){for(int i=front;i<tail;i++){cout<<"第"<<i<<"个元素为:"<<data[i]<<endl;}}
};
int main()
{myqueue a;a.push(2);a.show();a.push(4);a.push(7);a.show();a.pop();a.show();a.pop();a.show();a.push(9);a.show();cout<<a.size()<<endl;a.Front()=1;a.show();a.Tail()=10;a.show();return 0;
}


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

相关文章:

  • 【深入了解常用类】
  • Java笔试面试题AI答之正则表达式(2)
  • 【iOS】暑期学习总结
  • Spring 学习笔记
  • 探索 Nuxt Devtools:功能全面指南
  • Vue3 中的响应式系统:深入理解 Proxy API
  • LeetCode LCR088.使用最小花费爬楼梯
  • wget下载速度受到哪些因素影响?
  • 揭秘Java后端框架:从Spring到Netty,全方位解析!
  • 【归并分而治之】逆序对的应对之策
  • KTH5641 系列具有模拟输出的比例式线性霍尔效应传感器
  • 【Qt】消息对话框 QMessageBox
  • 7.Lab Six —— Cow Fork
  • 6. MyBatis中的@Mapper注解和XML映射文件的区别是什么?
  • 【重构获得模式 Refactoring to Patterns】
  • 屏幕像素初步认识
  • 【非常简单】 猿人学web第一届 第17题 天杀的 Http2.0
  • 最新2024年国际EI会议集合
  • C语言从头学55——学习头文件errno.h、float.h
  • c++引用和指针