题目:自己画一张图,建立邻接矩阵,并求出每个顶点的度
#include "AdjMGraph.h"
void CreatGraph(AdjMGraph *g, DataType V[],int n, RowColWeight E[], int e)//创建图
{
Initiate(g, n);
for (int i = 0; i < n; i++)
InsertVertex(g, V[i]);
for (int i = 0; i < e; i++)
InsertEdge(g, E[i].row, E[i].col, E[i].weight);
}
void GetDu(AdjMGraph g)//得到节点的度
{
int n = 0;
printf("出度:
");
for (int i = 0; i < g.Vertices.size; i++)
{
n = 0;
for (int j = 0; j < g.Vertices.size; j++)
{
if (g.edge[i][j] != 0 && g.edge[i][j] < MaxWeight)
n++;
}
printf("%c :%d
", g.Vertices.list[i], n);
}
printf("入度:
");
for (int j = 0; j < g.Vertices.size; j++)
{
n = 0;
for (int i = 0; i < g.Vertices.size; i++)
{
if (g.edge[i][j] != 0 && g.edge[i][j] < MaxWeight)
n++;
}
printf("%c :%d
", g.Vertices.list[j], n);
}
}
int main()
{
AdjMGraph g;
DataType v[] = { 'A','B','C','D' };
RowColWeight rcw[] = { {0,1,1},{1,2,1},{2,3,1},{3,0,1},{0,2,1} };
CreatGraph(&g, v, 4, rcw, 5);
printf("%顶点序列:
");
for (int i = 0; i < g.Vertices.size; i++)
printf("%c ", g.Vertices.list[i]);
printf("
");
printf("邻接矩阵:
");
for (int i = 0; i < g.Vertices.size; i++)
{
for (int j = 0; j < g.Vertices.size; j++)
printf("(%c,%c):%5d ", g.Vertices.list[i], g.Vertices.list[j], g.edge[i][j]);
printf("
");
}
printf("
");
printf("顶点的度:
");
GetDu(g);
return 0;
}
运行结果:
*头文件:*
SeqList:
#pragma once
#pragma once
#include "stdio.h"
#define MaxSize 100
typedef int DataType;
typedef struct {
DataType list[MaxSize];
int size;
}SeqList;
void ListInitiate(SeqList *L) {//初始化顺序表
L->size = 0;
}
int ListLength(SeqList L) {//返回顺序表长度
return L.size;
}
int ListInerst(SeqList *L, int i, DataType x) {//插入元素
int j;
if (L->size >= MaxSize) {
printf("顺序表已满无法插入!");
return -1;
}
else if (i<0 || i>L->size) {
printf("输入参数有误!");
return -1;
}
else {
for (int j = L->size; j > i; j--)
L->list[j] = L->list[j - 1];
L->list[i] = x;
L->size++;
return 1;
}
}
int ListDelete(SeqList *L, int i, DataType *x) {//删除元素
int j;
if (L->size <= 0) {
printf("顺序表已空,无数据可删!");
return -1;
}
else if (i<0 || i>L->size - 1) {
printf("输入参数有误!");
return -1;
}
else {
*x = L->list[i];
for (j = i + 1; j <= L->size - 1; j++)
L->list[j - 1] = L->list[j];
L->size--;
return 1;
}
}
int ListGet(SeqList L, int i, DataType *x) {//取出索引为i处的元素
if (i < 0 || i>L.size - 1) {
printf("参数不合法!");
return -1;
}
else {
*x = L.list[i];
return 1;
}
}
int ListFind(SeqList L, DataType x) {//查找元素并返回索引
int i;
bool hasFind = false;
for (i = 0; i < L.size; i++) {
if (x == L.list[i]) {
return i;
hasFind = true;
break;
}
}
if (!hasFind) {
printf("顺序表中没有该数据!
");
return -1;
}
}
int SortListInerst(SeqList *L, DataType x) {
if (L->size == 0) {
L->list[0] = x;
L->size++;
return 1;
}
else {
for (int i = 0; i < L->size; i++)
if (x < L->list[i]) {
for (int j = L->size; j > i; j--)
L->list[j] = L->list[j - 1];
L->list[i] = x;
break;
}
else
L->list[L->size] = x;
L->size++;
return 1;
}
}
AdjMGraph.h
#pragma once
#include "Limits.h"
#include"SeqList.h"
#define MaxVertices 100
#define MaxWeight 100
typedef struct
{
SeqList Vertices; //存放结点的顺序表
int edge[MaxVertices][MaxVertices]; //存放边的邻接矩阵
int numOfEdges; //边的条数
}AdjMGraph; //边的结构体定义
typedef struct
{
int row;
int col;
int weight;
}RowColWeight;
void Initiate(AdjMGraph *G, int n) //初始化
{
int i, j;
for (i = 0; i<n; i++)
for (j = 0; j<n; j++)
{
if (i == j)
G->edge[i][j] = 0;
else
G->edge[i][j] = MaxWeight;
}
G->numOfEdges = 0; //边的条数置为0
ListInitiate(&G->Vertices); //顺序表初始化
}
void InsertVertex(AdjMGraph *G, DataType vertex) //在图G中插入结点vertex
{
ListInerst(&G->Vertices, G->Vertices.size, vertex); //顺序表尾插入
}
void InsertEdge(AdjMGraph *G, int v1, int v2, int weight)
//在图G中插入边<v1,v2>,边<v1,v2>的权为weight
{
if (v1<0 || v1>G->Vertices.size || v2<0 || v2>G->Vertices.size)
{
printf("参数v1或v2越界出错!
");
return;
}
G->edge[v1][v2] = weight;
G->numOfEdges++;
}
void DeleteEdge(AdjMGraph *G, int v1, int v2) //在图中删除边<v1,v2>
{
if (v1<0 || v1>G->Vertices.size || v2<0 || v2>G->Vertices.size || v1 == v2)
{
printf("参数v1或v2越界出错!
");
return;
}
if (G->edge[v1][v2] == MaxWeight || v1 == v2)
{
printf("该边不存在!
");
return;
}
G->edge[v1][v2] = MaxWeight;
G->numOfEdges--;
}
void DeleteVerten(AdjMGraph *G, int v) //删除结点v
{
int n = ListLength(G->Vertices), i, j;
DataType x;
for (i = 0; i<n; i++) //计算删除后的边数
{
for (j = 0; j<n; j++)
if ((i == v || j == v) && G->edge[i][j]>0 && G->edge[i][j]<MaxWeight)
G->numOfEdges--; //计算被删除边
}
for (i = v; i<n; i++) //删除第v行
{
for (j = 0; j<n; j++)
G->edge[i][j] = G->edge[i + 1][j];
}
for (i = 0; i<n; i++) //删除第v列
{
for (j = v; j<n; j++)
G->edge[i][j] = G->edge[i][j + 1];
}
ListDelete(&G->Vertices, v, &x); //删除结点v
}
int GetFistVex(AdjMGraph *G, int v)
//在图G中寻找序号为v的结点的第一个邻接结点
//如果这样的邻接结点存在,返回该邻接结点的序号;否则,返回-1
{
int col;
if (v<0 || v>G->Vertices.size)
{
printf("参数v1越界出错!
");
return 0;
}
for (col = 0; col<G->Vertices.size; col++)
if (G->edge[v][col]>0 && G->edge[v][col]<MaxWeight)return col;
return -1;
}
int GetNextVex(AdjMGraph*G, int v1, int v2)
//在图中寻找v1结点的邻接结点v2的下一个邻接结点
//如果这样的结点存在,返回该邻接结点的序号;否则,返回-1
//v1和v2都是相应结点的序号
{
int col;
if (v1<0 || v1>G->Vertices.size || v2<0 || v2>G->Vertices.size)
{
printf("参数v1或v2越界出错!
");
return 0;
}
for (col = v2 + 1; col<G->Vertices.size; col++)
if (G->edge[v1][col]>0 && G->edge[v1][col]<MaxWeight)return col;
return -1;
}