自己写的 :
const int N=1e5;
template<typename T>
class mystack
{
private:
T A[N];
int tot=0;
public:
bool push(T i)
{
if(tot+1>N) return false;
A[++tot]=i;
return true;
}
bool pop()
{
if(tot-1) return false;
tot--;
return true;
}
int size(){return tot;}
bool empty(){return !tot;}
T top(){return A[tot];}
};代版本:
template<typename T>
class mystack
{
private:
struct pointer
{
T val;
pointer *ne=nullptr;
}*head=nullptr;
size_t tot=0;
public:
~mystack()
{
while (!empty())
pop();
}
void push(T i)
{
pointer*new_push=new pointer;
new_push->val=i;
new_push->ne=head;
head=new_push;
++tot;
}
bool pop()
{
if(!tot) return false;
head=head->ne;
--tot;
return true;
}
int size(){return tot;}
bool empty(){return !tot;}
T top(){return head->val;}
};自己写的 :
template<typename T>
class myqueue
{
private:
struct Node
{
T data;
Node*next=NULL;
Node(const T&value):data(value),next(NULL){}
};
Node*head=NULL;
Node*tail=NULL;
size_t count=0;
public:
void push(const T&element)
{
Node*new_node=new Node(element);
if (tail!=NULL)
tail->next=new_node;
tail=new_node;
if (head==NULL)
head=tail;
count++;
}
bool pop()
{
if (head!=NULL)
{
Node*temp=head;
head=head->next;
delete temp;
count--;
if(head==NULL)
tail=NULL;
return true;
}
return false;
}
T front() const{return head->data;}
T back() const{return tail->data;}
bool empty() const{return count==0;}
size_t size() const{return count;}
~myqueue()
{
while (head!=NULL)
{
Node*temp=head;
head=head->next;
delete temp;
}
}
};自己写的deque:
template<typename T>
class mydeque
{
private:
struct Node
{
T data;
Node*next=nullptr;
Node*prev=nullptr;
Node(const T&value):data(value){}
};
Node*head=nullptr;
Node*tail=nullptr;
size_t count=0;
public:
void push_front(const T&element)
{
Node*new_node=new Node(element);
if(head==nullptr)
head=tail=new_node;
else
{
new_node->next=head;
head->prev=new_node;
head=new_node;
}
count++;
}
void push_back(const T&element)
{
Node*new_node=new Node(element);
if(tail==nullptr)
head=tail=new_node;
else
{
tail->next=new_node;
new_node->prev=tail;
tail=new_node;
}
count++;
}
bool pop_front()
{
if (head!=nullptr)
{
Node*temp=head;
head=head->next;
if (head!=nullptr)
head->prev=nullptr;
else tail=nullptr;
delete temp;
count--;
return true;
}
return false;
}
bool pop_back()
{
if (tail!=nullptr)
{
Node*temp=tail;
tail=tail->prev;
if (tail!=nullptr)
tail->next=nullptr;
else head=nullptr;
delete temp;
count--;
return true;
}
return false;
}
T front()
{
if (head!=nullptr)
return head->data;
throw logic_error("Deque is empty");
}
T back()
{
if (tail!=nullptr)
return tail->data;
throw logic_error("Deque is empty");
}
bool empty()
{
return count==0;
}
size_t size()
{
return count;
}
~mydeque()
{
while (head!=nullptr)
{
Node*temp=head;
head=head->next;
delete temp;
}
}
};自制分数类:
long long minimumcommonmultipleoffractions;
long long maximumcommonfactoroffractions;
class fraction
{
private:
long long a=1,b=1,ansa,ansb;
long long gcd(long long i,long long j){return j==0?i:gcd(j,i%j);}
long long lcm(long long i,long long j){return i/gcd(i,j)*j;}
fraction add(fraction firstfraction,fraction secondfraction)
{
ansa=firstfraction.a*secondfraction.b+firstfraction.b*secondfraction.a;
ansb=firstfraction.b*secondfraction.b;
reductionofafraction(ansa,ansb);
return fraction(ansa,ansb);
}
fraction sub(fraction firstfraction,fraction secondfraction)
{
ansa=firstfraction.a*secondfraction.b-firstfraction.b*secondfraction.a;
ansb=firstfraction.b*secondfraction.b;
reductionofafraction(ansa,ansb);
return fraction(ansa,ansb);
}
fraction mul(fraction firstfraction,fraction secondfraction)
{
ansa=firstfraction.a*secondfraction.a;
ansb=firstfraction.b*secondfraction.b;
reductionofafraction(ansa,ansb);
return fraction(ansa,ansb);
}
fraction div(fraction firstfraction,fraction secondfraction)
{
ansa=firstfraction.a*secondfraction.b;
ansb=firstfraction.b*secondfraction.a;
reductionofafraction(ansa,ansb);
return fraction(ansa,ansb);
}
public:
fraction():a(0),b(1){}
fraction(long long aa,long long bb):a(aa),b(bb){}
void readnumerator(){scanf("%lld",&a);}
void readdenominator(){scanf("%lld",&b);}
void readfraction(){readnumerator();readdenominator();}
long double thevalueofthescore(){return a/b;}
void reductionofafraction(long long&i,long long&j)
{
minimumcommonmultipleoffractions=gcd(i,j);
i/=minimumcommonmultipleoffractions;
j/=minimumcommonmultipleoffractions;
}
friend istream&operator>>(istream&is,fraction&f)
{
scanf("%lld/%lld",&f.a,&f.b);
return is;
}
friend ostream&operator<<(ostream&os, const fraction&f)
{
os<<f.a<<"/"<<f.b;
return os;
}
fraction operator+(const fraction fa){return add(*this,fa);}
fraction operator-(const fraction fa){return sub(*this,fa);}
fraction operator*(const fraction fa){return mul(*this,fa);}
fraction operator/(const fraction fa){return div(*this,fa);}
fraction operator+=(const fraction fa){return *this=add(*this,fa);}
fraction operator-=(const fraction fa){return *this=sub(*this,fa);}
fraction operator*=(const fraction fa){return *this=mul(*this,fa);}
fraction operator/=(const fraction fa){return *this=div(*this,fa);}
};