深度优先搜索、广度优先搜索及其生成树

#include
#include
#define MAXVEX 30
typedef  struct vertextype
{ 
	char nam[10];
}name;

typedef struct edgenode 
{
	int adjvex;
	int value;
struct edgenode *next;
}arcnode;

typedef struct vexnode
{
	name data;
	arcnode *firstarc;
}vheadnode;
typedef struct
{
	int n,e;
	vheadnode vhnode[MAXVEX];
}adjlist;

int count=0;

     int createadjlist(adjlist *&g)
     {
		int i,b,t,w;
     	arcnode *p,*q;
     	g=(adjlist *)malloc(sizeof(adjlist));
     	cout<<"请输入顶点数,边数:";
     	cin>>g->n>>g->e;
         for(i=0;in;i++)
         {

			 cout<<"  序号为"<>g->vhnode[i].data.nam;
             g->vhnode[i].firstarc=NULL;
             cout<e;i++)
         {
            cout<<" 序号为"<";
            cout<<" 起点序号 终点序号 权值:";
            cin>>b>>t>>w;
            if(b<0||b>g->n)
            {
				 cout<<"输入的起点序号不存在!"<g->n)
            {
				cout<<"输入的重点序号不存在!"<value=w;p->adjvex=t;
            p->next=g->vhnode[b].firstarc;
            g->vhnode[b].firstarc=p;
            q=(arcnode *)malloc(sizeof(arcnode));
            q->value=w;q->adjvex=b;
            q->next=g->vhnode[t].firstarc;
            g->vhnode[t].firstarc=q;
         }
         return 1;
     }

     void display(adjlist *g)
     {
		 int i;
		 arcnode *p;
		 cout<<"图的邻接表表示如下:"<n;i++)
		 {
			 cout<<"["<vhnode[i].data.nam<<"]=>";
			 p=g->vhnode[i].firstarc;
			  while(p!=NULL)
			  {
				  cout<<"("<adjvex<<","<value<<")->";
				  p=p->next;
			  }
		 cout<<"^\n";
		 }
     }
     void bfs(adjlist *g,int vi)//广度优先搜索
     {
		 int i,v,visited[MAXVEX],count;
		count=0;
		int Q[MAXVEX],front=0,rear=0;
		arcnode *p;
		 for(i=0;in;i++)
         visited[i]=0;
		visited[vi]=1;
		cout<vhnode[v].firstarc;
            while(p!=NULL)
            {   
				if(visited[p->adjvex]==0)
				{   
					visited[p->adjvex]=1;
					cout<adjvex<<" ";
					count++;
					if(count%5==0)cout<adjvex;
				}
              p=p->next;
            }
        }
     }
     void dfs(adjlist *g,int vi,int visited[])
     {
		 arcnode *p; 
         visited[vi]=1;
         cout<vhnode[vi].firstarc;
			while(p!=NULL)
			{
				if(visited[p->adjvex]==0)
				dfs(g,p->adjvex,visited);
				 p=p->next;
			}
     }
            void dfsb(adjlist *g,int start,int visited[])
            {
				arcnode *p;
                visited[start]=1;
                p=g->vhnode[start].firstarc;
               while(p!=NULL)
			   {        
				   if(visited[p->adjvex]==0)
                 {
                     cout<<"("<adjvex<<")";
                     count++;
                     if(count%5==0)cout<adjvex,visited);
                 }
                 p=p->next;
			   }
            }
         
	void bfsb(adjlist *g,int vi)
	{
		int i,v,visited[MAXVEX];
		int count=0;
        int Q[MAXVEX],front=0,rear=0;
        arcnode *p;
        for(i=0;in;i++) visited[i]=0;
        visited[vi]=1;
        rear=(rear=1)%MAXVEX;
        Q[rear]=vi;
        while(front!=rear)
		{
           front=(front+1)%MAXVEX;
           v=Q[front];
           p=g->vhnode[v].firstarc;
           while(p!=NULL)
           {   
				if(visited[p->adjvex]==0)
				{   
					visited[p->adjvex]=1;
					cout<<"("<adjvex<<")";
					count++;
					if(count%5==0) cout<adjvex;
				}
				p=p->next;
            }
       }
	}
     void main()
     {
	    adjlist *g;
        createadjlist(g);
        display(g);
        int s;
		cout<<"请输入起点位置:"<>s;
		int visited[MAXVEX];
		 for(int i=0;in;i++)
         visited[i]=0;
		cout<<"深度优先搜索序列:";
		dfs(g,s,visited);
		cout<n;i++)
         visited[i]=0;
        count=0;
		 dfsb(g,s,visited);
		cout<

你可能感兴趣的:(杂,C++)