C++数据结构模板,可以实现基本功能,用法和stl差不多,比如Q.pop();Q.push(a);Q.front();...... 

(由于动态链表用的不多,若有错误望各位大神不吝赐教:)

队列:

 class Queue
{
private:
int Head,Tail,Size;
int val[]; public: Queue()
{
Head=;
Tail=-;
Size=;
memset(val,,sizeof(val));
} inline bool empty()
{
return Size==;
} inline void push(const int & ele)
{
Tail=(Tail+)%;
Size++;
val[Tail]=ele;
return ;
} inline void pop()
{
if(Size==)return ;
Head=(Head+)%;
Size--;
return ;
} inline int front()
{
if(Size==)return ;
return val[Head];
} inline int back()
{
if(Size==)return ;
return val[Tail];
} inline int size()
{
return Size;
} inline void clear()
{
Head=;
Tail=-;
Size=;
memset(val,,sizeof(val));
return ;
}
}Q;

栈:

class    stack
{
private: int Size;
int val[]; int h_top; public: inline int top()
{
return val[h_top];
} inline int size()
{
return Size;
} inline bool pop()
{
if(Size==)return false;
val[h_top]=;
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,,sizeof(val));
h_top=-;
Size=;
return ;
} inline bool empty()
{
return Size==;
}
}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=;
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=;i<=;++i)
l.push_back(i);
for(i=;i<=;++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(),)->insert_front();
l.find(l.begin(),)->insert_back();
l.find(l.begin(),)->del();
for(j=l.begin();j!=l.end();j=j->next()){
cout << j->data() << ' ' ;
} cout << endl; return ;
}
05-11 17:24