作业
自己封装 栈和队列
#include <iostream>using namespace std;class Stack
{
private:int *data;int top = -1;
public:Stack(){};~Stack(){delete(data);}//赋值const Stack operator=(int d){this->top++;this->data[top] = d;return *this;}//访问void stack_top(int n){if(empty()){return ;}cout<<"第"<<n<<"个元素为:"<<data[n]<<endl;}//判空bool empty(){return top == -1;}//容纳数量int size(Stack S){return S.top+1;}//栈顶插入void push(int d){top++;for(int i=top;i>0;i--){data[i] = data[i-1];}data[0] = d;}//栈顶删除void pop(){if(empty()){return ;}for(int i=0;i<top;i++){data[i] = data[i+1];}data[top] = 0;top--;}
};class Link
{
private:int* data;int front = 0;int tail = -1;
public:Link(){}~Link(){delete(data);}//赋值const Link operator=(int d){this->tail++;for(int i=tail;i>0;i--){data[i] = data[i-1];}this->data[front] = d;return *this;}//访问第一个元素void link_front(){if(empty()){return ;}cout<<"第1个元素为:"<<data[front]<<endl;}//访问最后一个元素void back(){if(empty()){return ;}cout<<"最后1个元素为:"<<data[tail]<<endl;}//判空bool empty(){return tail == -1;}//容纳数量int size(Link L){return L.tail+1;}//队尾插入void push(int d){tail++;data[tail] = d;}//队顶删除void pop(){if(empty()){return ;}for(int i=0;i<tail;i++){data[i] = data[i+1];}data[tail] = 0;tail--;}
};int main()
{return 0;
}