1、各种数据结构模板源代码,有了它数据结构随意滴很,会了它,胜利哥哥也拿你没辙~~~~~无敌汇编著~~~~~学数据结构学生必备
无意中寻求的源代码,最近在学数据结构的同学对于严蔚敏教程的代码很头疼,完成代码也不好找,于是我便把我的宝贝珍藏都拿出来了,希望对大家有用!
其中可能有点小错误,自己改一下吧....我正在一点点的完善...
抽象数据类型的数组的实现。
#include 2、s Type >class Array{
public:
Array(int Size =DefaultSize);
Array(const Array 3、ize(int sz);
private:
Type *elements;
int ArraySize;
void getArray();
};
template 4、
}
template 5、elements=new Type[n];
if(elements==0){cerr<<"Memory Allocation error"< 6、index out of range"< 7、dl;return ;}
int n=(sz<=ArraySize)?sz:ArraySize;
Type *srcptr=elements;
Type *destptr =newarray;
while(n--)*destptr++=*srcptr++;
delete []elements;
elements =newarray;
ArraySize=sz;
}
}
int main()
{
Array 8、t【3】是elements【3】
//的引用;
int a=t[3];
cout<
using namespace std;
#define defaultsize 10
template 9、size=defaultsize); //构造函数
~seqlist(){delete []data;} //析构函数
int length()const {return last+1;}//求长度
int find(type &x)const; //查找
int isin(type &x); //存在
int insert(type &x,int i); //插入
int remove(type &x); //删除
int next(type &x); //下一个位置
int prior(type &x); //前一个位置
int ise 10、mpty(){return last==maxsize-1;}//为空
type get(int i){return i<0||i>last?NULL:data[i];}//得到第i个数
private:
type *data;
int maxsize;
int last;
};
template 11、template 12、x)i++;
else found=1;
return found;
}
template 13、x;
return 1;
}
}
template 14、eturn i+1; //返回x的前一个位置。
else return -1;
}
template 15、重复元素只留一个
int n=la.length();
int m=lb.length();
for(int i=1;i<=m;i++){
type x=lb.get(i);
int k=la.find(x);
if(k==-1)
{
la.insert(x,n+1);n++;
}
}
}
template 16、);
int m=lb.length();
int i=0;
while(i 17、~~~华丽分割~~~~~~~~~~~~~~~~~~~~~~~~~~~字符串结构的实现
#include 18、or()(int pos,int len);
int operator==(const String &ob)const{return strcmp(ch,ob.ch)==0;}
int operator!=(const String &ob)const{return strcmp(ch,ob.ch)!=0;}
int operator!()const{return curlen==0;}
String &operator=(const String &ob);
String &operator+=(const String &ob);
char &oper 19、ator[](int i);
int find(String &pat)const;
private:
int curlen;
char *ch;
};
String::String(const String &ob)
{//串复制构造函数
ch=new char[maxlen+1];
if(!ch){cerr<<"allcation error\n";exit(1);}
curlen=ob.curlen;
strcpy(ch,ob.ch);
}
String ::String(const char *ini 20、t){
//串构造函数
ch =new char[maxlen+1];
if(!ch){cerr<<"allocation failed\n";exit(1);}
curlen=strlen(init);
strcpy(ch,init);
}
String::String(){
//默认构造函数
ch=new char[maxlen+1];
if(!ch){cerr<<"allocation error\n";exit(1);}
curlen=0;
ch[0]='\0';
}
String &Stri 21、ng::operator()(int pos,int len){
//求子串
String *tmp=new String;
if(pos<0||pos+len-1>=maxlen||len<0){
tmp->curlen=0;tmp->ch[0]='\0';
}
else{
if(pos +len-1>=curlen)len=curlen-pos;
tmp->curlen=len;
for(int i=0,j=pos;i 22、}
return *tmp;
}
String &String::operator=(const String &ob){
//串赋值
if(&ob!=this)
{
delete []ch;
ch=new char[maxlen+1];
if(!ch){cerr<<"out of memory \n";exit(1);}
curlen=ob.curlen;
strcpy(ch,ob.ch);
}
else cout<<"attempted assignment of a String to itself"<< 23、endl;
return *this;
}
String &String::operator+=(const String &ob){
//串连接
char *tmp=ch;
curlen+=ob.curlen;
ch =new char[maxlen+1];
if(!ch){cerr<<"out of memory!"< 24、operator[](int i){
//取×this的第i个字符
if(i<0&&i>=curlen){cout<<"out of memory"< 25、
else {i++;s=ch+i;p=pat.ch;}
return -1;
}
int main()
{
String ss;
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~华丽分割~~~~~~~~~~~~~~~~~~~~~~~
顺序栈的实现
#include 26、t =10);
~Sstack(){delete[]elements;}
void Push(const type &item);
type Pop();
type GetTop();
void MakeEmpty(){top=-1;}
int IsEmpty()const {return top==-1;}
int IsFull()const {return top==maxSize-1;}
private:
int top;
type *elements;
int maxSize;
};
template < 27、class type> Sstack 28、ert(!IsEmpty());
return elements[top];
}
int main(){
Sstack 29、tackNode{
friend class Lstack 30、type GetTop();
void MakeEmpty();
int IsEmpty()const{ return top==NULL;}
private:
StackNode 31、onst type &item){
top=new StackNode 32、t(!IsEmpty());
return top->data;
}
int main(){
Lstack 33、ueue(){delete[]elements;}
void EnQueue(const type &item);
type DeQueue();
type GetFront();
void MakeEmpty(){front=rear=0;}
int IsEmpty()const {return front==rear;}
int IsFull()const {return (rear+1)%maxSize==front;}
int Length()const {return (rear-front+maxSize)%maxSize;}
pri 34、vate:
int rear,front;
type *elements;
int maxSize;
};
template 35、));
rear=(rear+1)%maxSize;
elements[rear]=item;
}
template 36、1)%maxSize];
}
int main()
{
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~华丽分割~~~~~~~~~~~~~~~~~~~~~~
链式队列的实现
#include 37、e>;
private:
type data;
QueueNode 38、id MakeEmpty();
int IsEmpty()const{return front==NULL;}
private:
QueueNode 39、EnQueue(const type &item){
if(front==NULL) front=rear=new QueueNode 40、nk;
delete p;
return value;
}
template 41、g namespace std;
template 42、onst type&item,ListNode 43、)const;
ListNode 44、link(NULL){} //无参数构造函数
template 45、ype>::GetNode(const type& item,
ListNode 46、f(link==NULL)return NULL;
link=tempptr->link;
return tempptr;
}
//★★★★★★★★★★List 的成员函数。
template 47、){
q=first->link;
first->link=q->link;
delete q;
}
last=first;
}
template 48、e>ListNode 49、rst;
ListNode 50、ue,p->link);
if(p->link==NULL)last =newnode;
if(p->link=newnode)
return 1;
}
template
©2010-2025 宁波自信网络信息技术有限公司 版权所有
客服电话:4009-655-100 投诉/维权电话:18658249818