C++ 十字链表图转java版

C++ 图


#include <iostream>  
#include <string>  
#include <queue>  
#include <stack>  
using namespace std;  
  
bool visited[100]; //顶点是否已被访问的标志数组  
  
//十字链表存储图  
//弧结点  
struct ArcBox  
{  
    //弧结点头尾结点位置  
    int tailvex;  
    int headvex;  
    //弧头和弧尾相同的弧的链域  
    ArcBox *hlink;  
    ArcBox *tlink;  
};  
  
//顶点节点  
struct VexNode  
{  
    string data; //顶点名称  
    ArcBox *firstin; //指向第一条入弧  
    ArcBox *firstout; //指向第一条出弧  
};  
  
class OLGraph  
{  
private:  
    VexNode *xlist; //指向顶点数组的指针  
    int vexnum;  //顶点数  
    int arcnum;  //弧数  
    int maxnum; //顶点数的最大值  
public:  
    OLGraph(int num=20)  
    {  
        xlist=new VexNode[num];  
        maxnum=num;  
    }  
      
    int Locate_Vex(string v)  
    {  
        for(int i=0;i<vexnum;i++)  
        {  
            if(xlist[i].data==v)  
                return i;  
        }  
        return -1;  
    }  
  
    void CreateDG_OLG()  
    {  
        //构造有向图  
        string v1,v2;  
        int i,j,k;  
        cout<<"输入顶点数和边的数目:";  
        cin>>vexnum>>arcnum;  
          
        while(vexnum>maxnum)       
        {  
            cout<<"顶点数目大于最大限制,请重新输入:";  
            cin>>vexnum;  
        }  
  
        cout<<"输入各个顶点的名称:";  
        for(i=0;i<vexnum;i++)  
        {  
            cin>>xlist[i].data;  
            xlist[i].firstin=NULL;  
            xlist[i].firstout=NULL;  
        }  
          
        for(k=0;k<arcnum;k++)  
        {  
            cout<<"输入第"<<k+1<<"条边的两个顶点(尾—>头的顺序):";  
            cin>>v1>>v2;  
            i=Locate_Vex(v1);  
            j=Locate_Vex(v2);  
              
            while(i == -1 || j == -1)  
            {  
                cout<<"结点位置输入错误,重新输入: ";  
                cin>>v1>>v2;  
                i=Locate_Vex(v1);  
                j=Locate_Vex(v2);     
            }         
              
            ArcBox *p=new ArcBox;  
            p->tailvex=i;  
            p->headvex=j;  
            p->hlink=xlist[j].firstin;  
            p->tlink=xlist[i].firstout;  
            xlist[i].firstout=xlist[j].firstin=p;  
        }  
  
        cout<<"有向图构造完成"<<endl;  
    }  
  
    //统计顶点入度  
    int In_degree(string v)  
    {  
        int pos=Locate_Vex(v);  
        if(pos == -1)  
        {  
            cout<<"结点不在图中"<<endl;  
            return -1;  
        }  
        ArcBox *p=xlist[pos].firstin;  
        int ins=0;  
        while(p)  
        {  
            ins++;  
            p=p->hlink;  
        }  
        return ins;  
    }  
  
    //统计顶点出度  
    int Out_degree(string v)  
    {  
        int pos=Locate_Vex(v);  
        if(pos == -1)  
        {  
            cout<<"结点不在图中"<<endl;  
            return -1;  
        }  
        ArcBox *p=xlist[pos].firstout;  
        int out=0;  
        while(p)  
        {  
            out++;  
            p=p->tlink;  
        }  
        return out;  
    }  
  
    //深度优先遍历  
    void DFS(int v)  
    {  
        visited[v]=true;  
        cout<<xlist[v].data<<"  ";  
        ArcBox *p=xlist[v].firstout;  
        while(p)  
        {  
            if(!visited[p->headvex])  
                DFS(p->headvex);  
            p=p->tlink;  
        }  
    }  
  
    void DFS_Traverse()  
    {  
        for(int i=0;i<vexnum;i++)  
            visited[i]=false;  
        for(int i=0;i<vexnum;i++) 
            if(!visited[i])  
                DFS(i); 	 
    }  
  
    //广度优先遍历  
    void BFS(int v)  
    {  
        visited[v]=true;  
        cout<<xlist[v].data<<"  ";  
        queue<int> qu;  
        int vex;  
        ArcBox *p;  
        qu.push(v);  
        while(!qu.empty())  
        {  
            vex=qu.front();  
            qu.pop();  
            p=xlist[vex].firstout;  
            while(p)  
            {  
                if(!visited[p->headvex])  
                {  
                    visited[p->headvex]=true;  
                    cout<<xlist[p->headvex].data<<"  ";  
                    qu.push(p->headvex);  
                }  
                p=p->tlink;  
            }  
        }  
    }  
  
    void BFS_Traverse()  
    {  
        for(int i=0;i<vexnum;i++)  
            visited[i]=false;  
        for(int i=0;i<vexnum;i++)  
            if(!visited[i])  
                BFS(i);  
    }  
  
    void DFS_2(int v)  
    {  
        visited[v]=true;  
        cout<<xlist[v].data<<"  ";  
        stack<int> s;  
        ArcBox *p;  
        int pos;  
        s.push(v);  
        while(!s.empty())  
        {  
            pos=s.top();  
            p=xlist[pos].firstout;  
            while(p && visited[p->headvex])  
                p=p->tlink;  
            if(!p)  
                s.pop();  
            else  
            {  
                visited[p->headvex]=true;  
                cout<<xlist[p->headvex].data<<"  ";  
                s.push(p->headvex);  
            }  
        }  
    }  
  
    void DFS_Traverse_2()  
    {  
        for(int i=0;i<vexnum;i++)  
            visited[i]=false;  
        for(int i=0;i<vexnum;i++)  
            if(!visited[i])  
                DFS_2(i);  
    }  
    //求连通分支数  
    int Connect_Cpnt()  
    {  
        for(int i=0;i<vexnum;i++)  
            visited[i]=false;  
        cout<<"下面的每一行显示一个连通分支:"<<endl;  
        int num=1;  
        DFS(0);  
        cout<<endl;  
        for(int i=0;i<vexnum;i++)  
        {  
            if(!visited[i])  
            {  
                num++;  
                DFS(i);  
                cout<<endl;  
            }  
        }  
        return num;  
    }  
};  

#include "Graphis.h"  
#include <iostream>  
#include <string>  
using namespace std;  
  
int main()  
{  
    OLGraph G;  
    string v;  
    int ins,out,a;  
    G.CreateDG_OLG();  
      
    cout<<"输入要统计哪个结点的入度:";  
    cin>>v;  
    ins=G.In_degree(v);  
    if(ins != -1)  
        cout<<"顶点"<<v<<"的入度为:"<<ins<<endl;  
  
    cout<<"输入要统计哪个结点的出度:";  
    cin>>v;  
    out=G.Out_degree(v);  
    if(out != -1)  
        cout<<"顶点"<<v<<"的出度为:"<<out<<endl;  
  
    cout<<"深度优先遍历:";  
    G.DFS_Traverse();  
    cout<<endl;  
  
    cout<<"广度优先遍历:";  
    G.BFS_Traverse();  
    cout<<endl;  
  
    cout<<"计算图的连通分支"<<endl;  
    int num=G.Connect_Cpnt();  
    cout<<"图的连通分支数目为:"<<num<<endl;  
  
    cout<<"深度优先遍历非递归:";  
    G.DFS_Traverse_2();  
    cout<<endl;  
  
    return 0;  
}  

java版


package graphis;

public class ArcBox {
	//弧结点头尾结点位置  
    int tailvex;  
    int headvex;  
    //弧头和弧尾相同的弧的链域  
    ArcBox hlink;  
    ArcBox tlink; 

}

package graphis;

public class VexNode {
	String data; //顶点名称  
    ArcBox firstin; //指向第一条入弧  
    ArcBox firstout; //指向第一条出弧  
}

package graphis;
import java.util.*;

public class OLGraph {
	
	  boolean visited[]=new boolean[100];
	    public  VexNode[] xlist=new VexNode[20]; //指向顶点数组的指针  
	    private int vexnum;  //顶点数  
	    private int arcnum;  //弧数  
	    private int maxnum; //顶点数的最大值  
	  
	public  OLGraph(int num)  
	    {  
	       this.xlist=new VexNode[num];
	       for(int i=0;i<20;i++)
			{
	    	   xlist[i]=new VexNode();
			}
	        maxnum=num;  
	    }  
	      
	    int Locate_Vex(String v)  
	    {  
	        for(int i=0;i<vexnum;i++)  
	        {  
	            if(xlist[i].data.equals(v); 	            	
	                return i;  
	        }  
	        return -1;  
	    }  
	  
	    void CreateDG_OLG()  
	    {  
	        //构造有向图  
	        String v1,v2;  
	        int i,j,k;  
	        System.out.println("输入顶点数和边的数目:");  
	        Scanner input = new Scanner(System.in);
	        vexnum = input.nextInt();
	        arcnum =  input.nextInt(); 
	          
	        while(vexnum>maxnum)       
	        {  
	        	System.out.println("顶点数目大于最大限制,请重新输入:");  
	            vexnum=input.nextInt(); ;  
	        }  
	  
	        System.out.println("输入各个顶点的名称:");  
	        for(i=0;i<vexnum;i++)  
	        {  
	            xlist[i].data=input.next();
	            xlist[i].firstin=null;  
	            xlist[i].firstout=null;  
	        }  
	          
	        for(k=0;k<arcnum;k++)  
	        {  
	        	System.out.println("输入第"+k+1+"条边的两个顶点(尾—>头的顺序):");
	            v1=input.next();
	            v2=input.next();  
	            i=Locate_Vex(v1);  
	            j=Locate_Vex(v2);  
	              
	            while(i == -1 || j == -1)  
	            {  
	            	 System.out.println("结点位置输入错误,重新输入: ");  
	            	v1=input.next();
	 	            v2=input.next();    
	                i=Locate_Vex(v1);  
	                j=Locate_Vex(v2);     
	            }         
	              
	            ArcBox p=new ArcBox();  
	            p.tailvex=i;  
	            p.headvex=j;  
	            p.hlink=xlist[j].firstin;  
	            p.tlink=xlist[i].firstout;  
	            xlist[i].firstout=xlist[j].firstin=p;  
	        }  
	  
	        System.out.println("有向图构造完成\n");  
	        input.close();
	    }  
	  
	    //统计顶点入度  
	    int In_degree(String v)  
	    {  
	        int pos=Locate_Vex(v);  
	        if(pos == -1)  
	        {  
	        	System.out.println("结点不在图中\n");  
	            return -1;  
	        }  
	        ArcBox p=xlist[pos].firstin;  
	        int ins=0;  
	        while(p!=null)  
	        {  
	            ins++;  
	            p=p.hlink;  
	        }  
	        return ins;  
	    }  
	  
	    //统计顶点出度  
	    int Out_degree(String v)  
	    {  
	        int pos=Locate_Vex(v);  
	        if(pos == -1)  
	        {  
	        	System.out.println("结点不在图中\n");  
	            return -1;  
	        }  
	        ArcBox p=xlist[pos].firstout;  
	        int out=0;  
	        while(p!=null)  
	        {  
	            out++;  
	            p=p.tlink;  
	        }  
	        return out;  
	    }  
	  
	    //深度优先遍历  
	    void DFS(int v)  
	    {  
	        visited[v]=true;  
	        System.out.println(xlist[v].data+"  ");  
	        ArcBox p=xlist[v].firstout;  
	        while(p!=null)  
	        {  
	            if(!visited[p.headvex])  
	                DFS(p.headvex);  
	            p=p.tlink;  
	        }  
	    }  
	  
	    void DFS_Traverse()  
	    {  
	        for(int i=0;i<vexnum;i++)  
	            visited[i]=false;  
	        for(int i=0;i<vexnum;i++) 
	            if(!visited[i])  
	                DFS(i); 	 
	    }  
	  
	    //广度优先遍历  
	    void BFS(int v)  
	    {  
	        visited[v]=true;  
	        System.out.println(xlist[v].data+"  ");  
	        Queue<Integer> qu= new LinkedList<Integer>();  
	        int vex;  
	        ArcBox p;  
	        qu.offer(v);  
	        while(qu.peek()!=null)  
	        {  
	            vex=qu.peek();  
	            qu.poll();  
	            p=xlist[vex].firstout;  
	            while(p!=null)  
	            {  
	                if(!visited[p.headvex])  
	                {  
	                    visited[p.headvex]=true;  
	                    System.out.println(xlist[p.headvex].data+"  ");  
	                    qu.offer(p.headvex);  
	                }  
	                p=p.tlink;  
	            }  
	        }  
	    }  
	  
	    void BFS_Traverse()  
	    {  
	        for(int i=0;i<vexnum;i++)  
	            visited[i]=false;  
	        for(int i=0;i<vexnum;i++)  
	            if(!visited[i])  
	                BFS(i);  
	    }  
	  
	    void DFS_2(int v)  
	    {  
	        visited[v]=true;  
	        System.out.println(xlist[v].data+"  ");  
	        Stack<Integer> s = new Stack<Integer>();   
	        ArcBox p;  
	        int pos;  
	        s.push(v);  
	        while(!s.empty())  
	        {  
	            pos=s.peek(); 
	            p=xlist[pos].firstout;  
	            while(p!=null && visited[p.headvex])  
	                p=p.tlink;  
	            if(p==null)  
	                s.pop();  
	            else  
	            {  
	                visited[p.headvex]=true;  
	                System.out.println(xlist[p.headvex].data+"  ");  
	                s.push(p.headvex);  
	            }  
	        }  
	    }  
	  
	    void DFS_Traverse_2()  
	    {  
	        for(int i=0;i<vexnum;i++)  
	            visited[i]=false;  
	        for(int i=0;i<vexnum;i++)  
	            if(!visited[i])  
	                DFS_2(i);  
	    }  
	    //求连通分支数  
	    int Connect_Cpnt()  
	    {  
	        for(int i=0;i<vexnum;i++)  
	            visited[i]=false;  
	        System.out.println("下面的每一行显示一个连通分支:\n");  
	        int num=1;  
	        DFS(0);  
	        System.out.println("\n");   
	        for(int i=0;i<vexnum;i++)  
	        {  
	            if(!visited[i])  
	            {  
	                num++;  
	                DFS(i);  
	                System.out.println("\n");  
	            }  
	        }  
	        return num;  
	    }  

}

package graphis;

import java.util.Scanner;

public class Test {
	public static void main(String args[]){
		
		OLGraph G=new OLGraph(20);  
	    String v;  
	    int ins,out;  
	    G.CreateDG_OLG();  
	      
	    System.out.println("输入要统计哪个结点的入度:"); 
	    Scanner input = new Scanner(System.in);
	    v=input.next();  
	    ins=G.In_degree(v);  
	    if(ins != -1)  
	    	 System.out.println("顶点"+v+"的入度为:"+ins+'\n');  
	  
	    	 System.out.println("输入要统计哪个结点的出度:");  
	    v=input.next(); 
	    out=G.Out_degree(v);  
	    if(out != -1)  
	    	System.out.println("顶点"+v+"的出度为:"+out+'\n');  
	  
	    System.out.println("深度优先遍历:");  
	    G.DFS_Traverse();  
	    System.out.println('\n');  
	  
	    System.out.println("广度优先遍历:");  
	    G.BFS_Traverse();  
	    System.out.println('\n');  
	  
	    System.out.println("计算图的连通分支"+'\n');  
	    int num=G.Connect_Cpnt();  
	    System.out.println("图的连通分支数目为:"+num+'\n');  
	  
	    System.out.println("深度优先遍历非递归:");  
	    G.DFS_Traverse_2();  
	    System.out.println('\n');  
	    input.close();
	    
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值