#include<iostream.h>
typedef struct node
{
int order;
char data;
node *next;
bool tag;
}*ptrn;
class linklist
{
protected:
int curposition;
ptrn curptr;
int count;
public:
ptrn head;
void sethead(ptrn &head1)
{
head=head1;
curposition=0;
curptr=head;
count=0;
}
ptrn getelemptr(int position)
{
if(curposition>position)
{
curposition=0;
curptr=head;
}
for(;curposition<position;curposition++)
curptr=curptr->next;
return curptr;
}
int getcurposition()
{
return curposition;
}
int length()
{
ptrn temp;
temp=head;
while(1)
{
if(temp!=0)
{
count++;
temp=temp->next;
}
else
break;
}
return count;
}
int getelem(int position,int n)//获取链中第position位置的元素,其下标为n
{
if(position<1||position>length()+1)
cout<<"越界"<<endl;
else
{
ptrn tem;
tem=getelemptr(position);
n=tem->order;
return n;
}
}
};
class linkqueue
{
private:
int maxsize;
int *stack;
int top;
int count;
public:
linkqueue(int size)
{
top=0;
count=0;
maxsize=size;
stack=new int[size];
}
void inqueue(int v)
{
if(count<=maxsize)
{
stack[top]=v;
count++;
top++;
}
else
cout<<"栈满"<<endl;
}
void outqueue(int u)
{
if(cout==0)
cout<<"栈为空"<<endl;
else
{
u=stack[top];
top--;
count--;
}
}
bool empty()
{
return count==0;
}
};
typedef struct adjlistgraphvexnode
{
char ch;
linklist *adjlink;
}*pt;
class adjmatrixdirgraph
{
protected:
char *elem;
int vexnum,edgenum;
int **matrix;
bool tag1;
ptrn *vextable;
public:
pt *vexarray;
adjmatrixdirgraph(int size)
{
edgenum=0;
vexnum=size;
tag1=false;
vexarray=new pt[vexnum];
vextable=new ptrn[vexnum];
matrix=(int **)new int *[vexnum];
elem=new char[vexnum];
cout<<"按顺序输入所有顶点的元素"<<endl;
for(int i=0;i<vexnum;i++)
{
cin>>elem[i];
}
for(i=0;i<vexnum;i++)
matrix[i]=new int[vexnum];
}
void inputmatrix()
{
cout<<"请输入矩阵"<<endl;
for(int i=0;i<vexnum;i++)
for(int j=0;j<vexnum;j++)
cin>>matrix[i][j];
}
void Matrix_to_adjlist()
{
ptrn temp;
for(int i=0;i<vexnum;i++)
{
vextable[i]=new node;
vextable[i]->data=elem[i];
vextable[i]->order=i;
vextable[i]->tag=tag1;
}
for(i=0;i<vexnum;i++)
{
temp=vextable[i];
for(int j=0;j<vexnum;j++)
{
ptrn newnode;
newnode=new node;
if(matrix[i][j]==0){}
else
{
newnode->order=vextable[j]->order;
newnode->data=vextable[j]->data;
newnode->tag=vextable[j]->tag;
temp->next=newnode;
temp=temp->next;
}
}
temp->next=0;
}
}
void Adjlist_to_matrix()
{
ptrn temp;
for(int i=0;i<vexnum;i++)
{
temp=vextable[i];
int j=0;
while(1)
{
if(temp==0)break;
else if(vextable[i]->order==j)
{
matrix[i][j]=0;
temp=temp->next;
j++;
}
else if(temp->order==j)
{
matrix[i][j]=1;
temp=temp->next;
j++;
}
else
{
matrix[i][j]=0;
temp=temp->next;
}
}
}
}
void settag(int v,bool val)
{
if(v<0||v>=vexnum)
cout<<"越界"<<endl;
else
vextable[v]->tag=val;
}
bool gettag(int v)
{
if(v<0||v>=vexnum)
cout<<"越界"<<endl;
else
return vextable[v]->tag;
}
void getnode(int v,char ch)
{
if(v<0||v>=vexnum)
cout<<"越界"<<endl;
else
ch=vextable[v]->data;
}
int indexhelp(linklist *la,int v)//定位顶点v在链表中的位置
{
int curpos,adjv;
curpos=la->getcurposition();
adjv=la->getelem(curpos,adjv);
if(adjv==v)return curpos;
else
{
curpos=1;
for(curpos=1;curpos<=la->length();curpos++)
{
adjv=la->getelem(curpos,adjv);
if(adjv==v)break;
}
return curpos;
}
}
int firstadjvex(int v)//返回顶点V的第一个邻接点
{
if(v<0||v>=vexnum)
{cout<<"越界"<<endl;}
else
{
pt newnode=new adjlistgraphvexnode;
vexarray[v]=newnode;
vexarray[v]->adjlink=new linklist;
ptrn e=vextable[v];
vexarray[v]->ch=e->data;
vexarray[v]->adjlink->sethead(e);
if(vexarray[v]->adjlink->head==0)
{return -1;}
else
{
int adjv;
adjv=vexarray[v]->adjlink->getelem(1,adjv);
return adjv;
}
}
}
int nextadjvex(int v1,int v2)//返回顶点V1的相对于V2的下一个邻接点
{
if(v1<0||v1>=vexnum)cout<<"v1不合法"<<endl;
if(v2<0||v2>=vexnum)cout<<"v2不合法"<<endl;
if(v1==v2)cout<<"两者不能相等"<<endl;
else
{
pt newnode=new adjlistgraphvexnode;
vexarray[v1]=newnode; vexarray[v1]->adjlink=new linklist;
ptrn e=vextable[v1];
vexarray[v1]->adjlink->sethead(e);
if(vexarray[v1]->adjlink->head==0)
{
cout<<"没有以"<<v1<<"顶点为头结点的链表"<<endl;
return -1;
}
else
{
int cur=indexhelp(vexarray[v1]->adjlink,v2);
if(cur<vexarray[v1]->adjlink->length())
{
int adjv;
adjv=vexarray[v1]->adjlink->getelem(cur+1,adjv);
return adjv;
}
else
{
cout<<v1<<"的邻接点没有"<<v2<<endl;
return -1;
}
}
}
}
void DFS(int v)
{
settag(v,true);
char ch1;
getnode(v,ch1);
cout<<ch1<<" ";
for(int w=firstadjvex(v);w>=0;w=nextadjvex(v,w))
{
if(!gettag(w))
DFS(w);
}
}
void DFStravese()
{
int v;
for(v=0;v<vexnum;v++)
{
settag(v,false);
}
for(v=0;v<vexnum;v++)
{
if(!gettag(v))
DFS(v);
}
}
void BFS(int v)
{
settag(v,true);
char m;
getnode(v,m);
cout<<m<<" ";
linkqueue q(vexnum);
q.inqueue(v);
while(!q.empty())
{
int u,w;
q.outqueue(u);
for(w=firstadjvex(u);w>=0;w=nextadjvex(u,w))
{
if(!gettag(w))
{
settag(w,true);
char e;
getnode(w,e);
cout<<e<<" ";
q.inqueue(w);
}
}
}
}
void BFStravese()
{
int v;
for(v=0;v<vexnum;v++)
{
settag(v,false);
}
for(v=0;v<vexnum;v++)
{
if(!gettag(v))
BFS(v);
}
}
void outputmatrix()
{
for(int i=0;i<vexnum;i++)
{
for(int j=0;j<vexnum;j++)
{cout<<matrix[i][j]<<" ";}
cout<<endl;
}
}
void outputadjlist()
{
for(int i=0;i<vexnum;i++)
{
ptrn temp;
temp=vextable[i];
while(1)
{
if(temp!=0)
{
cout<<temp->order<<temp->data<<" ";
temp=temp->next;
}
else
break;
}
cout<<endl;
}
}
};
void main()
{
adjmatrixdirgraph adj(9);
adj.inputmatrix();
adj.outputmatrix();
adj.Matrix_to_adjlist();
adj.outputadjlist();
adj.Adjlist_to_matrix();
adj.outputmatrix();
adj.DFStravese();
adj.BFStravese();
}
图的矩阵与邻接矩阵转换和遍历
原文作者:数据结构之图
原文地址: https://blog.csdn.net/riyuedangkong1/article/details/51049615
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
原文地址: https://blog.csdn.net/riyuedangkong1/article/details/51049615
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。