First, the purpose and requirements (needs analysis):
1, master adjacency list storage structure and the adjacent table build and operate.
2, the configuration of an undirected graph adjacency list, the number of edges required to enter from the keyboard and the number of vertices, and shows the configuration of the adjacent table)
Experimental Development: 1. built with the adjacent table to map
2. determines whether the edge exists
3. The degree of vertex seek
Here is the code:
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#define one 20
using namespace std;
typedef struct arcnode
{
int adjvex; // vertex number corresponding to a side
struct arcnode * next; // pointer to the next one side
} ArcNode;
typedef struct vexnode
{
int vertex; // vertex numbers
ArcNode * first; // pointer to point to the edge of attachment of the first vertex
AdjList} [one];
typedef struct
{
AdjList adjlist;
int vexnum, arcnum; // number of vertices and edges
}Graph;
void Init (Graph * GA, int a, int b) // initialize
{
int i;
GA->vexnum=a;
GA->arcnum=b;
for(i=0;i<GA->vexnum;i++)
{
GA-> adjlist [i] .vertex = i; // initialize vertex information
GA-> adjlist [i] .first = NULL; // first abutment point i is initialized to NULL
}
}
Construction void InsertArcnode (Graph * GA) // undirected graph
{
ArcNode *p;
int i,j,k;
for(k=0;k<GA->arcnum;k++)
{
cout << "Please enter" "[separated by a space between two vertices (starting from 0)] sides:" << k + 1 <<;
scanf("%d %d",&i,&j);
p = (ArcNode *) malloc (sizeof (ArcNode)); // table generated node j
p->adjvex=j;
p-> next = GA-> adjlist [i] .first; // j to node i is linked to a single list in
GA->adjlist[i].first=p;
p = (ArcNode *) malloc (sizeof (ArcNode)); // generate the table of node i
p->adjvex=i;
p-> next = GA-> adjlist [j] .first; // linked list node i to j in a single
GA->adjlist[j].first=p;
}
}
void PrintGraph (Graph * GA) // Print map
{
cout<<endl;
cout << "adjacent table generated as follows:" << endl;
for(int i=0;i<GA->vexnum;i++)
{
printf("v%d:",i);
ArcNode *p=GA->adjlist[i].first;
while(p!=NULL)
{
printf("-->%d",p->adjvex);
p=p->next;
}
printf("\n");
}
printf("-----------------------------------\n");
}
void CreateLink (Graph * GA) {// has established a directed graph
ArcNode *p;
int i,j,k;
for(k=0;k<GA->arcnum;k++){
cout << "Please enter" "[separated by a space between two vertices (starting from 0)] sides:" << k + 1 <<;
scanf("%d %d",&i,&j);
p=(ArcNode *)malloc(sizeof(ArcNode));
p->adjvex=j;
p->next=GA->adjlist[i].first;
GA->adjlist[i].first=p;
}
}
void Judge (Graph * GA) {// determines whether or not there has edges to the graph
int i,j;
cout << "Please enter the edge to be determined:";
scanf("%d %d",&i,&j);
ArcNode *p=GA->adjlist[i].first;
while(p!=NULL){
if(p->adjvex==j){cout<<"该边存在"<<endl;break;}
else p=p->next;
}
if (p == NULL) {cout << "that there is no side" << endl;}
}
void Count_degree (Graph * GA) {// calculated to have the degree of the vertices of the figure
int i;
int out = 0, in = 0; // out of the store, in the storage of
cout << "Please input vertex number (starting with 0):";
scanf("%d",&i);
if(GA->adjlist[i].first==NULL){out=0;}
else {
ArcNode *p=GA->adjlist[i].first;
while(p!=NULL){
out++;
p=p->next;
}
}
for(int k=0;k<GA->vexnum;k++)
{
ArcNode *p=GA->adjlist[k].first;
while(p!=NULL)
{
if(p->adjvex==i){in++;break;}
p=p->next;
}
}
cout << "the degree of the vertex is:" << out << endl ;;
cout << "the degree of the vertex is:" << in << endl ;;
}
void choice () {// selection screen
int choice1;
cout << "Please select the function:" << "undirected graphs 1 to 2. Figure 3. Quit." << endl;
cin >> choice1;
if(choice1==1){
int a,b;
Graph GA;
printf("-----------------------------------\n");
printf ( "Please enter the number of vertices and edges to the graph no:");
scanf("%d %d",&a,&b);
Init(&GA,a,b);
InsertArcnode(&GA);
PrintGraph(&GA);
choice();
}
if(choice1==2){
int a,b;
Graph GA;
printf("-----------------------------------\n");
printf ( "Please enter the number of vertices and edges to the graph:");
scanf("%d %d",&a,&b);
Init(&GA,a,b);
CreateLink(&GA);
PrintGraph(&GA);
cout << ". Function: Query 1 Query 2 side whether there is a vertex of a penetration 3. Return" << endl;
int choice2;
cin >> choice2;
while(choice2!=3){
switch(choice2){
case 1:Judge(&GA);break;
case 2:Count_degree(&GA);break;
}
cout << ". Function: Query 1 Query 2 side whether there is a vertex of a penetration 3. Return" << endl;
cin >> choice2;
}
choice();
}
if(choice1==3){return;}
}
int main ()
{
choice (); // function interface
return 0;
}