修改下列代码,使每行输出后没有空格#include<iostream> #define OK 1 #define ERROR 0 #define OVERFLOW -2 #define MVNum 100 //最大顶点数 using namespace std; typedef struct ArcNode{ int adjvex; //邻接点域:该边所指向的顶点的位置 int data; //数据域:存储和边相关的信息 struct ArcNode* nextarc; //链域:指向下一条边的指针 }ArcNode; typedef struct VNode{ int data; //顶点结点的数据域 ArcNode *firstarc; //链域:指向第一条依附该顶点的边的指针 }VNode,AdjList[MVNum]; //AdjList表示邻接表类型 typedef struct{ AdjList vertices; int vexnum,arcnum; //图的当前顶点数和边数 }ALGragh; int CreateUDG(ALGragh &G,int vexnum,int arcnum){ //采用邻接表表示法,创建无向图G G.vexnum = vexnum; G.arcnum = arcnum; for(int i=1;i<=vexnum;i++){ G.vertices[i].data = i; G.vertices[i].firstarc = NULL; } for(int k=1;k<=arcnum;k++){ int i,j; cin>>i>>j; ArcNode *p = new ArcNode; p->adjvex = j; p->nextarc = G.vertices[i].firstarc; G.vertices[i].firstarc = p; ArcNode *q = new ArcNode; q->adjvex = i; q->nextarc = G.vertices[j].firstarc; G.vertices[j].firstarc = q; } return OK; } int InsertVex(ALGragh &G){ //在以邻接表形式存储的无向图G上插入顶点v int v; cin>>v; G.vexnum++; G.vertices[G.vexnum].data = v; G.vertices[G.vexnum].firstarc = NULL; return OK; } int PrintGraph(ALGragh G){ //输出图G for(int i=1;i<=G.vexnum;i++){ cout<<G.vertices[i].data<<" "; ArcNode *p = G.vertices[i].firstarc; while(p){ cout<<p->adjvex<<" "; p = p->nextarc; } cout<<endl; } return OK; } int main(){ int n,m; while(cin>>n>>m){ if(n==0 && m==0) break; ALGragh G; CreateUDG(G,n,m); InsertVex(G); PrintGraph(G); } return 0; }
时间: 2024-01-06 11:05:30 浏览: 73
下面是修改后的代码,主要是去掉了输出中的空格:
#include<iostream>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MVNum 100 //最大顶点数
using namespace std;
typedef struct ArcNode{
int adjvex; //邻接点域:该边所指向的顶点的位置
int data; //数据域:存储和边相关的信息
struct ArcNode* nextarc; //链域:指向下一条边的指针
}ArcNode;
typedef struct VNode{
int data; //顶点结点的数据域
ArcNode *firstarc; //链域:指向第一条依附该顶点的边的指针
}VNode,AdjList[MVNum]; //AdjList表示邻接表类型
typedef struct{
AdjList vertices;
int vexnum,arcnum; //图的当前顶点数和边数
}ALGragh;
int CreateUDG(ALGragh &G,int vexnum,int arcnum){ //采用邻接表表示法,创建无向图G
G.vexnum = vexnum;
G.arcnum = arcnum;
for(int i=1;i<=vexnum;i++){
G.vertices[i].data = i;
G.vertices[i].firstarc = NULL;
}
for(int k=1;k<=arcnum;k++){
int i,j;
cin>>i>>j;
ArcNode *p = new ArcNode;
p->adjvex = j;
p->nextarc = G.vertices[i].firstarc;
G.vertices[i].firstarc = p;
ArcNode *q = new ArcNode;
q->adjvex = i;
q->nextarc = G.vertices[j].firstarc;
G.vertices[j].firstarc = q;
}
return OK;
}
int InsertVex(ALGragh &G){ //在以邻接表形式存储的无向图G上插入顶点v
int v;
cin>>v;
G.vexnum++;
G.vertices[G.vexnum].data = v;
G.vertices[G.vexnum].firstarc = NULL;
return OK;
}
int PrintGraph(ALGragh G){ //输出图G
for(int i=1;i<=G.vexnum;i++){
cout<<G.vertices[i].data;
ArcNode *p = G.vertices[i].firstarc;
while(p){
cout<<" "<<p->adjvex;
p = p->nextarc;
}
cout<<endl;
}
return OK;
}
int main(){
int n,m;
while(cin>>n>>m){
if(n==0 && m==0) break;
ALGragh G;
CreateUDG(G,n,m);
InsertVex(G);
PrintGraph(G);
}
return 0;
}
阅读全文