思维导图:![](https://i-blog.csdnimg.cn/blog_migrate/16bc123b542a0e5d9286c0ae0bdd17be.png)
封装顺序栈:
stack.h
#ifndef STACK_H
#define STACK_H
#include <iostream>
using namespace std;
#define MAX 5
typedef int datatype;
class Stack{
public:
//构造函数
Stack();
//析构函数
~Stack();
//拷贝构造函数
Stack(const Stack &other);
//入栈
int push(datatype e);
//出栈
int pop();
//清空栈
void clear();
//判空
bool empty();
//判满
bool full();
//获取栈顶元素
datatype topdata();
//求栈的大小
int size();
private:
datatype *data;
int top;
};
#endif // STACK_H
stack.cpp
#include"stack.h"
//构造函数
Stack::Stack():data(new datatype[MAX]),top(-1){
cout<<"构造函数"<<endl;
}
//析构函数
Stack::~Stack(){
delete []data; //释放指针空间
cout<<"析构函数"<<endl;
}
//拷贝构造函数
Stack::Stack(const Stack &other):data(new datatype[MAX]),top(other.top){
std::copy(other.data,other.data+MAX,data);
cout<<"拷贝函数"<<endl;
}
//入栈
int Stack::push(datatype e){
if(Stack::full()){
cout<<"栈满,无法入栈;"<<endl;
return 0;
}
data[++top]=e;
cout<<data[top]<<"已入栈"<<endl;
return 1;
}
//出栈
int Stack::pop(){
if(Stack::empty()){
cout<<"栈空,无法出栈;"<<endl;
return 0;
}
cout<<data[top--]<<"已出栈"<<endl;
return 1;
}
//清空栈
void Stack::clear(){
cout<<"****清空栈****"<<endl;
while(!Stack::empty()){
Stack::pop();
}
}
//判空
bool Stack::empty(){
return -1 == top;
}
//判满
bool Stack::full(){
return MAX-1 == top;
}
//获取栈顶元素
datatype Stack::topdata(){
cout<<"栈顶元素为:";
return data[top];
}
//求栈的大小
int Stack::size(){
cout<<this<<"栈的大小为:";
return top+1;
}
main.cpp
#include"stack.h"
int main()
{
Stack s;
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
s.push(6);
cout<<s.size()<<endl;
Stack s2=s;
cout<<s2.size()<<endl;
Stack s3=s2;
cout<<s3.size()<<endl;
s3.pop();
s3.pop();
s3.pop();
cout<<s3.topdata()<<endl;
s3.clear();
s3.pop();
cout<<s3.size()<<endl;
cout<<s.size()<<endl;
return 0;
}
效果图
![](https://i-blog.csdnimg.cn/blog_migrate/9028f04f0f1b73e8ae09ecc8d935e33d.png)
封装循环队列:
queue.h
#ifndef QUEUE_H
#define QUEUE_H
#include <iostream>
typedef int datatype;
#define MAX 5
using namespace std;
class Queue{
public:
//构造函数
Queue();
//析构函数
~Queue();
//拷贝构造函数
Queue(const Queue &other);
//入队
int push(datatype e);
//出队
int pop();
//清空队列
void clear();
//判空
bool empty();
//判满
bool full();
//求队列大小
int size();
private:
int front;
int tail;
datatype *data;
};
#endif // QUEUE_H
queue.cpp
#include"queue.h"
//构造函数
Queue::Queue():front(0),tail(0),data(new datatype[MAX])
{
cout<<"构造函数"<<endl;
}
//析构函数
Queue::~Queue(){
delete []data;
cout<<"析构函数"<<endl;
}
//拷贝构造函数
Queue::Queue(const Queue &other):front(other.front),
tail(other.tail),
data(new datatype[MAX]){
std::copy(other.data,other.data+MAX,data);
cout<<"拷贝构造函数"<<endl;
}
//入队
int Queue::push(datatype e){
if(Queue::full()){
cout<<"队满,无法入队;"<<endl;
return 0;
}
data[tail]=e;
cout<<e<<"已入队"<<endl;
tail=(tail+1)%MAX;
return 1;
}
//出队
int Queue::pop(){
if(Queue::empty()){
cout<<"队空,无法出队;"<<endl;
return 0;
}
cout<<data[front]<<"已出队"<<endl;
front=(front+1)%MAX;
return 1;
}
//清空队列
void Queue::clear(){
cout<<"****清空队列****"<<endl;
while(!Queue::empty()){
Queue::pop();
}
}
//判空
bool Queue::empty(){
return front==tail;
}
//判满
bool Queue::full(){
return (tail+1)%MAX==front;
}
//求队列大小
int Queue::size(){
cout<<this<<"队列大小为:";
return (tail-front+MAX)%MAX;
}
main.cpp
#include"queue.h"
int main()
{
Queue q;
q.push(1);
q.push(2);
q.push(3);
q.push(4);
q.push(5);
cout<<q.size()<<endl;
Queue q2=q;
cout<<q2.size()<<endl;
Queue q3=q2;
cout<<q2.size()<<endl;
q2.clear();
q2.pop();
cout<<q2.size()<<endl;
cout<<q.size()<<endl;
return 0;
}
效果图
![](https://i-blog.csdnimg.cn/blog_migrate/cc59e9743752726f6e6d036ae8a6d5f9.png)