:#include<iostream>usingnamespacestd;template<classT>classSeqQueue{public:SeqQueue(intmSize);~SeqQueue(){delete[]q;}boolIsEmpty()const{returnfront==rear;}boolIsFull()const{return(rear+1)%maxSize==front;}boolFront(T&x)const;boolEnQueue(Tx);//从队尾添加元素boolDeQueue();//从对头删除元素voidClear(){front=rear=0;}private:intfront,rear;intmaxSize;T*q;};template<classT>SeqQueue<T>::SeqQueue(intmSize){maxSize=mSize;q=newT[maxSize];front=rear=0;}template<classT>boolSeqQueue<T>::Front(T&x)const{if(IsEmpty()){cout<<"Empty"<<endl;returnfalse;}x=q[(front+1)%maxSize];returntrue;}template<classT>boolSeqQueue<T>::EnQueue(Tx){if(IsFull()){cout<<"Full"<<endl;returnfalse;}rear=(rear+1)%maxSize;q[rear]=x;returntrue;}template<classT>boolSeqQueue<T>::DeQueue(){if(IsEmpty()){cout<<"Undetflow"<<endl;returnfalse;}front=(front+1)%maxSize;returntrue;}#include<iostream>usingnamespacestd;enumResultCode{Overflow,Underflow,ess,Failure,Dulplicate,NotPresent};template<classT>classGraph{public:virtualResultCodeInsert(intu,intv,T&w)=0;virtualResultCodeRemove(intu,intv)=0;virtualboolExist(intu,intv)const=0;virtualintVertices()const{returnn;}protected:intn,e;};template<classT>classMGraph:publicGraph<T>{public:MGraph(intmSize,constT&noedg);~MGraph();ResultCodeInsert(intu,intv,T&w);ResultCodeRemove(intu,intv);boolExist(intu,intv)const;voidVisit(){for(inti=0;i<n;i++){for(intj=0;j<n;j++)cout<<a[i][j]<<"";cout<<endl;}}voidBFS();voidDFS();protected:T**a;TnoEdge;voidBFS(intv,bool*visited);voidDFS(intv,bool*visited);};template<classT>voidMGraph<T>::BFS(){bool*visited=newbool[n];for(inti=0;i<n;i++)visited[i]=false;for(i=0;i<n;i++)if(!visited[i])BFS(i,visited);delete[]visited;}template<classT>voidMGraph<T>::DFS(){bool*visited=newbool[n];for(inti=0;i<n;i++)visited[i]=false;for(i=0;i<n;i++)if(!visited[i])DFS(i,visited);delete[]visited;}template<classT>voidMGraph<T>::BFS(intv,bool*visited){SeqQueue<int>q(n);visited[v]=true;cout<<""<<v;(v);while(!()){(v);();for(in
数据结构图的基本运算 来自淘豆网m.daumloan.com转载请标明出处.