栈
#include <iostream>
#include <stdexcept>
using namespace std;class My_stack
{
private:int * data; //栈空间int capacity;int top; //栈顶元素的下标
protected:public:/******************成员函数*************///构造函数My_stack(int c = 10):capacity(c),top(-1){data = new int[capacity];cout<<"构造函数"<<endl;}//析构函数~My_stack(){delete []data;cout<<"析构函数"<<endl;}//operator= 赋值给容器适配器My_stack & operator=(const My_stack &R){this->data = R.data;this->top = R.top;return *this;}/*************元素访问******************///My_top 访问栈顶元素int My_top(){cout<<"My_top 访问栈顶元素"<<endl;if (this->top < 0){throw std::underflow_error("栈为空"); // 栈空}return data[this->top];}//show_top 显示栈顶元素void show_top(){cout<<"show_top 显示栈顶元素"<<endl;if (top < 0){throw std::underflow_error("栈为空"); // 栈空}cout<<data[top]<<endl;}void show(){cout<<"show 显示栈内所有元素"<<endl;if (this->top < 0){throw std::underflow_error("栈为空"); // 栈空}for(int i =0;i<=top;i++){cout<<data[i]<<" ";}cout<<endl;}/****************容量******************///emptybool empty() const{return top < 0; // 检查栈是否为空}//sizeint size() const{return top + 1; // 返回栈的大小}/***************修改器******************///pushvoid push(int value){if (top >= capacity - 1){throw std::overflow_error("栈溢出"); // 栈满}data[++top] = value; // 入栈}//popvoid pop(){if (top < 0){throw std::underflow_error("栈为空"); // 栈空}--top; // 出栈}//swapvoid swap(My_stack &other){//如果容量不足就报错if(this->top>other.capacity||other.top>this->capacity){throw std::underflow_error("栈数据交换失败"); // 栈空}for(int i = 0;i<=top;i++){int temp = this->data[i];this->data[i] = other.data[i];other.data[i] = temp;}int temp = this->top;this->top = other.top;other.top = temp;}};int main()
{My_stack s1;s1.push(10);s1.push(4);s1.push(6);s1.push(8);s1.My_top();s1.show_top();s1.show();return 0;
}
队列
#include <iostream>
#include <stdexcept>
using namespace std;class My_queue
{
private:int* data; // 队列空间int capacity; // 队列容量int front; // 队头元素的下标int rear; // 队尾元素的下标int count; // 当前元素数量public:/******************成员函数*************/// 构造函数My_queue(int c = 10) : capacity(c), front(0), rear(-1), count(0){data = new int[capacity];cout << "构造函数" << endl;}// 析构函数~My_queue(){delete[] data;cout << "析构函数" << endl;}// 赋值运算符重载My_queue& operator=(const My_queue& R){if (this != &R){delete[] data; // 释放旧内存capacity = R.capacity;front = R.front;rear = R.rear;count = R.count;data = new int[capacity];for (int i = 0; i < count; i++){data[i] = R.data[(front + i) % capacity];}}return *this;}/*************元素访问******************/// 队头元素int My_front(){cout << "My_front 访问队头元素" << endl;if (count == 0){throw std::underflow_error("队列为空"); // 队列空}return data[front];}// 显示队头元素void show_front(){cout << "show_front 显示队头元素" << endl;if (count == 0){throw std::underflow_error("队列为空"); // 队列空}cout << data[front] << endl;}// 显示所有元素void show(){cout << "show 显示队列内所有元素" << endl;if (count == 0){throw std::underflow_error("队列为空"); // 队列空}for (int i = 0; i < count; i++){cout << data[(front + i) % capacity] << " ";}cout << endl;}/****************容量******************/// emptybool empty() const{return count == 0; // 检查队列是否为空}// sizeint size() const{return count; // 返回队列的大小}/***************修改器******************/// 入队void enqueue(int value){if (count >= capacity){throw std::overflow_error("队列溢出"); // 队列满}rear = (rear + 1) % capacity; // 循环队列data[rear] = value; // 入队count++;}// 出队void dequeue(){if (count == 0){throw std::underflow_error("队列为空"); // 队列空}front = (front + 1) % capacity; // 循环队列count--;}
};int main()
{My_queue q;q.enqueue(10);q.enqueue(4);q.enqueue(6);q.enqueue(8);q.My_front();q.show_front();q.show();q.dequeue(); // 出队q.show(); // 显示队列内容return 0;
}