C language implementation of the adjacency list

1. In FIG storage study, the adjacency matrix and adjacency list are two commonly used storage FIG manner using adjacency table below is implemented in C

2. The specific implementation process is as follows:

① First, the structure using the structure of FIG statement, the structure of the vertices in the graph, and directed at one side of the structure, which can be referred to Yan Wei Min version data structure to write specific data structure:

typedef struct ArcNode{
	int adjvex;//该边所指向的节点的位置 
	struct ArcNode *nextarc;
//	边的信息, 例如边的权重 
	int info;
}ArcNode; 

typedef struct{
    //顶点表存储当前顶点的信息
	int data;
    //指向边的指针
	ArcNode *firstarc;
}Vnode; 

typedef struct{
    //顶点数组
	Vnode adjlist[maxSize];
    //图中的顶点的数目和边的数目
	int n, e;
}AGraph

After ② With these structures so we need to declare a pointer pointing to a map, and use malloc function allocates memory space for the map, if it is not allocated, then an error will occur when the first side is initialized vertex, this is different points of the Java language

③ In the beginning when the need for a pointer to the first edge of the first vertex point of the assignment list, the assignment of NULL, this step is essential, if no error occurs the program

④ Since stored here is entitled to view it in the console to enter the number of vertices, the number of edges, and edge weights ArcNode create a new node,

The node is assigned adjvex v, to null if it is empty firstarc current pointer points to the vertex node pointer is NULL is determined after the assignment at the input side of a first edge of the start vertex pointer pointing to FIG whether the Add call is not empty then inserted into the list of insertNode method, traversing the list of the node is inserted into the list of the final surface

⑤ created after the adjacent table so since we can traverse the map, and loop through the array of vertices to determine the current index corresponding firstarc vertices to be traversed is empty, empty explain the current vertex is not a degree, there are side then we need to use a while loop to traverse the entire vertex list

I feel the most important thing is to understand the meaning of each element of the expression of which is to look at the data structure as well as the C language pointer during initialization error appears to be otherwise is NULL

Test data are as follows:

3. The following is a specific code:

#include<stdio.h>
#include<iostream>
#include<malloc.h>
#define maxSize 1000
using namespace std;
typedef struct ArcNode{
	int adjvex;
	struct ArcNode *nextarc;
	int info;
}ArcNode; 

typedef struct{
	int data;
	ArcNode *firstarc;
}Vnode; 

typedef struct{
	Vnode adjlist[maxSize];
	int n, e;
}AGraph;

AGraph *graph;
void  insertNode(ArcNode *node, ArcNode *newNode){
	ArcNode *p = node;
	while(p->nextarc != NULL){
		p = p->nextarc;
	}
	p->nextarc = newNode;	
}

void create(){
	graph = (AGraph*)malloc(sizeof(AGraph));
	cout << "输入顶点的数目: " << endl;
	cin >> graph->n;
	cout << "输入图中边的数目: " << endl;
	cin >> graph->e;
	cout << graph->n << " ";

	int u = -1, v = -1, weight = -1;
	for(int i = 0; i < graph->n; i++){
		graph->adjlist[i].firstarc = NULL;
	}
	
	ArcNode *node; 
	cout << graph->e << endl;
	for(int i = 0; i < graph->e; i++){
		cin >> u >> v >> weight;
		node = (ArcNode *)malloc(sizeof(ArcNode));
		node->adjvex = v;
		node->info = weight;
		node->nextarc = NULL;
		graph->adjlist[u].data = u;
		if(graph->adjlist[u].firstarc == NULL){
			//边 
			graph->adjlist[u].firstarc = node; 
		}else{
			//插入边
			insertNode(graph->adjlist[u].firstarc, node); 
		}	
	} 
}

void  travseTree(){
	for(int i = 0; i < graph->n; i++){
		if(graph->adjlist[i].firstarc != NULL){
			cout << i << " ";
			ArcNode *p = graph->adjlist[i].firstarc;
			while(p != NULL){
				cout << p->adjvex <<  " ";
				p = p->nextarc;
			}
			cout << endl;
		} 
	} 
}

int main(void){
    create();
	travseTree();
	return 0;
} 

 

Guess you like

Origin blog.csdn.net/qq_39445165/article/details/92692027