#include <iostream>
using namespace std;
template <typename T>
class My_stack{
private:
T *ptr; //指向堆区空间
int size; //栈大小
int top; //记录栈顶元素下标
public:
My_stack<T>() : ptr(new T[10]), top(-1) {} //有参构造
My_stack<T>(int size) : ptr(new T[size]), size(size), top(-1) {} //无参构造
~My_stack<T>() { //析构函数
delete []ptr;
}
//拷贝赋值
My_stack<T> &operator = (const My_stack<T> &other){
if(this == &other){
return *this;
}
delete []ptr;
this->size = other.size;
this->top = other.top;
this->ptr = new T[size];
for(int i = 0; i < top + 1; i++){
ptr[i] = other.ptr[i];
}
return *this;
}
bool empty(){ //判空
return this->top == -1;
}
bool full(){ //判满
return this->top == size - 1;
}
int getsize(){ //容纳的元素数量
return this->top + 1;
}
//入栈
void push(T data){
if(full()){
cout << "栈满,无法继续入栈!" << endl;
return;
}
ptr[++top] = data; //先加后压
cout << "入栈成功!" << endl;
}
//出栈
T pop(){
if(empty()){
cout << "栈空,无法继续出栈!" << endl;
return -9999;
}
T data = ptr[top--]; //先弹后减
return data;
}
//访问栈顶元素
T gettop(){
return ptr[top];
}
//遍历栈内元素
void output(){
if(empty()){
cout << "栈空,栈内无元素!" << endl;
return;
}
cout << "栈内元素为:";
for(int i = 0; i < top + 1; i++){
cout << ptr[i] << " ";
}
cout << endl;
}
};
int main()
{
int n, data;
char c;
cout << "请输入栈的大小: ";
cin >> n;
My_stack<int> s1(n); //实例化类对象 有参构造
//入栈
do{
cout << "请输入入栈元素:" << endl;
cin >> data;
s1.push(data);
cout << "你还需要继续入栈吗? Y/N" << endl;
cin >> c;
}while(c == 'Y' || c == 'y');
//出栈
cout << "你需要出栈吗? Y/N" << endl;
cin >> c;
int s;
while(c == 'Y' || c == 'y'){
s = s1.pop();
if(s == -9999){
cout << "栈空,无法继续出栈" << endl;
}else{
cout << "出栈成功,出栈元素为:" << s << endl;
}
cout << "你还需要继续出栈吗? Y/N" << endl;
cin >> c;
}
s1.output(); //遍历栈内元素
cout << "栈内容纳元素数量为:" << s1.getsize() << endl;
cout << "栈顶元素为:" << s1.gettop() << endl;
cout << "*****************************" << endl;
My_stack<int> s2;
s2 = s1; //拷贝赋值
s2.output();
return 0;
}
#include <iostream>
using namespace std;
template <typename T>
class Lqueue{
private:
T *ptr; //指向堆区空间
int size; //队列大小
int front; //队头坐标
int rear; //队尾记号
public:
Lqueue<T>() : ptr(new T[10]), front(0), rear(0) {} //无参构造
Lqueue<T>(int size) : ptr(new T[size]), size(size), front(0), rear(0) {} //有参构造
~Lqueue<T>(){
delete []ptr;
}
//拷贝赋值
Lqueue<T> &operator = (const Lqueue &other){
if(this == &other){
return *this;
}
delete []ptr;
this->size = other.size;
this->ptr = new T[size];
this->front = other.front;
this->rear = other.rear;
for(int i = front; i != rear; i = (i + 1) % size){
this->ptr[i] = other.ptr[i];
}
return *this;
}
//判空
bool empty(){
return front == rear;
}
//判满
bool full(){
return front == (rear + 1) % size;
}
//容纳元素数量
int getsize(){
return (size + rear - front) % size;
}
//入队
void enqueue(T data){
if(full()){
cout << "队满,无法继续入队!" << endl;
return;
}
ptr[rear] = data; //队尾入队
rear = (rear + 1) % size;
cout << "入队成功!" << endl;
}
//出队
T dequeue(){
if(empty()){
cout << "队空,无法继续出队!" << endl;
return -9999;
}
T data = ptr[front]; //队头出队
front = (front + 1) % size;
return data;
}
//访问队头元素
T getfront(){
return ptr[front];
}
//访问队尾元素
T getrear(){
return ptr[rear - 1];
}
//遍历队内元素
void output(){
if(empty()){
cout << "队空,队内无元素!" << endl;
return;
}
cout << "队内元素为: ";
for(int i = front; i != rear; i = (i + 1) % size){
cout << ptr[i] << " ";
}
cout << endl;
}
};
int main()
{
int n, data;
char c;
cout << "请输入队的大小: ";
cin >> n;
Lqueue<int> l1(n); //实例化对象 有参构造
//入队
do{
cout << "请输入入队元素: ";
cin >> data;
l1.enqueue(data);
cout << "你还要继续入队吗? Y/N : ";
cin >> c;
}while(c == 'Y' || c == 'y');
//出队
cout << "你要出队吗? Y/N : ";
cin >> c;
int s;
while(c == 'Y' || c == 'y'){
s = l1.dequeue();
if(s == -9999){
cout << "队空,无法继续出队!" << endl;
}else{
cout << "出队成功,出队元素为:" << s << endl;
}
cout << "你还要继续出队吗? Y/N : ";
cin >> c;
}
l1.output(); //遍历队内元素
cout << "队内元素的个数为:" << l1.getsize() << endl;
cout << "队头元素为:" << l1.getfront() << endl;
cout << "队尾元素为:" << l1.getrear() << endl;
cout << "***************************" << endl;
Lqueue<int> l2;
l2 = l1; //拷贝赋值
l2.output();
return 0;
}
脑图
https://www.mubu.com/doc/2Sx5zY-uGsz