#pragma once
#include<iostream>
#include<string>namespaceJH{template<typenameT>classnode{public:Tdata;node<T>*prv;node<T>*nxt;node(){data=0,prv=nxt=nullptr;}node(Tvalue){data=value,prv=nxt=nullptr;}};template<typenameT>classlist{private:node<T>*nbegin;node<T>*nend;size_tnsize;public://constructorlist(){nbegin=newnode<T>();nend=newnode<T>();nbegin->nxt=nend;nend->prv=nbegin;nsize=0;}//O(1)//destructor~list();//O(n)//iteratorsnode<T>*begin(){returnnbegin->nxt;}//O(1)node<T>*end(){returnnend;}//O(1)//capacitysize_tsize(){returnnsize;}//O(1)boolempty(){return(nsize==0);}//O(1)//element accessTfront(){returnnbegin->nxt->data;}//O(1)Tback(){returnnend->prv->data;}//O(1)//modifiersvoidinsert(node<T>*,T);//O(1)voidinsert(int,T);//O(n)voiderase(node<T>*);//O(1)voiderase(int);//O(n)voidpush_back(T);//O(1)voidpop_back();//O(1)voidpush_front(T);//O(1)voidpop_front();//O(1)voidprint();//O(n)std::stringiterType();};template<typenameT>voidlist<T>::insert(node<T>*prv,Tvalue){if(prv==nend){std::cout<<"reject"<<std::endl;return;}if(prv==nullptr){std::cout<<"NullPointerException"<<std::endl;return;}nsize++;node<T>*ptr=newnode<T>(value);ptr->prv=prv;ptr->nxt=prv->nxt;prv->nxt->prv=ptr;prv->nxt=ptr;}template<typenameT>voidlist<T>::insert(intpos,Tvalue){node<T>*iter=nbegin;for(inti=0;i<pos;i++){iter=iter->nxt;}insert(iter,value);}template<typenameT>voidlist<T>::erase(node<T>*ptr){if(ptr==nbegin){std::cout<<"reject"<<std::endl;return;}if(ptr==nullptr){std::cout<<"NullPointerException"<<std::endl;return;}nsize--;ptr->prv->nxt=ptr->nxt;ptr->nxt->prv=ptr->prv;deleteptr;}template<typenameT>voidlist<T>::erase(intpos){node<T>*iter=nbegin;for(inti=0;i<=pos;i++){iter=iter->nxt;}erase(iter);}template<typenameT>voidlist<T>::push_back(Tvalue){insert(nend->prv,value);}template<typenameT>voidlist<T>::pop_back(){erase(nend->prv);}template<typenameT>voidlist<T>::push_front(Tvalue){insert(nbegin,value);}template<typenameT>voidlist<T>::pop_front(){erase(nbegin->nxt);}template<typenameT>list<T>::~list(){autonow=nbegin;autonxt=nbegin->nxt;while(now!=nend){if(now)deletenow;now=nxt;if(now!=nend)nxt=now->nxt;}}template<typenameT>voidlist<T>::print(){autoiter=begin();for(;iter!=end();iter=iter->nxt)std::cout<<iter->data<<std::ends;}template<typenameT>std::stringlist<T>::iterType(){return"bidirectional";}}
#include"vector.h"
#include"list.h"
#pragma once
namespaceJH{template<typenameT>classstack{private:vector<T>arr;public:boolempty(){returnarr.empty();}//O(1)size_tsize(){returnarr.size();}//O(1)Ttop(){returnarr.back();}//O(1)voidpush(Tvalue){arr.push_back(value);}//amortized O(1)voidpop(){arr.pop_back();}//amortized O(1)};template<typenameT>classstack_list{private:list<T>arr;public:boolempty(){returnarr.empty();}//O(1)size_tsize(){returnarr.size();}//O(1)Ttop(){returnarr.back();}//O(1)voidpush(Tvalue){arr.push_back(value);}//amortized O(1)voidpop(){arr.pop_back();}//amortized O(1)};}
queue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#pragma once
#include"list.h"namespaceJH{template<typenameT>classqueue{private:list<T>arr;public:boolempty(){returnarr.empty();}//O(1)size_tsize(){returnarr.size();}//O(1)Tback(){returnarr.back();}//O(1)Tfront(){returnarr.front();}//O(1)voidpush(Tvalue){arr.push_back(value);}//amortized O(1)voidpop(){arr.pop_front();}//amortized O(1)};}