An example of adjacency table storage representation of graphs _c language

Source: Internet
Author: User

Copy Code code as follows:

The adjacency table storage representation of---------graphs-------

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

#define MAX_VERTEXT_NUM 20

typedef int INFOTYPE;
typedef char Vertexttype;

typedef struct ARCNODE
{
int Adjvex;
struct Arcnode *nextarc;
InfoType *info;
}arcnode;

typedef struct VNODE
{
Vertexttype data;
Arcnode *firstarc;
}vnode, Adjlist[max_vertext_num];

typedef struct
{
Adjlist vertices;
int vexnum;
int arcnum;
int kind;
}algraph;

void Creategraph (Algraph *g);
void Displaygraph (Algraph *g);

int main ()
{
Algraph *graph = (algraph *) malloc (sizeof (algraph));
Creategraph (Graph);
Displaygraph (Graph);

System ("pause");
}

void Creategraph (Algraph *g)
{
int i,j,k;
Arcnode *arcnode;
printf_s ("Please enter the number of vertices and edges:");
scanf_s ("%d,%d", &g->vexnum, &g->arcnum);

Create a vertex table
printf_s ("Create vertex table \ n");
for (i = 0; i < g->vexnum; i++)
{
printf_s ("Please enter%d vertices:", i);
Fflush (stdin);//Flush Buffer
G->vertices[i].data = GetChar ();
G->vertices[i].firstarc = NULL;
}

   //Create side Table
    printf_s ("Create side table \ n");
    for (k = 0; k < g->arcnum; k++)
    {
         printf_s ("Please input (VI-VJ) vertex to serial number");
        scanf_s ("%d,%d", &i, &j);
        Arcnode = (Arcnode *) malloc (sizeof (Arcnode));
        Arcnode->adjvex = j;
        Arcnode->nextarc = g->vertices[i].firstarc;//Insert Header
         G->vertices[i].firstarc = Arcnode;

        Arcnode = (Arcnode *) malloc (sizeof (Arcnode));
        Arcnode->adjvex = i;
        Arcnode->nextarc = g->vertices[j].firstarc;//Insert Header
         G->vertices[j].firstarc = Arcnode;
   }
}

void Displaygraph (Algraph *g)
{
int i;
for (i = 0; i < g->vexnum; i++)
{
printf_s ("%d->", I);
while (G->vertices[i].firstarc!= NULL)
{
printf_s ("%d->", G->vertices[i].firstarc->adjvex);
G->vertices[i].firstarc = g->vertices[i].firstarc->nextarc;
}
printf_s ("\ n");
}
}



Copy Code code as follows:

Please enter the number of vertices and edges: 6,7
Create a vertex table
Please enter a No. 0 vertex: 0
Please enter a 1th vertex: 1
Please enter a 2nd vertex: 2
Please enter a 3rd vertex: 3
Please enter a 4th vertex: 4
Please enter a 5th vertex: 5
Create a side table
Please enter the vertex pair ordinal of (VI-VJ) 0,1
Please enter the vertex pair ordinal of (VI-VJ) 0,4
Please enter the vertex pair ordinal of (VI-VJ) 1,4
Please enter the vertex pair ordinal of (VI-VJ) 1,5
Please enter the vertex pair ordinal of (VI-VJ) 2,3
Please enter the vertex pair ordinal of (VI-VJ) 2,5
Please enter the vertex pair ordinal of (VI-VJ) 3,5
0->4->1->
1->5->4->0->
2->5->3->
3->5->2->
4->1->0->
5->3->2->1->
Please press any key to continue ...

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.