邻接表存图(出边表+入边表)

本文介绍了一种使用邻接表存储图的数据结构,并提供了C语言实现。邻接表包括出边表(InputLine)和入边表(OutputLine),用于记录每个顶点的邻接点。代码中包含创建图、打印每个顶点的入边和出边数量,以及删除图的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "malloc.h"

#define MAX 100

struct ArcNode
{
	int adjvex;//临接的边序号.
	ArcNode *next;//下一个节点
};

struct Vnode
{
	int data;//顶点信息
	ArcNode *InputLine;//出边表
	ArcNode *OutputLine;//入边表
};

struct Graph
{
	Vnode vertexs[MAX];//顶点数组
	int vexnum;//顶点数
	int arcnum;//边数
};

Graph G;

void CreatGraph()
{
	int i;
	ArcNode *pi;
	int v1,v2;
	G.vexnum=0;
	G.arcnum=0;
	printf("please input Graph's vexnum or arcnum.\n");
	scanf("%d %d",&G.vexnum,&G.arcnum);
	for(i=0;i<G.vexnum;i++)
	{
		G.vertexs[i].InputLine=G.vertexs[i].OutputLine=NULL;
	}
	printf("please input partner number like X X by point to point.\n");
	for(i=0;i<G.arcnum;i++)
	{
		scanf("%d %d",&v1,&v2);
		v1--,v2--;
		//input line
		pi=(ArcNode*)malloc(sizeof(struct ArcNode));
		pi->adjvex=v2;
		pi->next=G.vertexs[v1].InputLine;
		G.vertexs[v1].InputLine=pi;
		//output line

		pi=(ArcNode*)malloc(sizeof(struct ArcNo
/* * 基于邻接边表实现结构 */ package dsa; public class Graph_List implements Graph { //变量 protected List E;//容器:中所有边 protected List V;//容器:中所有顶点 //构造方法 public Graph_List() { E = new List_DLNode(); V = new List_DLNode(); } //取边表、顶点表 protected List getE() { return E; } protected List getV() { return V; } //取中顶点、边的数目 public int vNumber() { return V.getSize(); } public int eNumber() { return E.getSize(); } //取中所有顶点、顶点位置的迭代器 public Iterator vertices() { return V.elements(); } public Iterator vPositions() { return V.positions(); } //返回中所有边、边位置的迭代器 public Iterator edges() { return E.elements(); } public Iterator ePositions() { return E.positions(); } //检测是否有某条边从顶点u指向v public boolean areAdjacent(Vertex u, Vertex v) { return (null != edgeFromTo(u, v)); } //取从顶点u指向v的边,若不在,则返回null public Edge edgeFromTo(Vertex u, Vertex v) { for (Iterator it = u.outEdges(); it.hasNext();) {//逐一检查 Edge e = (Edge)it.getNext();//以u为尾的每一条边e if (v == e.getVPosInV(1).getElem())//若e是(u, v),则 return e;//返回该边 } return null;//若不在这样的(u, v),则返回null } //将顶点v从顶点集中删除,并返回之 public Vertex remove(Vertex v) { while (0 < v.outDeg())//将以v为尾的所有边 remove((Edge)(((Vertex_List)v).outEdges.first()).getElem());//逐一删除 while (0 < v.inDeg())//将以v为头的所有边 remove((Edge)(((Vertex_List)v).inEdges.first()).getElem());//逐一删除 return (Vertex)V.remove(v.getVPosInV());//在顶点表中删除v } //将边e从边集中删除,并返回之 public Edge remove(Edge e) { ((Vertex_List)e.getVPosInV(0).getElem()).outEdges.remove(e.getEPosInI(0));//从起点的边表中删除e ((Vertex_List)e.getVPosInV(1).getElem()).inEdges.remove(e.getEPosInI(1));//从终点的边表中删除e return (Edge)E.remove(e.getEPosInE());//从边表中删除e } //在顶点集V中插新顶点v,并返回其位置 public Position insert(Vertex v) { return V.insertLast(v); } //在边集E中插新边e,并返回其位置 public Position insert(Edge e) { return E.insertLast(e); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值