Establishment of graphs--adjacency table representation (C language +vc6.0 platform)

Source: Internet
Author: User

Graph is an important and relatively complex data structure, which is very useful in practical programming. Adjacency table is one of the main representations of graphs, and is a kind of link table representation method.

#include <stdio.h>

#include <stdlib.h>

The maximum number of vertices for the #define Max 10//graph is 10

typedef struct NODE//Edge table junction (ARC)

{

int adjvex;//number of connected vertices

int weight;//Benquan

struct node *pnext;//points to the next edge table node

}edgenode;

typedef struct VERTEXNODE//Vertex table node

{

int adjvex;//vertex number

Edgenode *pfirst;//side Table head pointer

}vertexnode;

typedef vertexnode ADJ_LIST[MAX]; Table Header Array

adjacency table Structure of typedef struct ADJLIST_GRAPH//graphs

{

int cnt_edges; Number of sides

int cnt_nodes; Top points

Adjacency table Header of Adj_list adjlist;//graph

}adjlist_graph;

void Create_graph (adjlist_graph* graph)//create diagram in adjacency table mode

{

edgenode* pnewnode;//New Benzi node

int i,j,k;

printf ("Number of vertices and sides of the input graph: \ n");

scanf ("%d%d", &graph->cnt_nodes,&graph->cnt_edges);

printf ("The vertex of the graph has%d, the number of sides is%d \ n",

Graph->cnt_nodes,graph->cnt_edges);

for (i=0;i<graph->cnt_nodes;i++)//initialization graph adjacency table vertex

{

graph->adjlist[i].adjvex=i;

graph->adjlist[i].pfirst=null;

}

for (k=0;k<graph->cnt_edges;k++)//Loop input graph each edge

{//no-map scenario

printf ("Enter a new edge: \ n");

scanf ("%d%d", &i,&j);

Pnewnode= (edgenode*) malloc (sizeof (Edgenode));

pnewnode->adjvex=j;

pnewnode->weight=0;

pnewnode->pnext=graph->adjlist[i].pfirst;

graph->adjlist[i].pfirst=pnewnode;

Diagram-free case with symmetry

Pnewnode= (edgenode*) malloc (sizeof (Edgenode));

pnewnode->adjvex=i;

pnewnode->weight=0;

pnewnode->pnext=graph->adjlist[j].pfirst;

graph->adjlist[j].pfirst=pnewnode;

}

}

void Show_adjlist_graph (adjlist_graph* graph)//show vertices in the graph and all sides associated with them

{

int i;

Edgenode* Pnode;

for (i=0;i<graph->cnt_nodes;i++)

{

pnode=graph->adjlist[i].pfirst;

printf ("\ n vertex number%d:", Graph->adjlist[i].adjvex);

while (Pnode!=null)

{

printf ("side:%d<->%d", Graph->adjlist[i].adjvex,pnode->adjvex);

pnode=pnode->pnext;

}

printf ("\ n");

}

}

int main (void)

{

Adjlist_graph *pgraph;

Pgraph= (adjlist_graph*) malloc (sizeof (adjlist_graph));

Create_graph (pgraph);

Show_adjlist_graph (pgraph);

}

Establishment of graphs--adjacency table representation (C language +vc6.0 platform)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.