数据结构08图的表的表示方法

图的表的表示方法
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


提示:以下是本篇文章正文内容,下面案例可供参考

一、main.cpp

#include"graphlink.h"

void main()
{
	graphlink glink;
	initlink(&glink);
	insertvertex(&glink,'A');
	insertvertex(&glink,'B');
	insertvertex(&glink,'C');
	insertvertex(&glink,'D');
	insertvertex(&glink,'E');


	insertedge(&glink,'A','B');
	insertedge(&glink,'A','D');
	insertedge(&glink,'B','C');
	insertedge(&glink,'B','E');
	insertedge(&glink,'D','C');
	insertedge(&glink,'C','E');

	showgraph(&glink);
	printf("\n");
	removeedge(&glink,'A','B');
	showgraph(&glink);
	printf("\n");

	int i=getfirstneighbor(&glink,'A');
	int j=getnextneighbor(&glink,'A','B');
	printf("%d%d",i,j);
}

二、graphlink.cpp

#include"graphlink.h"

void initlink(graphlink *gl)
{
	gl->arcnum=gl->vexnum=0;
	for(int i=0;i<defaultnum;i++)
	{
		gl->vertice[i].firstac=(aclink)malloc(sizeof(acnode));
		gl->vertice[i].firstac->next=NULL;
	}
}


void insertvertex(graphlink *gl,vertextype t)
{
	if(gl->vexnum>=defaultnum)
		return;
	gl->vertice[gl->vexnum++].data=t;
}


void showgraph(graphlink *gl)
{
	for(int i=0;i<gl->vexnum;i++)
	{
		printf("%d ",i);
		printf("%c :",gl->vertice[i].data);
		aclink vl=gl->vertice[i].firstac->next;
		while(vl!=NULL)
		{
			printf("%d ->",vl->adj);
			vl=vl->next;
		}
		printf("null\n");
	}
	
}


int findpos(graphlink *gl,vertextype t)
{
	for(int i=0;i<gl->vexnum;i++)
	{
		if(gl->vertice[i].data==t)
			return i;
	}
	return -1;
}


void insertedge(graphlink *gl,vertextype t1,vertextype t2)
{
	int p=findpos(gl,t1);
	int w=findpos(gl,t2);
	if(p==-1||w==-1)
		return;
	aclink vl=gl->vertice[p].firstac->next;
		while(vl!=NULL&&vl->next!=NULL)
		{
			vl=vl->next;
		}
		aclink nl=(aclink)malloc(sizeof( acnode));
		nl->adj=w;
		nl->next=NULL;
		if(!vl)
			gl->vertice[p].firstac->next=nl;
		else
			vl->next=nl;

	aclink wl=gl->vertice[w].firstac->next;
		while(wl!=NULL&&wl->next!=NULL)
		{
			wl=wl->next;
		}
		aclink n=(aclink)malloc(sizeof(acnode));
		n->adj=p;
		n->next=NULL;
		if(!wl)
			gl->vertice[w].firstac->next=n;
		else
			wl->next=n;

		gl->arcnum++;
}



void destroynode(aclink &vl)
{
	if(!vl)
		return;
	vl=vl->next;
	free(vl);
	vl=NULL;
}



void removeedge(graphlink *gl,vertextype t1,vertextype t2)
{
	int p=findpos(gl,t1);
	int w=findpos(gl,t2);
	if(p==-1||w==-1)
		return;
	aclink vl=gl->vertice[p].firstac;
	aclink pl;
	while(vl&&vl->adj!=w)
	{
		pl=vl;
		vl=vl->next;
	}
	if(!vl)
		return;
	pl->next=vl->next;
	free(vl);
	vl=NULL;


	 vl=gl->vertice[w].firstac;
	while(vl&&vl->adj!=p)
	{
		pl=vl;
		vl=vl->next;
	}
	if(!vl)
		return;
	pl->next=vl->next;
	free(vl);
	vl=NULL;
}




/*
void destroygraph(graphlink *gl)
{
	aclink vl;
	for(int i=0;i<gl->vexnum;i++)
	{
		vl=gl->vertice[i].firstac;
			destroynode(vl);
	}
	free(gl->vertice);
	gl->vertice=NULL;
	gl->vexnum=gl->arcnum=0;
}

  */
int getfirstneighbor(graphlink *gl,vertextype t)
{
	int p=findpos(gl,t);
	if(p==-1)
		return -1;
	aclink vl=gl->vertice[p].firstac->next;
	return vl->adj;

}

int getnextneighbor(graphlink *gl,vertextype t1,vertextype t2)
{
	int p=findpos(gl,t1);
	int w=findpos(gl,t2);
	if(p==-1)
		return -1;
	aclink vl=gl->vertice[p].firstac->next;
	while(vl&&vl->adj!=w)
		vl=vl->next;
	if(!vl)
		return -1;
	return vl->next->adj;
}



三、graphlink.h

#pragma once

#include<stdio.h>
#include<assert.h>
#include<malloc.h>

#define defaultnum 10

#define vertextype char


typedef struct acnode
{
	int adj;
	struct acnode *next;
}acnode,*aclink;

typedef struct vnode
{
	vertextype data;
	aclink firstac;
}vnode,*vlink;

typedef struct
{
	vnode vertice[defaultnum];
	int vexnum,arcnum;
}graphlink;

void initlink(graphlink *gl);
void insertvertex(graphlink *gl,vertextype t);
void showgraph(graphlink *gl);
void insertedge(graphlink *gl,vertextype t1,vertextype t2);
int findpos(graphlink *gl,vertextype t);
void removeedge(graphlink *gl,vertextype t1,vertextype t2);
void destroygraph(graphlink *gl);
int getfirstneighbor(graphlink *gl,vertextype t);
int getnextneighbor(graphlink *gl,vertextype t1,vertextype t2);
void destroynode(aclink &vl);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值