#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#define Max_VertexNum 50 //允许图的顶点个数的最大值
typedef char VertexType; //定义数据元素(顶点)类型为char
//********************************************************************************
//邻接表存储结构
struct EdgeNode //定义边存储结点
{
int adjvex; //邻接点的存储位置
EdgeNode *next; //指向下邻接点
};
struct VertexNode //定义顶点存储结点
{
VertexType vertex; //数据元素
struct EdgeNode *link; //第一个邻接点
};
typedef struct Graph //定义邻接表图结构
{
int VexNum; //图的顶点个数
VertexNode Nodetable[Max_VertexNum]; //一维数组-邻接表
} Graphlnk; //定义邻接表存储的图类型
//**********************************************************************************
// 基于邻接表存储的 无向、非加权图的各种操作的实现
//** 创建图
void create_graph(Graphlnk &g)
{
VertexType v1, v2;
int i, j;
struct EdgeNode *p, *q;
cin >> g.VexNum; //读入图的顶点个数
while (g.VexNum < 0)
cin >> g.VexNum;
for (i = 0; i < g.VexNum; i++)
{
cin >> g.Nodetable[i].vertex; //输入顶点元素
g.Nodetable[i].link = NULL; //邻接表初始化
}
cin >> v1 >> v2; //输入边的两个顶点
while (v1 != '*'&&v2 != '*')
{
for (i = 0; i < g.VexNum; i++)
if (g.Nodetable[i].vertex == v1) break;
for (j = 0; j < g.VexNum; j++)
if (g.Nodetable[j].vertex == v2) break;
if (i >= g.VexNum || j >= g.VexNum) cin >> v1 >> v2; //边顶点不正确,重新读
else //链入邻接点
{
p = (struct EdgeNode *)malloc(sizeof(struct EdgeNode));
p->adjvex = j;
p->next = g.Nodetable[i].link;
g.Nodetable[i].link = p;
q = (struct EdgeNode *)malloc(sizeof(struct EdgeNode));
q->adjvex = i;
q->next = g.Nodetable[j].link;
g.Nodetable[j].link = q;
cin >> v1 >> v2;
}
}
}
void print_graph(Graphlnk g)
{
int i;
struct EdgeNode *p;
cout << "Adjacency List is:" << endl;
for (i = 0; i < g.VexNum; i++)
{
cout << g.Nodetable[i].vertex << ":";
p = g.Nodetable[i].link;
while (p != NULL)
{
cout << "-->" << g.Nodetable[p->adjvex].vertex;
p = p->next;
}
cout << endl;
}
}
//**********************************************************************
///补充 插入边、删除边的函数
//k = Insert_Edge(g, vi, vj);
//k = Delete_Edge(g, vi, vj);
int Insert_Edge(Graphlnk &g, VertexType v1, VertexType v2)
{
int i,j;
struct EdgeNode *p, *q,*temp;
for (i = 0; i < g.VexNum; i++)
if (g.Nodetable[i].vertex == v1) break;
for (j = 0; j < g.VexNum; j++)
if (g.Nodetable[j].vertex == v2) break;
if (i >= g.VexNum || j >= g.VexNum) return -1; //边顶点不正确,重新读
else if (i==j) return 0;///边不存在
else //链入邻接点
{
int f=1;
p = (struct EdgeNode *)malloc(sizeof(struct EdgeNode));
p = g.Nodetable[i].link;
while (p != NULL&&f==1)
{
if(g.Nodetable[p->adjvex].vertex==v2) f=0;
p = p->next;
}
if(f==1)
{
temp = (struct EdgeNode *)malloc(sizeof(struct EdgeNode));
temp->adjvex = j;
temp->next = g.Nodetable[i].link;
g.Nodetable[i].link = temp;
}
if(f==0) return 0;
f=1;
q = (struct EdgeNode *)malloc(sizeof(struct EdgeNode));
q = g.Nodetable[j].link;
while (q != NULL&&f==1)
{
if(g.Nodetable[q->adjvex].vertex==v1)f=0;
q= q->next;
}
if(f==1)
{
temp = (struct EdgeNode *)malloc(sizeof(struct EdgeNode));
temp->adjvex = i;
temp->next = g.Nodetable[j].link;
g.Nodetable[j].link = temp;
}
if(f==0)return 0;
return 1;
}
}
int Delete_Edge(Graphlnk &g, VertexType v1, VertexType v2)
{
int i,j;
struct EdgeNode *p, *q,*last1,*last2;
while (v1 != '*'&&v2 != '*')
{
for (i = 0; i < g.VexNum; i++)
if (g.Nodetable[i].vertex == v1) break;
for (j = 0; j < g.VexNum; j++)
if (g.Nodetable[j].vertex == v2) break;
if (i >= g.VexNum || j >= g.VexNum) return -1; //边顶点不正确,重新读
else if ( g.Nodetable[i].link==NULL|| g.Nodetable[j].link==NULL) return 0;///边不存在
else if(i==j) return 0;
else //链入邻接点
{
p = (struct EdgeNode *)malloc(sizeof(struct EdgeNode));
last1 = (struct EdgeNode *)malloc(sizeof(struct EdgeNode));
p = g.Nodetable[i].link;
last1 = NULL;
int f=0;
while (1)
{
if(g.Nodetable[p->adjvex].vertex!=v2&&p!=NULL)
{
last1=p;
p = p->next;
}
else
{
f=1;
break;
}
}
if(f==0) return 0;
// cout<<g.Nodetable[p->adjvex].vertex<<endl;
if(last1==NULL)
{
g.Nodetable[i].link=p->next;
delete p;
}
else if(p->next!=NULL)
{
last1->next=p->next;
delete p;
}
else
{
last1->next=NULL;
}
f=0;
q = (struct EdgeNode *)malloc(sizeof(struct EdgeNode));
last2 = (struct EdgeNode *)malloc(sizeof(struct EdgeNode));
q = g.Nodetable[j].link;
last2= NULL;
while (1)
{
if(g.Nodetable[q->adjvex].vertex!=v1&&q!=NULL)
{
last2=q;
q = q->next;
}
else
{
f=1;
break;
}
}
if(f==0) return 0;
// cout<<g.Nodetable[p->adjvex].vertex<<endl;
if(last2==NULL)
{
g.Nodetable[j].link=q->next;
delete q;
}
else if(q->next!=NULL)
{
last2->next=q->next;
delete q;
}
else
{
last2->next=NULL;
}
return 1;
}
}
}
//1
//5
//abcde
//ab
//ac
//ad
//ae
//bc
//bd
//be
//**
//2
//**********************************************************************
int main()
{
Graphlnk g;
int ic;
VertexType vi, vj;
int k;
while (1)
{
//请输入要执行的操作:";
cin >> ic;
while (ic < 0 || ic>4)
cin >> ic;
if (ic == 1) create_graph(g); //创建图
if (ic == 2) print_graph(g); //输出图结构
if (ic == 3) //插入边
{
cin >> vi >> vj;
k = Insert_Edge(g, vi, vj);
if (k == -1) cout << "Error:Vertex does not exist!" << endl;
if(k==0) cout << "Error:Edge repetition!" << endl;
if(k==1) cout << "Edge insertion succeeded!" << endl;
}
if (ic == 4) //删除边
{
cin >> vi >> vj;
k = Delete_Edge(g, vi, vj);
if (k == -1) cout << "Error:Vertex does not exist!." << endl;
if (k == 0) cout << "Error:Edge does not exist!" << endl;
if (k == 1) cout << "Edge deletion succeeded!" << endl;
}
if (ic == 0) break;
}
return 0;
}