C++ 数据结构模板 队列 栈 动态链表 模板 Queue Stack List
C++数据结构模板,可以实现基本功能,用法和stl差不多,比如Q.pop();Q.push(a);Q.front();......
(由于动态链表用的不多,若有错误望各位大神不吝赐教:)
队列:
1 class Queue 2 { 3 private: 4 int Head,Tail,Size; 5 int val[30010]; 6 7 public: 8 9 Queue() 10 { 11 Head=0; 12 Tail=-1; 13 Size=0; 14 memset(val,0,sizeof(val)); 15 } 16 17 inline bool empty() 18 { 19 return Size==0; 20 } 21 22 inline void push(const int & ele) 23 { 24 Tail=(Tail+1)%30000; 25 Size++; 26 val[Tail]=ele; 27 return ; 28 } 29 30 inline void pop() 31 { 32 if(Size==0)return ; 33 Head=(Head+1)%30000; 34 Size--; 35 return ; 36 } 37 38 inline int front() 39 { 40 if(Size==0)return 0; 41 return val[Head]; 42 } 43 44 inline int back() 45 { 46 if(Size==0)return 0; 47 return val[Tail]; 48 } 49 50 inline int size() 51 { 52 return Size; 53 } 54 55 inline void clear() 56 { 57 Head=0; 58 Tail=-1; 59 Size=0; 60 memset(val,0,sizeof(val)); 61 return ; 62 } 63 }Q;
栈:
class stack { private: int Size; int val[50100]; int h_top; public: inline int top() { return val[h_top]; } inline int size() { return Size; } inline bool pop() { if(Size==0)return false; val[h_top]=0; h_top--; Size--; return true; } inline bool push(int temp_s) { h_top++; Size++; val[h_top]=temp_s; return true; } inline void clear() { memset(val,0,sizeof(val)); h_top=-1; Size=0; return ; } inline bool empty() { return Size==0; } }st;
动态链表:
#include <iostream> #include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> using namespace std; template<typename _Tp> class List{ public: class List_val{ private: List_val *_next; List_val *_last; _Tp _data; public: List_val(){ _next=NULL; _last=NULL; _data=0; return ; } public: void insert_back(_Tp _new_data){ List_val *_new_elem; _new_elem=new List_val; if(_next!=NULL)_new_elem->_next=_next; if(_next!=NULL)_next->_last=_new_elem; _new_elem->_last=this; _next=_new_elem; _new_elem->_data=_new_data; return ; } void insert_front(_Tp _new_data){ List_val *_new_elem; _new_elem=new List_val; if(_last!=NULL)_new_elem->_last=_last; if(_last!=NULL)_last->_next=_new_elem; _new_elem->_next=this; _last=_new_elem; _new_elem->_data=_new_data; return ; } public: List_val* next(){ return _next; } List_val* last(){ return _last; } _Tp data(){ return _data; } public: void change_next(List_val* _new_next){ _next= _new_next; return ; } void change_last(List_val* _new_last){ _last= _new_last; return ; } void change_data(_Tp _new_data){ _data= _new_data; return ; } void del(){ _last->_next=_next; _next->_last=_last; delete[] this; } }; private: List_val *_begin; List_val *_end; public: List(){ _begin=new List_val; _end =new List_val; _begin->change_next(_end); _end->change_last(_begin); return ; } void push_front(_Tp _new_data){ _begin->insert_back(_new_data); return ; } void push_back(_Tp _new_data){ _end->insert_front(_new_data); return ; } List_val* begin(){ return _begin->next(); } List_val* end(){ return _end; } List_val* find(List_val* _from,_Tp _find_data){ List_val *i; for(i=_from;i!=NULL;i=i->next()){ if(i->data()==_find_data)return i; } return _end; } void insert_back(List_val* _from,_Tp _new_data){ _from->insert_back(_new_data); return ; } void insert_front(List_val* _from,_Tp _new_data){ _from->insert_front(_new_data); return ; } void del(List_val* _from){ _from.del(); return ; } }; int main(){ List<int> l; int i; for(i=1;i<=10;++i) l.push_back(i); for(i=1;i<=10;++i) l.push_front(i); List<int>::List_val *j; for(j=l.begin();j!=l.end();j=j->next()){ cout << j->data() << ' ' ; } cout << endl; l.find(l.begin(),7)->insert_front(100); l.find(l.begin(),3)->insert_back(200); l.find(l.begin(),200)->del(); for(j=l.begin();j!=l.end();j=j->next()){ cout << j->data() << ' ' ; } cout << endl; return 0; }