数据结构算法集C++语言实现.docx
- 文档编号:9677832
- 上传时间:2023-02-05
- 格式:DOCX
- 页数:53
- 大小:22.80KB
数据结构算法集C++语言实现.docx
《数据结构算法集C++语言实现.docx》由会员分享,可在线阅读,更多相关《数据结构算法集C++语言实现.docx(53页珍藏版)》请在冰豆网上搜索。
数据结构算法集C++语言实现
//数据结构算法集---C++语言实现
//各种类都使用模版设计,可以对各种数据类型操作(整形,字符,浮点)
///////////////////////////
////
//堆栈数据结构stack.h//
////
//////////////////////////
#include
template
template
classStackNode
{
friendclassStack
private:
Typedata;
StackNode
StackNode(TypeD=0,StackNode
link(L),data(D){}
};
template
classStack
{
public:
Stack():
top(NULL),NumItem(0){}
voidPush(Typeitem);
TypePop();
TypeGetTop();
voidMakeEmpty();
boolISEmpty();
intGetNum();
private:
intNumItem;
StackNode
};
template
voidStack
:
Push(Typeitem)
{
top=newStackNode
NumItem++;
}
template
TypeStack
:
Pop()
{
StackNode
Typetemp;
temp=top->data;
p=top;
top=top->link;
deletep;
NumItem--;
returntemp;
}
template
TypeStack
:
GetTop()
{
returntop->data;
}
template
boolStack
:
ISEmpty()
{
returntop==NULL;
}
template
voidStack
:
MakeEmpty()
{
deletetop;
}
template
intStack
:
GetNum()
{
returnNumItem;
}
///////////////////////////
////
//队列数据结构Queue.h//
////
//////////////////////////
#include
template
template
{
friendclassQueue
private:
Typedata;
QueueNode
QueueNode(Typed=0,QueueNode*l=NULL):
data(d),link(l){}
};
template
{
public:
Queue():
rear(NULL),front(NULL){}
~Queue();
voidEnQueue(Typeitem);
TypeDelQueue();
TypeGetFront();
voidMakeEmpty();
boolISEmpty(){returnfront==NULL;}
private:
QueueNode
};
template
Queue
:
~Queue()
{
QueueNode
while(front!
=NULL)
{
p=front;
front=front->link;
deletep;
}
}
template
voidQueue
:
EnQueue(Typeitem)
{
if(front==NULL)
front=rear=newQueueNode
else
rear=rear->link=newQueueNode
}
template
TypeQueue
:
DelQueue()
{
QueueNode
Typetemp=p->data;;
front=front->link;
deletep;
returntemp;
}
template
TypeQueue
:
GetFront()
{
returnfront->data;
}
template
voidQueue
:
MakeEmpty()
{
QueueNode
while(front!
=NULL)
{
p=front;
front=front->link;
deletep;
}
}
///////////////////////////
////
//链表数据结构list.h//
////
//////////////////////////
#include
template
classlist;
template
classlistnode
{
public:
friendclasslist
private:
typedata;
listnode
};
template
classlist
{
public:
list();
~list();
voidinsertend(type);//向链表尾部插入元素
boolinsert(type,int);//向链表任意位置插入元素
voiddelnode(inti);//删除元素
intfind(typeT);//查找元素
voidmakeempty();//销毁链表
boolprint();//打印链表
intgetlen();//得到链表长度
private:
listnode
intlength;
};
template
voidinitlist(type&tmp);
template
voidlist_exit(list
voidinitation();
template
voidlist_insertend(list
template
:
getlen()
{
returnlength;
}
template
:
makeempty()
{
listnode
p1=first->next;
first->next=NULL;
while(p1!
=NULL)
{
p2=p1;
p1=p1->next;
deletep2;
}
length=0;
}
template
:
insertend(typet)
{
listnode
p=newlistnode
p->data=t;
p->next=NULL;
last->next=p;
last=p;
length++;
}
template
:
insert(typet,inti)
{
listnode
p=first;
intk=1;
while(p!
=NULL&&k
{
p=p->next;
k++;
}
if(p==NULL&&k!
=i)
returnfalse;
else
{
listnode
tp=newlistnode
tp->data=t;
tp->next=p->next;
p->next=tp;
length++;
returntrue;
}
}
template
:
delnode(inti)
{
intk=1;
listnode
p=first;
while(p->next!
=NULL&&k!
=i)
{
p=p->next;
k++;
}
t=p->next;
cout<<"你已经将数据项"< p->next=p->next->next; length--; deletet; } template : print() { listnode if(length==0) returnfalse; else { cout<<"链表中有"< "< while(p) { cout< p=p->next; } } cout< returntrue; } template : find(typeT) { listnode inti=1; while(p&&p->data! =T) { p=p->next; i++; } if(p) returni; else return0; } template : ~list() { deletefirst; cout<<"欢迎再次使用(! ^! )"< } template : list() { listnode node->next=NULL; first=last=node; length=0; } /////////////////////////// //// //图数据结构graph.h// //// ////////////////////////// #include #include"Queue.h" template template { friendclassGraph intnum; DisTypeval; Node }; template { friendclassGraph NameTypedata; Node }; template classGraph { public: voidCreat();//创建图 voidPrintNode();//打印图中的各个数据项 voidDFS();//图的深度优先搜索,主过程 voidDFS(intv,intvisited[]);//子过程 voidBFS();//图的广度优先搜索,主过程 voidBFS(intv,intvisited[]);//子过程 voidShortPath();//求最短路径 private: GpNode Node intNumNode;//节点个数 }; template voidGraph : Creat() { do { cout<<"请输入节点个数: "; cin>>NumNode; }while(NumNode<=0); table=newGpNode cout<<"请输入各节点数据项"< for(inti=0;i { cin>>table[i].data; table[i].link=NULL; } cout<<"请输入各边的关系(如: AB)"< i=1; NameTypenodeA,nodeB; boolfindA,findB; charISExit; intm,n; do { findA=findB=false; cout<<"请输入第"< cin>>nodeA>>nodeB; for(m=0,n=0;m (findA&findB);)//查找边的节点 { if(nodeA! =table[m].data) m++; else findA=true; if(nodeB! =table[n].data) n++; else findB=true; } if(! (findA&findB)) cout<<"输入的节点数据项有错误"< else { p=newNode p->next=table[m].link; p->num=n; table[m].link=p; cout<<"请输入该对边的权值: "; cin>>p->val; i++; } cout<<"是否继续输入: y)继续,X)任意键退出"; cin>>ISExit; if(ISExit! ='y'&&ISExit! ='Y') break; }while(true); } template voidGraph : PrintNode() { cout<<"图中各节点数据项: "; for(inti=0;i cout< cout< } template voidGraph : DFS() { int*visited=newint[NumNode]; cout<<"图的深度优先搜索: "; for(inti=0;i visited[i]=0; for(i=1;i DFS(i,visited); delete[]visited; cout< } template voidGraph : DFS(intv,intvisited[]) { Node if(visited[v]==0) cout< visited[v]=1; t=table[v].link; while(t! =NULL) { if(visited[t->num]==0) DFS(t->num,visited); t=t->next; } } template voidGraph : BFS() { int*visited=newint[NumNode]; cout<<"图的广度优先搜索: "; for(inti=0;i visited[i]=0; for(i=0;i BFS(i,visited); } template voidGraph : BFS(intv,intvisited[]) { Queue intn; if(visited[v]==0) { visited[v]=1; cout< q.EnQueue(v); while(! q.ISEmpty()) { n=q.DelQueue(); p=table[n].link; while(p! =NULL) { n=p->num; if(visited[n]==0) { cout< visited[n]=1; } p=p->next; } } } } /////////////////////////// //// //排序算法数据结构Compositor.h// //// ////////////////////////// #include template classCompositor { public: Compositor(): sort(NULL){} voidCreat();//创建排序数组 voidBubble();//冒泡排序 voidInsert();//插入排序 //快速排序 voidQuick(); voidQSort(int,int); intPartition(intlow,inthigh); //归并排序 voidMerge(TypeSR[],TypeTR[],inti,intm,intn); voidMsort(TypeSR[],TypeTR1[],ints,intt); voidMergeSort(); //选择排序 voidSelect(); voidPrint();//打印排序后的结果 protected: Type*sort; intleng; }; template voidCompositor : Creat() { cout<<"输入你需要排序的数据个数: "; cin>>leng; while(leng<=0) { cout<<"输入数据有误"; cin>>leng; } sort=newType[leng]; cout<<"请输入各数据项: "; for(inti=0;i cin>>sort[i]; } template voidCompositor : Insert() { Creat(); Typetemp; for(inti=1;i { if(sort[i] { temp=sort[i]; for(intj=i-1;temp { sort[j+1]=sort[j]; } sort[j+1]=temp; } } Print(); } template voidCompositor : Bubble() { Creat(); Typetemp; for(inti=leng-1;i>=0;i--) { for(intj=0;j { if(sort[j]>sort[j+1]) { temp=sort[j]; sort[j]=sort[j+1]; sort[j+1]=temp; } } } Print(); } template voidCompositor : Quick() { Creat(); QSort(0,leng-1); Print(); } template 如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。 copyright@ 2008-2022 冰点文档网站版权所有 经营许可证编号:鄂ICP备2022015515号-1