C語言_資料結構圖(留個筆記)

銅仁學院_棧發表於2020-11-24

C語言_資料結構圖(留個筆記)

實現程式碼:

# include <stdio.h>
# include <stdlib.h>
# define N 3

static int k;
struct Enode
{
	short bh;
	float wigh;
	Enode *next;
};
struct Vnode
{
	char data;
	Enode *brother;
	bool flag;
};
struct Graph
{
	Vnode G[N];
	void Create();
	void Traverse();
	int Outdegree(int i);
	int Indegree(int i);
	int Edge();   //求圖的邊 無向圖入度和出二    有向圖入度和
	void DFTraverse(int i);//連通圖深度優先遍歷,從第i個頂點開始
};

void Graph::DFTraverse(int i)
{
	Enode *p; 
	printf("%c",G[i].data);   //輸出第一個結點
	G[i].flag = true;
	p = G[i].brother;
	
	while (p)
	{
		int w = p->bh;
		if (G[w].flag == false) DFTraverse(w);
		p = p->next;
	}
}

int Graph::Edge()
{
	int s;
	Enode *p;
	int j,edge = 0;
	printf("\t\t1、有向圖的邊數      2、無向圖的邊數");
	printf("請輸入選項:[ ]\b\b");
	scanf("%d",&s);
	switch (s)
	{
	case 1:
		{
			for (j = 0 ; j < N ; j++)
			{
				edge += Indegree(j);
			}
			break;
		}
	case 2:
		{
			for (j = 0 ; j < N ; j++)
			{
				edge += Outdegree(j);
			}
			edge = edge/2;
			break;
		}
	}
	return edge;
}

int Graph::Indegree(int i)      //入度
{
	Enode *p;
	int j,len = 0;
	for (j = 0 ;j < N ; j++)
	{
		p = G[j].brother;
		while (p)
		{
			if (p->bh == i)
				len++;
			p = p->next;
		}
	}
	return len;
}

int Graph::Outdegree(int i)    //無向圖  但也適合有向圖出度
{
	Enode *p;
	int len = 0;
	p = G[i].brother;
	while (p)
	{
		len++;
		p = p->next;
	}
	return len;
}

void Graph::Traverse()
{
	int i;
	Enode *p;
	switch (k)
	{
	case 1:
		{
			printf("\n\n無向圖如下:\n\n");
				for (i=0;i<N;i++)
			{
				printf("%d ",i);
				printf("%3c:",G[i].data);
				p=G[i].brother;
				while (p)
				{
					printf(" (%c,%c) ",G[i].data,G[p->bh].data);
					p=p->next;
				}
				printf("\n");
			}
			break;
		}
	case 2:
		{
			printf("\n\n有向圖如下:\n\n");
			for (i=0;i<N;i++)
			{
				printf("%d ",i);
				printf("%3c:",G[i].data);
				p=G[i].brother;
				while (p)
				{
					printf(" <%c,%c> ",G[i].data,G[p->bh].data);
					p=p->next;
				}
				printf("\n");
			}
			break;
		}
	case 3:
		{
			printf("\n\n無向網如下:\n\n");
			for (i=0;i<N;i++)
			{
				printf("%d ",i);
				printf("%3c:",G[i].data);
				p=G[i].brother;
				while (p)
				{
					printf(" (%c,%c)%g ",G[i].data,G[p->bh].data,p->wigh);
					p=p->next;
				}
				printf("\n");
			}
			break;
		}
	case 4:
		{
			printf("\n\n有向網如下:\n\n");
			for (i=0;i<N;i++)
			{
				printf("%d ",i);
				printf("%3c:",G[i].data);
				p=G[i].brother;
				while (p)
				{
					printf(" <%c,%c>%g ",G[i].data,G[p->bh].data,p->wigh);
					p=p->next;
				}
				printf("\n");
			}
			break;
		}
	}
}

void Graph::Create()
{
	int i,n,j;
	Enode *p,*q;
	printf("請輸入頂點上的字母:");  //一次性輸完
	for (i=0;i<N;i++)
	{
		G[i].data=getchar();
		G[i].flag=false;
		G[i].brother=NULL;
	}
	printf("\t\t1.無向圖    2.有向圖\n\n");
	printf("\t\t3.無向網    4.有向網\n\n");
	printf("請輸入建立型別編號:[ ]\b\b");
	scanf("%d",&k);
	switch (k)
	{
	case 1:
		{
			for (i=0;i<N;i++)
			{
				printf("請輸入邊數:");
				scanf("%d",&n);
				for (j=0;j<n;j++)
				{
					p=(Enode *)malloc(sizeof(Enode));
					printf("請輸入編號:");
					scanf("%d",&p->bh);
					p->next=NULL;
					if (G[i].brother==NULL)
					{
						G[i].brother=p;
						q=p;
					}
					else
					{
						q->next=p;
						q=p;
					}
				}
			}
			break;
		}
		case 2:
		{
			for (i=0;i<N;i++)
			{
				printf("請輸入出邊數:");
				scanf("%d",&n);
				for (j=0;j<n;j++)
				{
					p=(Enode *)malloc(sizeof(Enode));
					printf("請輸入編號:");
					scanf("%d",&p->bh);
					p->next=NULL;
					if (G[i].brother==NULL)
					{
						G[i].brother=p;
						q=p;
					}
					else
					{
						q->next=p;
						q=p;
					}
				}
			}
			break;
		}
		case 3:
		{
			for (i=0;i<N;i++)
			{
				printf("請輸入邊數:");
				scanf("%d",&n);
				for (j=0;j<n;j++)
				{
					p=(Enode *)malloc(sizeof(Enode));
					printf("請輸入編號:");
					scanf("%d",&p->bh);
					printf("請輸入權值:");
					scanf("%f",&p->wigh);
					p->next=NULL;
					if (G[i].brother==NULL)
					{
						G[i].brother=p;
						q=p;
					}
					else
					{
						q->next=p;
						q=p;
					}
				}
			}
			break;
		}
		case 4:
		{
			for (i=0;i<N;i++)
			{
				printf("請輸入出邊數:");
				scanf("%d",&n);
				for (j=0;j<n;j++)
				{
					p=(Enode *)malloc(sizeof(Enode));
					printf("請輸入編號:");
					scanf("%d",&p->bh);
					printf("請輸入權值:");
					scanf("%f",&p->wigh);
					p->next=NULL;
					if (G[i].brother==NULL)
					{
						G[i].brother=p;
						q=p;
					}
					else
					{
						q->next=p;
						q=p;
					}
				}
			}
			break;
		}
	}
}

void main()
{
	Graph g;
	g.Create();
	g.DFTraverse(0);
	printf("\n");
	g.Traverse();
	printf("\n入度為:%d\n",g.Indegree(2));
	printf("\n邊數為:%d\n",g.Edge());
}

執行

相關文章