(
(
(
(
解
(一)用鄰接矩陣表示
#define MaxVertexNum l
typedef char VertexType
typedef int EdgeType
typedef struct{
VextexType vexs[MaxVertexNum] //頂點表
EdeType edges[MaxVertexNum][MaxVertexNum];
//鄰接矩陣
int n
}MGragh
(
void AddVertexMGraph(MGraph *G
{//往無向圖的鄰接矩陣中插入頂點
if (G
Error(
G
G
}
(
void AddedgeMGraph(MGraph *G
{//往無向圖的鄰接矩陣中插入邊(x
int i
i=
for(k=
{ if (g
if (g
}
if (i==
else {//插入邊(x
g
g
G
}
}
(
void DeleteVertexMGraph(MGraph *G
{//無向圖的鄰接矩陣中刪除頂點x
int i
i=
for(k=
if (g
if (i==
else {//刪除頂點以及邊
k=
for (j=
if (g
G
for (k=i+
for(j=
g
for (k=i+
for(j=
g
G
}
}
(
void DeleteedgeMGraph(MGraph *G
{//無向圖的鄰接矩陣中刪除邊(x
int i
i=
for(k=
{ if (g
if (g
}
if (i==
else if (g
{//刪除邊(x
g
g
G
}
}
(二)用鄰接表表示
typedef struct node{//邊表結點
int adjvex
struct node *next
//若要表示邊上的權
}EdgeNode;
typedef struct vnode{ //頂點表結點
VertexType vertex
EdgeNode *firstedge
}VertexNode
typedef VertexNode AdjList[MaxVertexNum]
typedef struct{
AdjList adjlist
int n
}ALGraph
(
void AddVertexALGraPh(ALGrahp *G
{//往無向圖的鄰接表表示中插入一個頂點
if (G
Error(
G
G
G
}
(
void AddedgeALGraPh(ALGrahp *G
{//往無向圖的鄰接表中插入邊(x
int i
EdgeNode *s
i=
for(k=
{ if (G
if (G
}
if (i==
else {
s=G
while ((s)&&(s
s=s
if (!s)//當鄰接表中無邊(x
{
s=(EdgeNode *)malloc(sizeof(EdgeNode))
s
s
G
s=(EdgeNode *)malloc(sizeof(EdgeNode))
s
s
G
G
}
}
}
(
void DeleteVertexALGraPh(ALGrahp *G
{//無向圖的鄰接表中刪除頂點x
int i
EdgeNode *s
i=
for(k=
if (G
if (i==
else {//刪除與x相關聯的邊
s=G
while (s)
{p=G
if (p)&&(p
{G
free(p);
}
else//不是第一個 邊表結點
{while (p)&&(p
p=p
q=p
p
}
q=s;s=s
G
}
//調整頂點表
for (j=i;j<G
{G
G
}
G
}
}
(
void DeleteedgeALGraPh(ALGraph *G
{//往無向圖的鄰接表中刪除邊(x
int i
EdgeNode *s
i=
for(k=
{ if (G
if (G
}
if (i==
else {
s=G
if (s)&&(s
{G
free(s);
}
else{
while (s)&&(s
s=s
if (!s
From:http://tw.wingwit.com/Article/program/sjjg/201311/23627.html