Implementing DFS and BFS with adjacency tables

Source: Internet
Author: User

#include <stdio.h> #include <stdlib.h> #define Maxvertex 10typedef Char vertextype;                   Vertex type typedef int EDGETYPE;                   The type of the edge typedef int ELEMTYPE;    The element type in the queue typedef struct edgenode{int Adjvex;    Edgetype weight; struct Edgenode *next;}                              Edgenode;    A node that holds the subscript value of the adjacency vertex in the adjacency table a typedef struct vertexnode{int Flag;    int vertex;    Vertextype data; Edgenode *firstedge;}         Vertexnode,adjlist[maxvertex];    Defines an adjacency table for a maxvertex vertex typedef struct graphadjlist{adjlist adjlist;    int Numvertex; int Numedge;}                          Graphadjlist;    Define a graph typedef struct qnode{elemtype qdata; struct Qnode *nextnode;}                                 Qnode;    Define the node of the queue typedef struct queuelist{Qnode *front; Qnode *rear;}                      Queuelist,*queue; Define a queue//initialize queue void Initqueue (Queuelist *q) {Q->front = (qnode*) malloc (Sizeo    F (Qnode)); if (q-&Gt;front = = NULL) {printf ("error!");    Exit (0);    } q->rear = q->front;//q->rear = NULL;                                        Q->front->nextnode = NULL;}    Insert a node to the queue void Insertqueue (Queuelist *q,elemtype *e) {Qnode *p;    p = (Qnode *) malloc (sizeof (Qnode));    P->qdata = *e;    P->nextnode = NULL;    Q->rear->nextnode = p;                                        Q->rear = P;}    Delete nodes in the queue (out of queue) void Deletequeue (Queuelist *q,elemtype *e) {Qnode *s;    if (Q->front = = q->rear) {return;    } s = q->front->nextnode;    *e = s->qdata;    Q->front->nextnode = s->nextnode;    if (Q->front->nextnode = = NULL) {q->rear = q->front;    } free (s); return;}    void Creategraph (Graphadjlist *g)//Build a diagram we want to traverse {int i = 0,j = 0,k = 0;    Edgenode *s;    Vertextype C;    printf ("Please enter the number of vertices and sides of the graph, separated by commas in the middle: \ n");    scanf ("%d,%d", &g->numvertex,&g->numedge);printf ("Please enter data stored in each vertex: \ n");    Fflush (stdin);    scanf ("%c", &c);        while (I < G->numvertex) {if (c = = ' \ n ') {break;        } g->adjlist[i].data = C; G->adjlist[i].        Flag = 0;        G->adjlist[i].vertex = i;        G->adjlist[i].firstedge = NULL;        i++;    scanf ("%c", &c);        } fflush (stdin);            for (k = 0;k < g->numedge;k++) {printf ("Please input edge vi~vj attached vertex subscript i and J: \ n");            scanf ("%d,%d", &i,&j);            s = (edgenode*) malloc (sizeof (Edgenode));            S->adjvex = j;            S->next = g->adjlist[i].firstedge;            G->adjlist[i].firstedge = s;            s = (edgenode*) malloc (sizeof (Edgenode));            S->adjvex = i;            S->next = g->adjlist[j].firstedge;        G->adjlist[j].firstedge = s;    }}//To see if the adjacency table is built correctly, this function is just to verify void print (Graphadjlist *g) {int i = 0; EdgenoDe *p;        for (i = 0;i < g->numvertex;i++) {printf ("\ n%d->", i);        p = g->adjlist[i].firstedge;            while (p) {printf ("%d->", P->adjvex);        p = p->next;    } printf ("end\n");    }}//DFS traversal of void Dfstraverse (Graphadjlist *g,int i) {int j = 0;    Edgenode *p; G->adjlist[i].    Flag = 1;    printf ("%c->", g->adjlist[i].data);    p = g->adjlist[i].firstedge; while (P! = NULL) {if (G->adjlist[p->adjvex].        Flag = = 0) {dfstraverse (G,p->adjvex);    } p = p->next;        }}//Determine if the queue is empty int queueempty (queuelist *q) {if (Q->front = = q->rear)    return 0;                                            else return 1;}    BFS traversal void Bfstraverse (Graphadjlist *g) {int i = 0,k = 0,flag = 0;    Edgenode *s;    Queuelist Q;    Initqueue (&AMP;Q); for (i = 0;i < G-> numvertex;i++) {g->adjlist[i].    Flag = 0; } for (i = 0;i < g->numvertex;i++) {if (G->adjlist[i]. Flag = = 0) {G->adjlist[i].            Flag = 1;//printf ("%c", g->adjlist[i].data);            Insertqueue (&q,&i);                while (Queueempty (&q)) {deletequeue (&q,&i);                printf ("%c->", g->adjlist[i].data);                s = g->adjlist[i].firstedge;                    while (s! = NULL) {k = s->adjvex; if (G->adjlist[k]. Flag = = 0) {G->adjlist[k].                        Flag = 1;//printf ("%c", g->adjlist[k].data);                    Insertqueue (&Q,& (S->adjvex));                } s = s->next; }}}} printf ("end\n");}                               int main () {int k = 0; Depth-first traversal from the 1th vertex (Start graphadjlist *g by input order);    Creategraph (G);    printf (the adjacency table for the "\ n vertex is: \ n");                                Print (G);    The adjacency table according to the head interpolation method printf ("\ndfs result is: \ n");                        Dfstraverse (G,K);    Depth-first traversal of printf ("end\n");    printf ("The result of the \NBFS is: \ n");                         Bfstraverse (G); Breadth first traverse return;}

  

Implementing DFS and BFS with adjacency tables

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.