1、链式图相关算法 C+实现.cpp 部分#include#include“graph2.h“using namespace std;int main ()graphlist tu1; /切记注意顶点编号从0 开始/int ch;coutch;while(ch!=0)switch(ch)case 1:int v1,v2;coutv1v2;coutv;coutv3v4;coutinv;tu1.insertv(inv);break;case 9:int inv1,inv2,inweight;coutinv1inv2inweight;tu1.inserte(inv1,inv2,inweight);bre
2、ak;case 10:int dele1,dele2;coutdele1dele2;tu1.dele(dele1,dele2);break;coutch;if(ch=0)coutusing namespace std;#define maxgraphsize 100#define maxstacksize 100struct edgefriend class graphlist;int veradj;/邻接顶点序号,从 0 开始编号/int cost;/边的权值/edge * link;/指向下一个边节点的指针/edge()veradj=0;cost=0;link=NULL;edge(int
3、v,int c,edge * l):veradj(v),cost(c),link(l);struct vertexfriend class graphlist;int vername;/顶点名称/edge * adjacent;/边链表的头指针/;class graphlistprivate:vertex * head;int graphsize;public:graphlist()int e,from,to,weight;head=new vertex maxgraphsize;coutgraphsize;for(int i=0;ie;for(i=0;ifromtoweight;edge *
4、 p=new edge();p-veradj=to;p-link=NULL;p-cost=weight;edge * q=headfrom.adjacent;if(q=NULL)headfrom.adjacent=p;else while(q-link!=NULL)q=q-link;q-link=p;graphlist()for(int i=0;ilink;delete p;p=headi.adjacent;delete head;bool graphempty()if(graphsize=0)return true;else return false;bool graphfull()if(g
5、raphsize=maxgraphsize)return true;else return false;int vnumber();int enumber();int getweight(int int * int getfirstneighbor(int v);int getnextneighbor(int v1,int v2);int RDFS(int v,int * visited);void insertv(int void inserte(intvoid delv(int v);void DepthFS();void DFS(int v);void BFS(int v);void D
6、shortpath(int v);void graphlist:dele(int v1,int v2)/删除一条边/edge * p=headv1.adjacent;edge * t=headv1.adjacent;p=p-link;/if(p=NULL)if(t-veradj=v2)headv1.adjacent=NULL;delete t;else coutveradj=v2)headv1.adjacent=t-link;delete t;else while(p-veradj!=v2p=p-link;if(p-veradj=v2)t-link=p-link;delete p; else
7、coutveradj=v2)return p-cost;p=p-link;return 0;int graphlist:getfirstneighbor(int v)if(v=-1)return -1;edge * p=headv.adjacent;if(p!=NULL)return p-veradj;else return -1;int graphlist:getnextneighbor(int v1,int v2)if(v1!=-1while(p-veradj!=v2if(p=NULL)return -1;p=p-link;/指向 v2 的邻接顶点/if(p=NULL)return -1;
8、/v2 没有邻接顶点/return p-veradj;return -1;void graphlist:DepthFS()/深度优先遍历/int * visited=new intgraphsize;for(int k=0;kclass stack /堆栈定义/private:int size;T * stackarray;int top;int maxsize;public:stack()size=maxsize=1000;stackarray=new Tmaxsize;top=-1;stack()delete stackarray;bool stackempty()if(top=-1)re
9、turn true;else return false;bool stackfull()if(top=maxsize)return true;else return false;bool push(Ts.push(v);int w;while(!s.stackempty()w=s.pop();coutlink!=NULL)p=p-link;p-link=iptr;cout#include“graph.h“using namespace std;templateclass queueprivate:int front;int rear;int count;int size;T * qarray;
10、int maxsize;public:queue()size=maxsize=1000;qarray=new T maxsize;front=0;rear=0;count=0;queue()delete qarray;bool isqempty()if(count=0)return true;else return false;void insert(T qarrayrear=item;count+;T qdelete()if(isqempty()cout q;q.insert(v);while(!q.isqempty()int s=q.qdelete();int w=getfirstneighbor(s);while(w !=-1)if(visitedw=0)coutveradj;if(sk!=1pathk=u;p=p-link;int ldist=max;for(i=0;i0u=i;su=1;p=headu.adjacent;cout“path:“ “;for(i=0;in;i+)coutpathi“ “;coutendl;cout“dist:“ “;for(i=0;in;i+)coutdisti“ “;coutendl;delete path;delete dist;