Directed graph adjacency list --p137-p138

Source:

 

#include <stdio.h>
#include <stdlib.h>

#define one 100

char VerTexType typedef;
typedef struct arcnode
{
  int adjvex; // next edge vertices numbered
  struct arcnode * nextarc; // pointer to the next one side
  int weight; weight range // weighted graph
} ArcNode;
typedef struct vexnode
{
  VerTexType Data; // vertex numbers
  arcnode * firstarc; // pointer to the first edge
} vNode, AdjList [vnum]; // all vertex in the array
typedef struct gp {

  Adjlist AdjList;
  int vexnum, arcnum; // number of vertices and edges
} Graph;

// arcnode edge structure as a vertex, storing the index of the other side of the vertex, a lower edge of the pointer, and the data associated with the edges. vexnode is a structure of each vertex,
// pointer holds data for each side and a vertex which is connected with the vertices. Graph FIG structure, including all the vertices and edges, storing the number of vertices and edges.

// determine the vertex position:

int LocateVex(Graph *G, VerTexType v)
{
  int i;
  for (i = 0; i < (G->vexnum); i++)
  {
    if (G->adjlist[i].data == v)
      return i;
  }
}

// Create the adjacent table:

int CreateAdjlist (Graph * G)
{
  int I, J, K;
  VerTexType V1, V2;
  arcnode * P1, * P2;
  the printf ( "Enter the total number of vertices and the total number of edges:");
  Scanf ( "% D% D" , & G-> vexnum, & G-> arcnum);
  the printf ( "input of each vertex values:");
  fflush (stdin);
  for (I = 0; I <G-> vexnum; I ++)
  {
    Scanf ( "% C ", & G-> adjlist [i ] .data); // input value of each vertex
    G-> adjlist [i] .firstarc = NULL; // first abutment point i is initialized to NULL
  }
  for (K = 0 ; K <G-> arcnum; K ++)
  {
    the printf ( "connected to the input sides (see a side enter one side):");
    fflush (stdin);
    Scanf ( "% C% C", & V1, & V2); / / arc and the input end of the first arc
  I = LocateVex (G, V1);
  J = LocateVex (G, V2);
  p1 = (arcnode *)malloc(sizeof(arcnode));
  p2 = (arcnode *)malloc(sizeof(arcnode));
  p1->adjvex = j;
  p1->nextarc = G->adjlist[i].firstarc;
  G->adjlist[i].firstarc = p1;
  p2->adjvex = i;
  p2->nextarc = G->adjlist[j].firstarc;
  G->adjlist[j].firstarc = p2;
  }
  return 1;
}

// first determine the number of vertices and edges (input) of the adjacent table, and then sequentially input values of each vertex. Unknown at this point because it has no connections to other vertices, so that the first side of the pointer assigned to NULL.
After //, then according to the number of vertices of the boundary value, the input to be connected together, to obtain two vertices subscripts i, j, as an edge pointer p1, p2 allocated space.
// vertex at the other side is p1 assignment subscript j, and p1 is inserted between the header and the first edge node. , Do the same for p2.

int main()
{
  int i;
  Graph G;
  arcnode *p;
  CreateAdjlist(&G);
  for (i = 0; i < G.vexnum; i++)
  {
    p = G.adjlist[i].firstarc;
    printf("%c相连的顶点有:", G.adjlist[i].data);
    while (p != NULL)
    {
      printf("%c ", G.adjlist[p->adjvex].data);
      p = p->nextarc;
    }
    printf("\n");
  }
  return 0;
} 

 operation result:

Guess you like

Origin www.cnblogs.com/duanqibo/p/11983466.html