栈
#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;
}
/****************容量******************/
//empty
bool empty() const
{
return top < 0; // 检查栈是否为空
}
//size
int size() const
{
return top + 1; // 返回栈的大小
}
/***************修改器******************/
//push
void push(int value)
{
if (top >= capacity - 1)
{
throw std::overflow_error("栈溢出"); // 栈满
}
data[++top] = value; // 入栈
}
//pop
void pop()
{
if (top < 0)
{
throw std::underflow_error("栈为空"); // 栈空
}
--top; // 出栈
}
//swap
void 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;
}
/****************容量******************/
// empty
bool empty() const
{
return count == 0; // 检查队列是否为空
}
// size
int 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;
}