修改下列代码要求如下:每组数据输出n-1行。为删除顶点后的邻接表。每两个数字之间用空格隔开,不增加main函数。代码如下:#include<iostream> #include<string> #include<algorithm> #include<vector> #include<set> #include<map> using namespace std;typedef struct LNode {int data;struct LNode* next; }*linklist, LNode; typedef struct {int vexnum;int arcnum;linklist VList; }ALGragh; void CreateUDG(ALGragh& G, int n, int m) {G.arcnum = m;G.vexnum = n;G.VList = new LNode[n + 1];for (int i = 1; i <= n; i++){G.VList[i].data = i;G.VList[i].next = NULL;}int h, k;for (int i = 0; i < m; i++){cin >> h >> k;linklist p = new LNode, q = new LNode;p->data = h;p->next = G.VList[k].next;G.VList[k].next = p;q->data = k;q->next = G.VList[h].next;G.VList[h].next = q;} } void PrintGraph(ALGragh G) {for (int i = 1; i <= G.vexnum; i++){linklist p = &G.VList[i];while (p->next){cout << p->data << ' ';p = p->next;}cout << p->data << endl;} } void DeleteVex(ALGragh& G) {int h, k;cin >> h >> k;for (int i = 1; i <= G.vexnum ; i++){linklist p = &G.VList[i];if (i == h){if (p->next){if (p->next->data == k)p->next = p->next->next;}}if (i == k){if (p->next){if (p->next->data == h)p->next = p->next->next;}}} }
时间: 2024-03-09 19:47:37 浏览: 63
修改后的代码如下:
```cpp
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef struct LNode {
int data;
struct LNode* next;
}*linklist, LNode;
typedef struct {
int vexnum;
int arcnum;
linklist VList;
}ALGragh;
void CreateUDG(ALGragh& G, int n, int m) {
G.arcnum = m;
G.vexnum = n;
G.VList = new LNode[n + 1];
for (int i = 1; i <= n; i++){
G.VList[i].data = i;
G.VList[i].next = NULL;
}
int h, k;
for (int i = 0; i < m; i++){
cin >> h >> k;
linklist p = new LNode, q = new LNode;
p->data = h;
p->next = G.VList[k].next;
G.VList[k].next = p;
q->data = k;
q->next = G.VList[h].next;
G.VList[h].next = q;
}
}
void PrintGraph(ALGragh G) {
for (int i = 1; i <= G.vexnum; i++){
linklist p = &G.VList[i];
while (p->next){
cout << p->data << ' ';
p = p->next;
}
cout << p->data << endl;
}
}
void DeleteVex(ALGragh& G) {
int h, k;
cin >> h >> k;
// 删除顶点h到k的边
linklist p = &G.VList[h];
while (p->next){
if (p->next->data == k){
p->next = p->next->next;
break;
}
p = p->next;
}
// 删除顶点k到h的边
p = &G.VList[k];
while (p->next){
if (p->next->data == h){
p->next = p->next->next;
break;
}
p = p->next;
}
// 输出删除后的邻接表
for (int i = 1; i <= G.vexnum; i++){
if (i == h || i == k) continue; // 已经删除了顶点h和k
linklist p = &G.VList[i];
while (p->next){
if (p->next->data == h || p->next->data == k){
p->next = p->next->next;
}else{
cout << p->data << ' ' << p->next->data << endl;
p = p->next;
}
}
}
}
```
阅读全文