30 邻接表:删除一个顶点
作者: 冯向阳时间限制: 1S章节: DS:图
截止日期: 2022-06-30 23:55:00
问题描述 :
目的:使用C++模板设计并逐步完善图的邻接表抽象数据类型(ADT)。
内容:(1)请参照图的邻接矩阵模板类原型,设计并逐步完善图的邻接表ADT。(由于该环境目前仅支持单文件的编译,故将所有内容都集中在一个源文件内。在实际的设计中,推荐将抽象类及对应的派生类分别放在单独的头文件中。)
(2)设计并实现一个算法,在已存在的图中查找指定元素值的结点,如查找成功,则删除之(包括与之相关的边);否则,返回。图的存储结构采用邻接表。将其加入到ADT中。
注意:DG(有向图), DN(有向网), UDG(无向图), UDN(无向网)
参考函数原型:
//往G中删除一个顶点
template<class TypeOfVer, class TypeOfEdge>
bool adjlist_graph<TypeOfVer, TypeOfEdge>::DeleteVer(const int u);
图的邻接表模板类原型参考如下:
/* 边表的结点定义 */
template<class TypeOfEdge>
struct edgeNode
{
int data;
TypeOfEdge weight;
edgeNode<TypeOfEdge> *next;
edgeNode(const int &d, edgeNode<TypeOfEdge> *ptr = NULL) //构造函数,用于构造其他结点(无权图)
//函数参数表中的形参允许有默认值,但是带默认值的参数需要放后面
{
next = ptr;
data = d;
}
edgeNode(const int &d, const TypeOfEdge &w, edgeNode<TypeOfEdge> *ptr = NULL) //构造函数,用于构造其他结点(带权图)
//函数参数表中的形参允许有默认值,但是带默认值的参数需要放后面
{
next = ptr;
data = d;
weight = w;
}
int getData(){ return data;} //取得结点的序号(顶点集)
TypeOfEdge getWeight(){ return weight;} //取得边集中对应边的权值
void SetLink( edgeNode<TypeOfEdge> *link ){ next = link; } //修改结点的next域
void SetData( int value ){ data = value; } //修改结点的序号(顶点集)
void SetWeight(TypeOfEdge value ){ weight = value; } //修改边集中对应边的权值
};
//图的邻接表类
template<class TypeOfVer, class TypeOfEdge>
struct verNode
{
TypeOfVer ver;
edgeNode<TypeOfEdge> *head;
verNode(edgeNode<TypeOfEdge> *h = NULL){head = h;}
TypeOfVer getVer(){ return ver;} //取得结点值(顶点集)
edgeNode<TypeOfEdge> *getHead(){ return head;} //取得对应的边表的头指针
void setVer(TypeOfVer value){ ver = value;} //设置结点值(顶点集)
void setHead(edgeNode<TypeOfEdge> *value){ head = value;} //设置对应的边表的头指针
};
template <class TypeOfVer, class TypeOfEdge>
class adjlist_graph{
private:
int Vers; //顶点数
int Edges; //边数
verNode<TypeOfVer,TypeOfEdge> *verList;
string GraphKind; //图的种类标志
bool Delete_Edge( int u, int v );
bool DFS(int u, int &num, int visited[]); //DFS遍历(递归部分)
public:
adjlist_graph( const string &kd, int vSize, const TypeOfVer d[]); //构造函数构造一个只有结点没有边的图。
adjlist_graph( const string &kd, int vSize, int eSize, const TypeOfVer d[], int **e); 构造函数构造一个无权图。5个参数的含义:图的类型、结点数、边数、结点集和边集
adjlist_graph( const string &kd, int vSize, int eSize, const TypeOfVer d[], int **e, const TypeOfEdge w[]); //构造函数构造一个有权图。
bool GraphisEmpty() { return Vers == 0; } //判断图空否
string GetGraphKind(){ return GraphKind; }
bool GetVer(int u, TypeOfVer &data); //取得G中指定顶点的值
int GetFirstAdjVex(int u, int &v); //返回G中指定顶点u的第一个邻接顶点的位序(顶点集)。若顶点在G中没有邻接顶点,则返回-1
int GetNextAdjVex(int u, int v, int &w); //返回G中指定顶点u的下一个邻接顶点(相对于v)的位序(顶点集)。若顶点在G中没有邻接顶点,则返回false
bool PutVer(int u, TypeOfVer data); //对G中指定顶点赋值
bool InsertVer(const TypeOfVer &data); //往G中添加一个顶点
int LocateVer(TypeOfVer data); //返回G中指定顶点的位置
bool ExistEdge(int u, int v);
bool PrintVer(); //输出顶点集
bool PrintAdjList(); //输出邻接矩阵
int GetVerNum(){ return Vers;} //取得当前顶点数
int GetEdgeNum(){ return Edges;} //取得当前边数
bool Insert_Edge(int u, int v); //无权图插入一条边
bool Insert_Edge(int u, int v, TypeOfEdge w); //有权图插入一条边
bool DeleteVer(const int u); //往G中删除一个顶点
bool DeleteEdge( int u, int v ); //删除边 (外壳:有向(删除1条边), 无向(删除2条边))
void DFS_Traverse(int u); //DFS遍历(外壳部分)
void BFS_Traverse(int u); //BFS遍历
~adjlist_graph(); //析构函数
};
输入说明 :
建图的输入数据格式参见建图的算法说明。
第一行:图的类型
第二行:结点数
第三行:结点集
第四行:边数
第五行:边集
第六行:待删除的顶点的位序
输出说明 :
第一行:图的类型
第三行:删除前的顶点数
第四行:删除前的边数
第五行:删除前的邻接表
空行
第六行:删除后的顶点集
第七行:删除后的顶点数
第八行:删除后的边数
第九行:删除后的邻接表
输入范例 :
UDG
6
A B C D E F
6
0 1
0 2
0 3
1 4
2 4
3 5
1
----------
UDG
A B C D E F
6
6
A->3->2->1->nullptr
B->4->0->nullptr
C->4->0->nullptr
D->5->0->nullptr
E->2->1->nullptr
F->3->nullptr
A C D E F
5
4
A->2->1->nullptr
C->3->0->nullptr
D->4->0->nullptr
E->1->nullptr
F->2->nullptr
#include<iostream>
#include<queue>
using namespace std;
int m = 0;
int temp = 0;
struct bian
{
int num;
int weight;
bian* next = NULL;
};
struct point
{
string letter;
bian* next = NULL;
};
struct graph
{
point m[100];
int number_of_point;
int number_of_bian;
};
void creat_no_direction(graph& g)
{
int e;
int f;
cin >> e;
g.number_of_point = e;
for (int i = 0; i < e; i++)
{
cin >> g.m[i].letter;
}
//上面是输入的顶点的字母
cin >> f;
g.number_of_bian = f;
for (int i = 0; i < f; i++)
{
int v1;
int v2;
cin >> v1 >> v2;
bian* p1 = new bian;
p1->num = v2;
p1->next = g.m[v1].next;
g.m[v1].next = p1;
bian* p2 = new bian;
p2->num = v1;
p2->next = g.m[v2].next;
g.m[v2].next = p2;
}
}
void creat_direction(graph& g)
{
int e;
int f;
cin >> e;
g.number_of_point = e;
for (int i = 0; i < e; i++)
{
cin >> g.m[i].letter;
}
//上面是输入的顶点的字母
cin >> f;
g.number_of_bian = f;
for (int i = 0; i < f; i++)
{
int v1;
int v2;
cin >> v1 >> v2;
bian* p1 = new bian;
p1->num = v2;
p1->next = g.m[v1].next;
g.m[v1].next = p1;
}
}
void display(graph& g)
{
for (int h = 0; h < g.number_of_point; h++)
{
if (m == 1)
{
cout << " ";
}
m = 1;
cout << g.m[h].letter;
}
m = 0;
cout << endl;
cout << g.number_of_point << endl;
cout << g.number_of_bian << endl;
for (int u = 0; u < g.number_of_point; u++)
{
cout << g.m[u].letter;
bian* p = g.m[u].next;
while (p)
{
cout << "->";
cout << p->num;
p = p->next;
}
if (p == NULL)
{
cout << "->" << "nullptr" << endl;
}
}
}
void display2(graph &g,int am)
{
for (int h = 0; h < g.number_of_point; h++)
{
if (h != am)
{
if (m == 1)
{
cout << " ";
}
m = 1;
cout << g.m[h].letter;
}
}
m = 0;
cout << endl;
cout << g.number_of_point-1 << endl;
cout << g.number_of_bian-temp << endl;
for (int u = 0; u < g.number_of_point; u++)
{
if (u == am)
{
continue;
}
cout << g.m[u].letter;
bian* p = g.m[u].next;
while (p)
{
cout << "->";
cout << p->num;
p = p->next;
}
if (p == NULL)
{
cout << "->" << "nullptr" << endl;
}
}
}
void change_bian(graph& g, int am)
{
bian* t = g.m[am].next;
while (t != NULL)
{
if (t)
{
temp++;
}
t = t->next;
}
}
void change_bian_direct(graph &g,int am)
{
for (int i = 0; i < g.number_of_point; i++)
{
bian* t = g.m[i].next;
while (t != NULL)
{
if (t->num == am)
{
temp++;
}
t = t->next;
}
}
bian* t = g.m[am].next;
while (t != NULL)
{
if (t)
{
temp++;
}
t = t->next;
}
}
void change(graph &g,int am)
{
change_bian(g, am);
for (int i = 0; i < g.number_of_point; i++)
{
bian* t = g.m[i].next;
if (t == NULL)
{
continue;
}
else if (t->num == am)
{
g.m[i].next = t->next;
}
else if (t->num < am)
{
//不做变化
}
else if (t->num > am)
{
t->num--;
}
while (t->next!=NULL)
{
if (t->next->num < am)
{
//
}
else if (t->next->num == am)
{
t->next = t->next->next;
}
else if (t->next->num > am)
{
t->next->num--;
}
if (t == NULL)
{
break;
}
else if (t->next != NULL)
{
t = t->next;
}
}
//以上操作修改权值
}
}
void change2(graph& g, int am)
{
change_bian_direct(g, am);
for (int i = 0; i < g.number_of_point; i++)
{
bian* t = g.m[i].next;
if (t == NULL)
{
continue;
}
else if (t->num == am)
{
g.m[i].next = t->next;
}
else if (t->num < am)
{
//不做变化
}
else if (t->num > am)
{
t->num--;
}
while (t->next != NULL)
{
if (t->next->num < am)
{
//
}
else if (t->next->num == am)
{
t->next = t->next->next;
}
else if (t->next->num > am)
{
t->next->num--;
}
if (t == NULL)
{
break;
}
else if (t->next != NULL)
{
t = t->next;
}
}
//以上操作修改权值
}
}
int main()
{
string kind;
cin >> kind;
if (kind == "DN")//
{
graph g;
creat_direction(g);
int am;
cin >> am;
cout << "DN" << endl;
display(g);
change2(g, am);
cout << endl;
display2(g, am);
}
else if (kind == "DG")
{
graph g;
creat_direction(g);
int am;
cin >> am;
cout << "DG" << endl;
display(g);
change2(g, am);
cout << endl;
display2(g, am);
}
else if (kind == "UDN")//
{
graph g;
creat_no_direction(g);
int am;
cin >> am;
cout << "UDN" << endl;
display(g);
change(g, am);
cout << endl;
display2(g, am);
}
else if (kind == "UDG")
{
graph g;
creat_no_direction(g);
int am;
cin >> am;
cout << "UDG" << endl;
display(g);
change(g, am);
cout << endl;
display2(g, am);
}
return 0;
}