不定期更新,目前主要实现常用数据结构的部分功能。
实现的不是很好,还望大佬指教。
更新日志:
upd 2023-01-19:增加队列(queue)模板,并改进代码。
upd 2023-01-28:增加复数(complex)模板(因为复数调用时一般用double类型,所以没加泛型其实是我懒)。
upd 2023-07-03:鸽了好久,终于来更新了。
更改队列和双端队列代码(改为链表实现)。同时增加平面向量(vec)模板(可以用作计算几何)。
如有错误,欢迎大家指正。
当前总行数:292
当前总长度:6847
CODE:
//Author:Leowang2009
const int N=1e6;
template<typename Tp_>
class stack{//栈
public:
void clear(){
head=-1;
}
unsigned int size(){
return head+1;
}
void pop(){
head--;
}
void push(Tp_ x){
val[++head]=x;
}
Tp_ top(){
return val[head];
}
private:
Tp_ val[N];
int head=-1;
};
template<typename Tp_>
class queue{//队列
public:
unsigned int size(){
return siz;
}
void pop(){
stk.push(head);
siz--;
head=val[head].nxt;
}
void push(Tp_ x){
if(!stk.size()){
stk.push(++id);
}
if(!siz){
head=tail=stk.top();
val[stk.top()].nxt=0;
stk.pop();
}else{
val[stk.top()].nxt=0;
val[tail].nxt=stk.top();
tail=stk.top();
stk.pop();
}
siz++;
}
Tp_ front(){
return val[head].v;
}
Tp_ back(){
return val[tail].v;
}
private:
struct node{
Tp_ v;
int nxt;
}val[N];
int head=-1,tail=-1,siz,id;
stack<int>stk;
};
template<typename Tp_>
class deque{//双端队列
public:
unsigned int size(){
return siz;
}
void pop_front(){
stk.push(head);
siz--;
head=val[head].nxt;
val[head].lst=0;
}
void pop_back(){
stk.push(tail);
siz--;
tail=val[tail].lst;
val[tail].nxt=0;
}
void push_back(Tp_ x){
if(!stk.size()){
stk.push(++id);
}
if(!siz){
head=tail=stk.top();
val[stk.top()].lst=val[stk.top()].nxt=0;
stk.pop();
}else{
val[stk.top()].lst=tail;
val[stk.top()].nxt=0;
val[tail].nxt=stk.top();
tail=stk.top();
stk.pop();
}
siz++;
}
void push_front(Tp_ x){
if(!stk.size()){
stk.push(++id);
}
if(!siz){
head=tail=stk.top();
val[stk.top()].lst=val[stk.top()].nxt=0;
stk.pop();
}else{
val[stk.top()].nxt=head;
val[stk.top()].lst=0;
val[head].lst=stk.top();
head=stk.top();
stk.pop();
}
siz++;
}
Tp_ front(){
return val[head].v;
}
Tp_ back(){
return val[tail].v;
}
private:
struct node{
Tp_ v;
int lst,nxt;
}val[N];
int head=-1,tail=-1,siz,id;
stack<int>stk;
};
class complex{//复数
public:
complex(double _r=0,double _i=0){
r=_r,i=_i;
}
complex&operator+=(const complex&);
complex&operator-=(const complex&);
complex&operator*=(const complex&);
complex&operator+=(const double&);
complex&operator-=(const double&);
complex&operator*=(const double&);
double real()const{
return r;
}
double imag()const{
return i;
}
private:
double r,i;
friend complex&comp_plus(complex*,const complex&);
friend complex&comp_minus(complex*,const complex&);
friend complex&comp_mul(complex*,const complex&);
friend complex&comp__plus(complex*,const double&);
friend complex&comp__minus(complex*,const double&);
friend complex&comp__mul(complex*,const double&);
};
inline complex&comp_plus(complex*comp,const complex&another){
comp->r+=another.r;
comp->i+=another.i;
return *comp;
}
inline complex&comp_minus(complex*comp,const complex&another){
comp->r-=another.r;
comp->i-=another.i;
return *comp;
}
inline complex&comp_mul(complex*comp,const complex&another){
comp->r=(comp->r)*another.r-(comp->i)*another.i;
comp->i=(comp->r)*another.i+(comp->i)*another.r;
return *comp;
}
inline complex&comp__plus(complex*comp,const double&another){
comp->r+=another;
return *comp;
}
inline complex&comp__minus(complex*comp,const double&another){
comp->r-=another;
return *comp;
}
inline complex&comp__mul(complex*comp,const double&another){
comp->r*=another;
comp->i*=another;
return *comp;
}
inline complex&complex::operator+=(const complex&another){
return comp_plus(this,another);
}
inline complex&complex::operator-=(const complex&another){
return comp_minus(this,another);
}
inline complex&complex::operator*=(const complex&another){
return comp_mul(this,another);
}
inline complex&complex::operator+=(const double&another){
return comp__plus(this,another);
}
inline complex&complex::operator-=(const double&another){
return comp__minus(this,another);
}
inline complex&complex::operator*=(const double&another){
return comp__mul(this,another);
}
inline complex operator+(const complex&comp,const complex&another){
return complex(comp.real()+another.real(),comp.imag()+another.imag());
}
inline complex operator-(const complex&comp,const complex&another){
return complex(comp.real()-another.real(),comp.imag()-another.imag());
}
inline complex operator*(const complex&comp,const complex&another){
return complex(comp.real()*another.real()-comp.imag()*another.imag(),comp.imag()*another.real()+comp.real()*another.imag());
}
inline complex operator+(const complex&comp,const double&another){
return complex(comp.real()+another,comp.imag());
}
inline complex operator-(const complex&comp,const double&another){
return complex(comp.real()-another,comp.imag());
}
inline complex operator*(const complex&comp,const double&another){
return complex(comp.real()*another,comp.imag()*another);
}
#include<iostream>
inline std::ostream&operator<<(std::ostream&o,const complex&comp){
return o<<'('<<comp.real()<<','<<comp.imag()<<')';
}
class vec{//平面向量
public:
vec(double _x=0,double _y=0){
x=_x,y=_y;
}
vec&operator+=(const vec&);
vec&operator-=(const vec&);
vec&operator*=(const double&);
double _x()const{
return x;
}
double _y()const{
return y;
}
private:
double x,y;
friend vec&vector_plus(vec*,const vec&);
friend vec&vector_minus(vec*,const vec&);
friend vec&vector_mul(vec*,const double&);
};
inline vec&vector_plus(vec*vct,const vec&another){
vct->x+=another.x;
vct->y+=another.y;
return *vct;
}
inline vec&vector_minus(vec*vct,const vec&another){
vct->x-=another.x;
vct->y-=another.y;
return *vct;
}
inline vec&vector_mul(vec*vct,const double&another){
vct->x*=another;
vct->y*=another;
return *vct;
}
inline vec&vec::operator+=(const vec&another){
return vector_plus(this,another);
}
inline vec&vec::operator-=(const vec&another){
return vector_minus(this,another);
}
inline vec&vec::operator*=(const double&another){
return vector_mul(this,another);
}
inline vec operator+(const vec&vct,const vec&another){
return vec(vct._x()+another._x(),vct._y()+another._y());
}//向量加
inline vec operator-(const vec&vct,const vec&another){
return vec(vct._x()-another._x(),vct._y()-another._y());
}//向量减
inline vec operator*(const vec&vct,const double&another){
return vec(vct._x()*another,vct._y()*another);
}//向量乘浮点
inline double operator*(const vec&vct,const vec&another){
return vct._x()*another._y()-vct._y()*another._x();
}//叉乘
inline double operator^(const vec&vct,const vec&another){
return vct._x()*another._x()+vct._y()*another._y();
}//点乘
int main(){
deque<int>dq;
stack<int>stk;
queue<int>que;
complex comp;
vec vct;
return 0;
}