#include <iostream>
using namespace std;
#define maxSize 100
#define Elemtype int
typedef struct {
Elemtype vexs[maxSize];
int arcs[maxSize][maxSize];
int numVexs;
int numArcs;
}adjacenryMatrixGraph;
void initAdjacenryMatrixGraph(adjacenryMatrixGraph &G) {
G.numArcs = 0;
G.numVexs = 0;
}
void outputAdjacenryMatrixGraph(adjacenryMatrixGraph G) {
for(int i = 0; i < G.numVexs; i ++) {
for(int j = 0; j < G.numVexs; j ++) {
if(j != G.numVexs - 1) cout << G.arcs[i][j] << " ";
else cout << G.arcs[i][j] << endl;
}
}
cout << endl;
}
int getVexIndexAdjacenryMatrixGraph(adjacenryMatrixGraph G, Elemtype e) {
for(int i = 0; i < G.numVexs; i ++) {
if(e == G.vexs[i]) return i;
}
return -1;
}
Elemtype getIndexVexAdjacenryMatrixGraph(adjacenryMatrixGraph G, int index) {
if(index < G.numVexs) {
return G.vexs[index];
}
else return -1;
}
void createAdjacenryMatrixGraph(adjacenryMatrixGraph &G) {
cout << "please cin number of vexs: ";
int numberVexs;
cin >> numberVexs;
cout << "please cin every vexs: ";
G.numVexs = numberVexs;
for(int i = 0; i < numberVexs; i ++) {
cin >> G.vexs[i];
}
Elemtype firstVex;
Elemtype lastVex;
cout << "please cin two vexs where the edge is connected: ";
cin >> firstVex >> lastVex;
int firstIndex;
int lastIndex;
while(firstVex != -1 && lastVex != -1) {
firstIndex = getVexIndexAdjacenryMatrixGraph(G, firstVex);
lastIndex = getVexIndexAdjacenryMatrixGraph(G, lastVex);
if(firstIndex == -1 || lastVex == -1) {
cout << "cin ERROR, please cin again!!!" << endl;
continue;
}
else {
G.arcs[firstIndex][lastIndex] = 1;
G.arcs[lastIndex][firstIndex] = 1;
G.numArcs ++;
cout << "please cin two vexs where the edge is connected: ";
cin >> firstVex >> lastVex;
}
}
}
bool adjacentAdjacenryMatrixGraph(adjacenryMatrixGraph G, Elemtype a, Elemtype b) {
int firstIndex = getVexIndexAdjacenryMatrixGraph(G, a);
int lastIndex = getVexIndexAdjacenryMatrixGraph(G, b);
if(firstIndex == -1 || lastIndex == -1) {
cout << "not exits" << endl;
return false;
}
else if(G.arcs[firstIndex][lastIndex] == 1) {
cout << "exits" << endl;
return true;
}
else {
cout << "not exits" << endl;
return false;
}
}
void listNeighborsAdjacenryMatrixGraph(adjacenryMatrixGraph G, Elemtype a) {
int index = getVexIndexAdjacenryMatrixGraph(G, a);
if(index == -1) {
cout << "cin vex ERROR";
return ;
}
else {
cout << "The point adjacent to the point: ";
for(int i = 0; i < G.numVexs; i ++) {
if(G.arcs[index][i] == 1) {
Elemtype res = getIndexVexAdjacenryMatrixGraph(G, i);
if(res != -1) {
cout << res << " ";
}
}
}
cout << endl;
return ;
}
}
bool insertVexAdjacenryMatrixGraph(adjacenryMatrixGraph &G, Elemtype e) {
if(G.numVexs == maxSize - 1) {
cout << "this adj filled" << endl;
return false;
}
else {
G.vexs[G.numVexs - 1] = e;
G.numVexs ++;
return true;
}
}
bool deleteVexAdjacenryMatrixGraph(adjacenryMatrixGraph &G, Elemtype e) {
int index = getVexIndexAdjacenryMatrixGraph(G, e);
if(index == -1) {
cout << "vex not exits" << endl;
return false;
}
else {
for(int i = 0; i < G.numVexs; i ++) {
if(G.arcs[index][i] == 1) G.numArcs --;
}
for(int i = 0; i < G.numVexs; i ++) {
for(int j = 0; j < G.numVexs; j ++) {
if(i > index && j > index)
G.arcs[i - 1][j - 1] = G.arcs[i][j];
else if(i > index)
G.arcs[i - 1][j] = G.arcs[i][j];
else if(j > index)
G.arcs[i][j - 1] = G.arcs[i][j];
}
}
for(int i = index; i < G.numVexs; i++) {
G.vexs[i]=G.vexs[i+1];
}
G.numVexs--;
}
}
bool insertArcAdjacenryMatrixGraph(adjacenryMatrixGraph &G, Elemtype a, Elemtype b) {
int firstIndex = getVexIndexAdjacenryMatrixGraph(G, a);
int lastIndex = getVexIndexAdjacenryMatrixGraph(G, b);
if(firstIndex == -1 || lastIndex == -1) {
cout << "vexs has not exits" << endl;
return false;
}
else {
if(G.arcs[firstIndex][lastIndex] == 1) {
cout << "this edge has exits" << endl;
return false;
}
else {
G.arcs[firstIndex][lastIndex] = 1;
G.arcs[lastIndex][firstIndex] = 1;
G.numArcs ++;
return true;
}
}
}
bool deleteArcAdjacenryMatrixGraph(adjacenryMatrixGraph &G, Elemtype a, Elemtype b) {
int firstIndex = getVexIndexAdjacenryMatrixGraph(G, a);
int lastIndex = getVexIndexAdjacenryMatrixGraph(G, b);
if(firstIndex == -1 || lastIndex == -1) {
cout << "vexs has not exits" << endl;
return false;
}
else {
if(G.arcs[firstIndex][lastIndex] == 0) {
cout << "this edge not exits" << endl;
return false;
}
else {
G.arcs[firstIndex][lastIndex] = 0;
G.arcs[lastIndex][firstIndex] = 0;
G.numArcs --;
return true;
}
}
}
int main() {
adjacenryMatrixGraph G;
initAdjacenryMatrixGraph(G);
createAdjacenryMatrixGraph(G);
outputAdjacenryMatrixGraph(G);
deleteVexAdjacenryMatrixGraph(G, 20);
outputAdjacenryMatrixGraph(G);
return 0;
}
数据结构邻接矩阵图C语言
最新推荐文章于 2023-11-08 20:36:31 发布