思维导图
顺序栈定义成模板类
/*
---------------------------------
@author:YoungZorn
created on 2023/7/22 16:23.
---------------------------------
*/
#include<iostream>
using namespace std;
template<typename T>
class my_stack {
private:
T *ptr; //执行堆区空间,栈首地址(数组首地址)
int top; //记录栈顶元素
public:
//构造函数
my_stack() : ptr(new T[10]), top(-1) {}
//析构函数
~my_stack() { delete[] ptr; }
//判空函数(empty)
bool isEmpty() {
if (top == -1 || ptr == nullptr) {
return true;
} else {
return false;
}
}
//判满函数(full)
bool isFull() {
if (top >= 9) {
return true;
} else {
return false;
}
}
//入栈函数(push)
bool push(T item) {
if (isFull()) {
cout << "Full stack" << endl;
return false;
}
ptr[++top] = item;
return true;
}
//出栈函数(pop)
T pop() {
if (isEmpty()) {
return false;
}
return ptr[top--];
}
//遍历栈
void output() {
if (isEmpty()) {
cout << "Empty stack" << endl;
return;
}
for (int i = 0; i <= top; i++) {
cout << ptr[i] << " ";
}
cout << endl;
}
//获取栈顶元素的引用(top)
T &getTop(){
if (isEmpty()){
throw "empty stack";
}
return ptr[top];
}
//返回栈中元素个数size
int size(){
if (isEmpty())
return 0;
return top;
}
//运算符重载 ( = )
my_stack & operator=(const my_stack & R){
if (this == &R){
return *this;
}
delete [] ptr;
//深拷贝栈
top = R.top;
ptr = new T[10];
for (int i = 0; i <= top; i++) {
ptr[i] = R.ptr[i];
}
return *this;
}
};
int main() {
my_stack<double> stack1;
stack1.push(1.3);
stack1.push(2.4);
stack1.push(3.1);
stack1.push(8.1);
stack1.push(5.1);
stack1.output();
stack1.pop();
stack1.output();
cout<<"top: "<<stack1.getTop()<<endl;
my_stack<double> stack2;
stack2.push(1.2);
stack2.push(1.1);
stack2.push(3.1);
stack2 = stack1;
stack2.output();
stack2.pop();
stack2.output();
cout<<"top: "<<stack2.getTop()<<endl;
return 0;
}
循环顺序队列定义成模板类
/*
---------------------------------
@author:YoungZorn
created on 2023/7/22 16:31.
---------------------------------
*/
#include<iostream>
using namespace std;
template<typename T>
class seqQueue{
private:
T *data;
int front;
int rear;
int MAXSIZE;
public:
//无参构造
seqQueue(){}
//有参构造
seqQueue(int M):front(0),rear(0),MAXSIZE(M+1){
data = new T(MAXSIZE); //调用类T的构造函数来创建一个T类型的对象
}
//析构函数
~seqQueue(){delete [] data;}
//判空
bool isEmpty(){
return front == rear;
}
//判满
bool isFull(){
return front == (rear+1)%MAXSIZE;
}
//出队(pop)
T dequeue(){
if (isEmpty())
return;
T temp = data[front];
front = (front+1)%MAXSIZE;
return temp;
}
//入队(push)
void enqueue(const T& temp){
if (isFull())
return;
data[rear] = temp;
rear = (rear+1)%MAXSIZE;
}
//输出
void output(){
if (isEmpty())
return;
for (int i = front; i < rear; i = (i+1)%MAXSIZE) {
cout<<"data["<<i<<"] = "<<" "<<data[i]<<endl;
}
}
//队列中元素的个数(size)
int elemCount(){
if (isEmpty())
return 0;
return (MAXSIZE-front+rear)%MAXSIZE;
}
//运算符重载 ( = )
seqQueue& operator=(const seqQueue &other){
if (this == &other)
return *this;
//释放当前对象的缓存
delete [] data;
//深拷贝循环队列
MAXSIZE = other.MAXSIZE;
front = other.front;
rear = other.rear;
data = new T(MAXSIZE);
int i = front;
while (i != rear){
data[i] = other.data[i];
i = (i+1)%MAXSIZE;
}
return *this;
}
//front访问第一个元素
T elemFront(){
if (isEmpty())
return T();
return data[front];
}
//back访问最后一个元素
T elemRear(){
if (isEmpty())
return T();
return data[rear-1];
}
};
int main(){
seqQueue<double> queue(5);
queue.enqueue(1.2);
queue.enqueue(2.1);
queue.enqueue(9.3);
queue.output();
seqQueue<double>queue2(5);
queue2.enqueue(4.1);
queue2.enqueue(5.9);
queue2.enqueue(6.2);
queue2 = queue;
queue2.output();
cout<<"queue:elemCount: "<<queue.elemCount()<<endl;
cout<<"queue2:elemCount: "<<queue2.elemCount()<<endl;
cout<<"queue-->front: "<<queue.elemFront()<<endl;
cout<<"queue-->rear: "<<queue.elemRear()<<endl;
return 0;
}