C语言_数据结构图(留个笔记)

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());
}

运行

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页