1.封装栈
#include <iostream>
using namespace std;
class myStack
{
private:
int size; // 大小
int capacity;
int *ptr;
int top; // 栈顶下标
public:
// 无参构造函数
myStack():size(0), top(-1), capacity(10) {ptr = new int[capacity];}
// 有参构造函数
myStack(int c):size(0), top(-1), capacity(c) {ptr = new int[capacity];}
// 析构函数
~myStack() {delete []ptr;}
// 拷贝赋值函数
myStack& operator=(myStack& other);
// 判空
bool empyt();
// 判满
bool full();
// 访问栈顶元素
int& get_top();
// 返回容纳的元素数
int get_size();
// 向栈顶插入元素
bool push(int e);
// 删除栈顶元素
bool pop();
};
// 拷贝赋值函数
myStack& myStack::operator=(myStack& other)
{
delete []ptr;
this->size = other.size;
this->capacity = other.capacity;
this->top = other.top;
this->ptr = new int[capacity];
memcpy(this->ptr, other.ptr, this->capacity);
return *this;
}
// 判空
bool myStack::empyt()
{
return top == -1;
}
// 判满
bool myStack::full()
{
return top == capacity-1;
}
// 访问栈顶元素
int& myStack::get_top()
{
return ptr[top];
}
// 返回容纳的元素数
int myStack::get_size()
{
return size;
}
// 向栈顶插入元素
bool myStack::push(int e)
{
if(full())
{
return false;
}
top++;
ptr[top] = e;
size++;
return true;
}
// 删除栈顶元素
bool myStack::pop()
{
if(empyt())
{
return false;
}
top--;
size--;
return true;
}
int main()
{
myStack S1(5);
for(int i=0; i<10; i++)
{
S1.push(i+1);
cout << "size = " << S1.get_size() << endl;
if(!S1.empyt())
{
cout << "S1[top] = " << S1.get_top() << endl;
}
}
cout << "--------------------------" << endl;
myStack S2 = S1;
cout << "S2[top] = " << S2.get_top() << endl;
for(int i=0; i<10; i++)
{
S2.pop();
cout << "size = " << S2.get_size() << endl;
if(!S2.empyt())
{
cout << "S2[top] = " << S2.get_top() << endl;
}
}
cout << "--------------------------" << endl;
return 0;
}
2.封装队列
#include <iostream>
using namespace std;
class myQueue
{
private:
int front; // 队首
int back; // 队尾
int size; // 大小
int capacity;// 容量
int *ptr;
public:
// 无参构造
myQueue():front(0), back(0), size(0), capacity(10) {ptr = new int[capacity];}
// 有参构造
myQueue(int c):front(0), back(0), size(0), capacity(c) {ptr = new int[capacity];}
// 析构函数
~myQueue() {delete []ptr;}
// 拷贝赋值函数
myQueue& operator=(myQueue& other);
// 判空
bool empty();
// 判满
bool full();
// 访问队首元素
int& get_front();
// 访问队尾元素
int& get_back();
// 返回容纳的元素数
int get_size();
// 向队尾插入元素
bool push(int e);
// 删除队首元素
bool pop();
};
// 拷贝赋值函数
myQueue& myQueue::operator=(myQueue& other)
{
delete []ptr;
this->size = other.size;
this->front = other.front;
this->back = other.back;
this->capacity = other.capacity;
this->ptr = new int[this->capacity];
memcpy(this->ptr, other.ptr, this->capacity);
return *this;
}
// 判空
bool myQueue::empty()
{
return front == back;
}
// 判满
bool myQueue::full()
{
return (back+1)%capacity == front;
}
// 访问队首元素
int& myQueue::get_front()
{
return ptr[front];
}
// 访问队尾元素
int& myQueue::get_back()
{
return ptr[back-1];
}
// 返回容纳的元素数
int myQueue::get_size()
{
return size;
}
// 向队尾插入元素
bool myQueue::push(int e)
{
if(full())
{
return false;
}
ptr[back] = e;
back = (back+1)%capacity;
size++;
return true;
}
// 删除队首元素
bool myQueue::pop()
{
if(empty())
{
return false;
}
front = (front+1)%capacity;
size--;
return true;
}
int main()
{
myQueue q1(5);
cout << "size = " << q1.get_size() << endl;
cout << "--------------------------" << endl;
q1.push(666);
cout << "q1[front] = " << q1.get_front() << endl;
cout << "q1[back] = " << q1.get_back() << endl;
cout << "size = " << q1.get_size() << endl;
cout << "--------------------------" << endl;
q1.push(777);
q1.push(888);
q1.push(999);
q1.push(111); // 入队失败
cout << "q1[front] = " << q1.get_front() << endl;
cout << "q1[back] = " << q1.get_back() << endl;
cout << "size = " << q1.get_size() << endl;
cout << "--------------------------" << endl;
q1.pop();
cout << "q1[front] = " << q1.get_front() << endl;
cout << "q1[back] = " << q1.get_back() << endl;
cout << "size = " << q1.get_size() << endl;
cout << "--------------------------" << endl;
myQueue q2 = q1;
cout << "q2[front] = " << q2.get_front() << endl;
cout << "q2[back] = " << q2.get_back() << endl;
cout << "size = " << q2.get_size() << endl;
cout << "--------------------------" << endl;
return 0;
}
思维导图: