p=G.vertices[i].firstarc;p;p nextarc什么意思
时间: 2024-03-21 15:44:08 浏览: 27
`p=G.vertices[i].firstarc` 是指获取图 `G` 中第 `i` 个顶点的第一条邻接边,将其赋值给指针 `p`。其中,`G.vertices[i]` 表示获取图 `G` 中的第 `i` 个顶点,`.firstarc` 表示获取该顶点的第一条邻接边。邻接边是指从该顶点出发的一条边。
`p` 是指向邻接边的指针,表示当前正在遍历的邻接边。
`p->nextarc` 表示获取指针 `p` 指向的邻接边的下一条邻接边。其中,`->` 是指向结构体或类的指针所使用的符号,表示获取指针指向的结构体或类的成员变量或成员函数。
在遍历图时,可以通过 `p` 和 `p->nextarc` 来遍历当前顶点的所有邻接边,例如可以使用 while 循环遍历当前顶点的所有邻接边,具体实现方式如下:
```
p = G.vertices[i].firstarc;
while (p != NULL) {
// 对当前邻接边进行操作,例如输出该边的信息
printf("(%d, %d)\n", i, p->adjvex);
// 将指针 p 指向下一条邻接边
p = p->nextarc;
}
```
这段代码将输出从顶点 `i` 出发的所有邻接边的信息,包括该边的起点和终点。
相关问题
优化这段代码,并写出来typedef struct ArcNode { int adjvex; struct ArcNode *nextarc; }ArcNode; typedef struct VNode { VertexType data; ArcNode *firstarc; }VNode, AdjList; typedef struct { AdjList vertices[MVNum]; int vexnum, arcnum; }ALGraph; void CreateUDG(ALGraph &G) { cout << "请输入顶点数和边数:\n"; cin>>G.vexnum>>G.arcnum; for(int i=0; i<G.vexnum; ++i) { cout << "请输入顶点信息:\n"; cin>> G.vertices[i].data; G.vertices[i].firstarc=NULL; } for(int k = 0; k<G.arcnum;++k) { int j; ArcNode *p1,*p2; cout<<"请输入每条边对应的两个顶点的序号:\n"; cin>>i>>j; p1=new ArcNode; p1->adjvex=j; p1->nextarc= G.vertices[i].firstarc; G.vertices[i].firstarc=p1; p2=new ArcNode; p2->adjvex=i; p2->nextarc= G.vertices[j].firstarc; G.vertices[j].firstarc=p2; } } void DFS(ALGraph G, int v) { //图G为邻接表类型 cout<<v; visited[v] = true; //访问第v个顶点 p= G.vertices[v].firstarc; //p指向v的边链表的第一个边结点 while(p!=NULL) //边结点非空 { w=p->adjvex; //表示w是v的邻接点 if(!visited[w]) DFS(G, w); //如果w未访问,则递归调用DFS p=p->nextarc; //p指向下一个边结点 } }
优化后的代码如下:
```c++
#include <iostream>
#include <cstring>
using namespace std;
const int MVNum = 100;
typedef int VertexType;
struct ArcNode {
int adjvex;
struct ArcNode* nextarc;
};
struct VNode {
VertexType data;
ArcNode* firstarc;
};
typedef VNode AdjList[MVNum];
struct ALGraph {
AdjList vertices;
int vexnum, arcnum;
};
void InputVertex(ALGraph& G) {
for (int i = 0; i < G.vexnum; ++i) {
cout << "请输入顶点信息:\n";
cin >> G.vertices[i].data;
G.vertices[i].firstarc = nullptr;
}
}
void InputEdge(ALGraph& G) {
for (int k = 0; k < G.arcnum; ++k) {
int i, j;
ArcNode* p1, * p2;
cout << "请输入每条边对应的两个顶点的序号:\n";
cin >> i >> j;
p1 = new ArcNode;
p1->adjvex = j;
p1->nextarc = G.vertices[i].firstarc;
G.vertices[i].firstarc = p1;
p2 = new ArcNode;
p2->adjvex = i;
p2->nextarc = G.vertices[j].firstarc;
G.vertices[j].firstarc = p2;
}
}
void CreateUDG(ALGraph& G) {
cout << "请输入顶点数和边数:\n";
cin >> G.vexnum >> G.arcnum;
InputVertex(G);
InputEdge(G);
}
bool visited[MVNum];
void DFS(ALGraph G, int v) {
cout << v;
visited[v] = true;
ArcNode* p = G.vertices[v].firstarc;
while (p != nullptr) {
int w = p->adjvex;
if (!visited[w]) {
DFS(G, w);
}
p = p->nextarc;
}
}
int main() {
memset(visited, false, sizeof(visited));
ALGraph G;
CreateUDG(G);
DFS(G, 0);
return 0;
}
```
修改下列代码,使每行输出后没有空格#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; }
下面是修改后的代码,主要是去掉了输出中的空格:
#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;
}