日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

c语言邻接表的构建_C++实现有向图邻接表的构建

發(fā)布時間:2025/3/12 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c语言邻接表的构建_C++实现有向图邻接表的构建 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本文實例為大家分享了C++實現(xiàn)有向圖鄰接表的構(gòu)建代碼,供大家參考,具體內(nèi)容如下

數(shù)據(jù)結(jié)構(gòu)里面的一道基礎(chǔ)題,分享下自己的寫法,驗證可跑。

#include

#include

const int MAX = 20;

using namespace std;

struct ArcNode { //弧結(jié)點

int adjvex = -1; //所指頂點位置

ArcNode *nextarc = nullptr; //下一條狐指針

size_t info = 0; //弧信息

};

struct VNode { //頂點

string data = "0";

ArcNode *firstarc = nullptr; //第一條依附該頂點的弧的指針

};

struct Graph { //圖結(jié)構(gòu)

VNode vertices[MAX]; //全部頂點

int vexnum, arcnum; //頂點數(shù)和弧數(shù)

Graph(int m, int n) :vexnum(m), arcnum(n) {};

Graph() :vexnum(0), arcnum(0) {};

};

int main()

{

int vnum, anum, tempanum = 0;

cout << "輸入頂點數(shù):";

cin >> vnum;

cout << "輸入弧數(shù):";

cin >> anum;

cout << "\n\n";

Graph G(vnum, anum);

for (int i = 0; i != vnum; ++i) {

cout << "輸入結(jié)點" << i << "的信息:";

cin >> G.vertices[i].data;

if (tempanum != anum)

cout << "輸入依靠此結(jié)點的弧的信息(輸入-1以停止):\n";

else

cout << "已輸入所有弧的信息!\n";

bool first = true;

ArcNode *p, *temp;

for (int j = 0; (j != anum) && (tempanum != vnum); ++j) {

int pointto;

cout << "輸入弧" << tempanum << "所指向的頂點位置:";

cin >> pointto;

if (pointto == -1) break;

else {

++tempanum;

if (first == true) {

first = false;

G.vertices[i].firstarc = new ArcNode;

G.vertices[i].firstarc->adjvex = pointto;

p = G.vertices[i].firstarc;

}

else {

temp = new ArcNode;

temp->adjvex = pointto;

p->nextarc = temp;

p = temp;

}

}

}

cout << endl;

}

for (int i = 0; i != anum; ++i) {

cout << "頂點" << i << ": |" << G.vertices[i].data << "|";

if (G.vertices[i].firstarc) {

cout << " -> " << G.vertices[i].firstarc->adjvex;

auto pt = G.vertices[i].firstarc->nextarc;

while (pt) {

cout << " -> " << pt->adjvex;

pt = pt->nextarc;

}

cout << "-> ^";

}

else

cout << " -> ^";

cout << endl;

}

return 0;

}

由于只是單純構(gòu)建基本的無權(quán)值有向圖鄰接表,里面的弧結(jié)構(gòu)中弧信息未利用到。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

本文標(biāo)題: C++實現(xiàn)有向圖鄰接表的構(gòu)建

本文地址: http://www.cppcns.com/ruanjian/c/310269.html

總結(jié)

以上是生活随笔為你收集整理的c语言邻接表的构建_C++实现有向图邻接表的构建的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。