7-2 邻接表创建无向图 (20 分)

采用邻接表创建无向图G ,依次输出各顶点的度。
输入格式:

输入第一行中给出2个整数i(0 输出格式:

依次输出各顶点的度,行末没有最后的空格。
输入样例:

5 7
ABCDE
AB
AD
BC
BE
CD
CE
DE
输出样例:

2 3 3 3 3

#include
using namespace std;

typedef struct ENode{/表结点 
	char data;
	struct ENode *next; 
}ENode;

typedef struct VNode{  //头结点 
	char data;
	ENode *fistedges;
}VNode,vertex[20];

typedef struct{
	vertex v;
	int numNode,numedges;
}  Graph;

int find(Graph G,int c) 找弧尾 
{
	for(int i=0;i>G.numNode>>G.numedges;
	
	for(int i=0;i>a;
		G.v[i].data=a; 
		G.v[i].fistedges=NULL;
	}
	
	for(int i=0;i>a>>b;
		int p=find(G,a);
		int q=find(G,b);
		ENode *pre=(ENode*)malloc(sizeof(ENode));

/将b接在表头a的后面///		
		pre=G.v[p].fistedges;
		if(pre==NULL)
		{
			ENode *qre=(ENode*)malloc(sizeof(ENode));
			qre->data=b;
			qre->next=NULL;
			G.v[p].fistedges=qre; 
		}
		else
		{
			while(pre->next!=NULL)
			{
				pre=pre->next;
			}
			ENode *qre=(ENode*)malloc(sizeof(ENode));
			qre->data=b;
			qre->next=NULL;
			pre->next=qre;			
		}
//将a接到表头b的后面///		
		pre=G.v[q].fistedges;
		if(pre==NULL)
		{
			ENode *qre=(ENode*)malloc(sizeof(ENode));
			qre->data=a;
			qre->next=NULL;
			G.v[q].fistedges=qre; 
		}
		else
		{
			while(pre->next!=NULL)
			{
				pre=pre->next;
			}
			ENode *qre=(ENode*)malloc(sizeof(ENode));
			qre->data=a;
			qre->next=NULL;
			pre->next=qre;			
		}
	}
///输出各个顶点的度///	
	for(int i=0;inext)
		{
			s++;
		}
		if(i==0)
			cout<

你可能感兴趣的:(数据结构)