作业:
1、手动实现栈
仿照stack类实现My_Stack,实现一个栈的操作
fun.h代码
#ifndef FUN_H
#define FUN_H
#include <iostream>
using namespace std;
class My_Stack {
private:
static const int MAX_SIZE = 100; // 定义栈的最大容量
int arr[MAX_SIZE]; // 数组用于存储栈元素
int top; // 栈顶索引
public:
// 构造函数
My_Stack():top(-1){}
// 析构函数
~My_Stack(){}
//拷贝构造函数
My_Stack(const My_Stack& other);
void push(int value); // 入栈操作
int pop(); // 出栈操作
int top1(); // 获取栈顶元素
bool empty(); // 判断栈是否为空
int size(); // 获取栈的当前大小
void swap_t(My_Stack& other);
void show(); // 显示栈中的内容
My_Stack& operator=(const My_Stack& other);
};
#endif // FUN_H
fun.cpp代码
#include"fun.h"
using namespace std;
//拷贝构造函数
My_Stack:: My_Stack(const My_Stack& other):top(other.top) {
for (int i = 0; i <= top; ++i) {
arr[i] = other.arr[i];
}
}
// 入栈操作
void My_Stack:: push(int value) {
if (top>=MAX_SIZE - 1) {
cout<<"full"<< value<<endl;
return;
}
arr[++top] = value; // 增加栈顶并赋值
cout<<"in: "<<value<<endl;
}
// 出栈操作
int My_Stack:: pop() {
if (empty()) {
cout << "empty" << endl;
return -1; // 返回 -1 作为错误指示
}
int pop = arr[top--]; // 返回栈顶值并减少栈顶索引
return pop;
}
// 获取栈顶元素
int My_Stack:: top1() {
if (empty()) {
cout << "empty" << endl;
return -1;
}
return arr[top]; // 返回栈顶值
}
// 判断栈是否为空
bool My_Stack:: empty() {
return top == -1; //则栈为空,栈顶索引为 -1
}
// 获取栈的当前大小
int My_Stack:: size() {
return top + 1; // 返回栈中元素的个数
}
void My_Stack::swap_t(My_Stack& other) {
// 交换栈顶索引
swap(top, other.top);
// 交换栈中的内容
for (int i = 0; i < MAX_SIZE; ++i) {
swap(arr[i], other.arr[i]);
}
}
// 显示栈中的内容
void My_Stack:: show() {
if (empty()) {
cout << "empty" << endl;
return;
}
for (int i = 0; i <= top; ++i) {
cout << arr[i] << " "; // 打印每个元素
}
cout << endl;
}
My_Stack& My_Stack:: operator=(const My_Stack& other){ // 赋值操作符重载
if (this != &other) { // 自我赋值检查
top = other.top; // 复制栈顶索引
for (int i = 0; i <= top; ++i) {
arr[i] = other.arr[i]; // 深拷贝
}
}
return *this; // 返回当前对象的引用
}
main.cpp代码
#include "fun.h"
int main() {
// 创建第一个栈并进行入栈操作
My_Stack s1;
s1.push(1);
s1.push(2);
s1.push(3);
// 创建第二个栈并进行入栈操作
My_Stack s2;
s2.push(4);
s2.push(5);
s2.push(6);
cout << "**********" << endl;
cout<<"s1:";
s1.show();
cout<<"s2:";
s2.show();
My_Stack s3 = s2;
cout<<"s3:";
s3.show();
// 交换s1和s2的内容
s1.swap_t(s2);
cout << "**********" << endl;
cout << " swap " << endl;
cout<<"s1:";
s1.show();
cout<<"s2:";
s2.show();
// 出栈
cout<<"s1 out:"<<s1.pop()<<endl;
cout<<"s2 out:"<<s2.pop()<<endl;
// 检查栈的大小
cout<<"s1.size: "<<s1.size()<<endl;
cout<<"s2.size: "<<s2.size()<<endl;
// 再次弹出元素
s1.pop();
s2.pop();
cout << "s1.size: " << s1.size() << endl;
cout << "s2.size: " << s2.size() << endl;
return 0;
}
运行结果:
2、手动实现队列
fun.h代码
#ifndef FUN_H
#define FUN_H
#include <iostream>
using namespace std;
class My_Queue {
private:
static const int MAX_SIZE = 100;
int arr[MAX_SIZE];
int fron;
int rear;
public:
// 构造函数
My_Queue():fron(0), rear(0) {}
// 析构函数
~My_Queue(){}
//拷贝构造函数
My_Queue(const My_Queue& other);
void push(int value); // 入队操作
int pop(); // 出队操作
int front(); // 访问第一个元素
int back(); // 访问最后一个元素
bool empty(); // 判断栈是否为空
int size(); // 获取队列的当前大小
void swap_t(My_Queue& other);
void show(); // 显示队列中的内容
My_Queue& operator=(const My_Queue& other);
};
#endif // FUN_H
fun.cpp代码
#include"fun.h"
using namespace std;
// 拷贝构造函数
My_Queue::My_Queue(const My_Queue& other) : fron(other.fron), rear(other.rear) {
for (int i = 0; i < size(); ++i) {
arr[i] = other.arr[i];
}
}
// 入队操作
void My_Queue::push(int value) {
if ((size() + 1) % MAX_SIZE == fron) { // 检查队列是否已满
cout << "full" << endl;
return;
}
arr[rear] = value;
rear = (rear + 1) % MAX_SIZE; // 循环队列尾部指针更新
}
// 出队操作
int My_Queue::pop() {
if (empty()) {
cout << "empty" << endl;
return -1;
}
int value = arr[fron];
fron = (fron + 1) % MAX_SIZE; // 循环队列头部指针更新
return value;
}
// 获取队首元素
int My_Queue::front() {
if (empty()) {
cout << "empty" << endl;
return -1;
}
return arr[fron];
}
// 获取队尾元素
int My_Queue::back() {
if (empty()) {
cout << "empty" << endl;
return -1;
}
int index = (rear == 0 ? MAX_SIZE : 1);
return arr[rear - index];
}
// 判断队列是否为空
bool My_Queue::empty() {
return fron == rear;
}
// 获取队列的当前大小
int My_Queue::size() {
return (rear - fron + MAX_SIZE) % MAX_SIZE;
}
// 交换两个队列的数据
void My_Queue::swap_t(My_Queue& other) {
swap(fron, other.fron);
swap(rear, other.rear);
swap(arr, other.arr);
}
// 显示队列内容
void My_Queue::show() {
if (empty()) {
cout << "empty" << endl;
return;
}
for (int i = fron; i != rear; i = (i + 1) % MAX_SIZE) {
cout << arr[i] << " ";
}
cout << endl;
}
// 赋值运算符重载
My_Queue& My_Queue::operator=(const My_Queue& other) {
if (this != &other) {
fron = other.fron;
rear = other.rear;
for (int i = 0; i < size(); ++i) {
arr[i] = other.arr[i];
}
}
return *this;
}
main.cpp代码
#include "fun.h"
int main() {
// 创建第一个队列并进行入队操作
My_Queue s1;
s1.push(1);
s1.push(2);
s1.push(3);
// 创建第二个队列并进行入队操作
My_Queue s2;
s2.push(4);
s2.push(5);
s2.push(6);
cout << "**********" << endl;
cout<<"s1:";
s1.show();
cout<<"s2:";
s2.show();
cout << s2.front() << endl;
cout << s2.back() << endl;
My_Queue s3 = s2;
cout<<"s3:";
s3.show();
// 交换s1和s2的内容
s1.swap_t(s2);
cout << "**********" << endl;
cout << " swap " << endl;
cout<<"s1:";
s1.show();
cout<<"s2:";
s2.show();
// 出队
cout<<"s1 out:"<<s1.pop()<<endl;
cout<<"s2 out:"<<s2.pop()<<endl;
// 检查队列的大小
cout<<"s1.size: "<<s1.size()<<endl;
cout<<"s2.size: "<<s2.size()<<endl;
// 再次弹出元素
s1.pop();
s2.pop();
cout << "s1.size: " << s1.size() << endl;
cout << "s2.empty: " << s2.empty() << endl;
return 0;
}
运行结果: