作业1=viod
作业2=作业1
1.栈
#include <iostream>
using namespace std;
template <typename T>
class Node
{
int size;//容器最大容量
int top;//栈顶元素下标
T *next;
public:
Node():size(8),top(-1){ next =new T[size];}
Node(T e);
//细狗函数
~Node(){delete next;}
//而被扩容
void two_size()
{
T *ptr=new T[size*2];
for(int i=0;i<=top;i++)
{
ptr[i]=next[i];
}
delete next;
next=ptr;
ptr=NULL;
size*=2;
}
//访问容器最大容纳量
int num()
{
return size;
}
//判空
bool empty()
{
return top==-1;
}
//判满
bool full()
{
return top==size-1;
}
//赋值运算
Node &operator=(T &a)
{
if(full())
{
two_size();
cout<<"二倍扩容"<<endl;;
}
top++;
next[top]=a;
return *this;
}
//移动赋值运算
Node &operator=(T &&a)
{
if(full())
{
two_size();
cout<<"二倍扩容"<<endl;;
}
top++;
next[top]=a;
return *this;
}
//便立
void show()
{
cout<<size<<endl;
for(int i=0;i<=top;i++)
{
cout<<next[i]<<" ";
}
cout<<endl;
}
//返回容器中的元素个数
int size_size()
{
return top+1;
}
//访问栈顶元素
Node top_top()
{
cout<<next[top]<<endl;
return next[top];
}
//向栈顶添加数据
void push(const T &a)
{
if(full())
{
two_size();
}
top++;
next[top]=a;
return ;
}
//懂得抖动
void push(const T &&a)
{
if(full())
{
two_size();
}
top++;
next[top]=a;
return ;
}
//删除栈顶元素
void pop()
{
if(empty())
{
cout<<"么得东西了老大"<<endl;
return ;
}
next[top]={0};
top--;
return ;
}
};
template <typename T>
Node<T>::Node(T e):size(8),top(0)
{
next =new T[8];
next[0]=e;
}
int main()
{
Node <double>s1;
Node <int>s2;
Node <string>s3;
cout<<s2.num()<<endl;
for(int i=0;i<=10;i++)
{
s2=i;
}
cout<<"容量为"<<s2.num()<<endl;
cout<<"栈顶的元素是";
s2.top_top();
s2.show();
s1=1.1;
cout<<"容器中元素个数为";
cout<<s2.size_size()<<endl;
s2.pop();
cout<<"容器中元素个数为";
cout<<s2.size_size()<<endl;
s2.show();
return 0;
}
2. 队列
#include <iostream>
using namespace std;
template <typename T>
class Node
{
int first;//队头
int tail;//队尾
int size;
T *next;
public:
Node():first(0),tail(0),size(8){next =new T[size];}
Node(T e):first(0),tail(1),size(8){next=new T[size];next[0]=e;}
void x()
{
cout<<first<<" "<<tail<<endl;
}
//细狗函数
~Node(){delete next;}
//判空
bool empty()
{
return first==tail;
}
//判满
bool full()
{
return (tail+1)%size==first;
}
//遍历
void show()
{
if(empty())
{
cout<<"判个der"<<endl;
return ;
}
for(int i=first;i!=tail;i=(i+1)%size)
{
cout<<next[i]<<" ";
}
cout<<endl;
}
//而被扩容
void two_size()
{
T *ptr=new T[size*2];
for(int i=first;i!=tail;i=(i+1)%size)
{
ptr[i]=next[i];
}
delete next;
next=ptr;
ptr=NULL;
size*=2;
}
//=赋值
Node& operator=(const T &a)
{
if(full())
{
cout<<"而被扩容!启动!!!"<<endl;//这个容器表好像不需要而被扩容吧
two_size();
}
next[tail]=a;
tail=(tail+1)%size;
return *this;
}
//真正的懂不需要说
Node& operator=(const T &&a)
{
if(full())
{
cout<<"而被扩容!启动!!!"<<endl;//这个容器表好像不需要而被扩容吧
two_size();
}
next[tail]=a;
tail=(tail+1)%size;
return *this;
}
//访问第一个元素
Node font()
{
if(empty())
{
cout<<"我没得选"<<endl;
return *this;
}
return next[first];
}
//访问队尾袁术
Node back()
{
if(empty())
{
cout<<"难办啊"<<endl;
return *this;
}
return next[tail];
}
//返回容纳得元素书
int num()
{
return (tail-first+size)%size;
}
//尾插
void push(const T a)
{
if(full())
{
cout<<"满了"<<endl;
return ;
}
next[tail]=a;
tail=(tail+1)%size;
return ;
}
//头删
void pop()
{
if(empty())
{
cout<<"山不动了"<<endl;
return ;
}
next[first]={0};
first=(first+1)%size;
return ;
}
//容器容量
int max_size()
{
return size;
}
};
int main()
{
Node<int>s1;
Node<double>s2=1.1;
Node<string>s3("kkk");
s2=2.2;
s2=3.3;
s2.show();
s3 ="hahaha";
s3.show();
s1 =1;
s1=2;
s1=3;
s1.x();
s1.show();
cout<<"容器中又"<<s1.num()<<"个元素"<<endl;
cout<<"容器最大容量为"<<s1.max_size()<<endl;
for(int i=0;i<=10;i++)
{
s1 =i;
}
s1.x();
s1.show();
cout<<"容器中又"<<s1.num()<<"个元素"<<endl;
cout<<"容器最大容量为"<<s1.max_size()<<endl;
s1.pop();
s1.show();
cout<<"容器中又"<<s1.num()<<"个元素"<<endl;
cout<<"容器最大容量为"<<s1.max_size()<<endl;
s1.push(1);
s1.x();
s1.show();
cout<<"容器中又"<<s1.num()<<"个元素"<<endl;
cout<<"容器最大容量为"<<s1.max_size()<<endl;
return 0;
}