Graph breadth Traversal

Source: Internet
Author: User


# Include <stdio. h>
# Include <stdlib. h>
# Include <conio. h>
# Include <string. h>
Int visited [10];/* access flag array */
Typedef struct ArcCell {
Int adj;/* vertex link type. 1 indicates adjacent, 0 indicates not adjacent */
} ArcCell, ** AdjMatrix;/* adjacent matrix */
Typedef struct type {
Char data [3];/* vertex value */
Struct type * next;/* next pointer to the vertex */
} VertexType;
Typedef struct {
VertexType * vexs;/* vertex vector */
AdjMatrix arcs;/* adjacent matrix */
Int vexnum, arcnum;/* Number of vertices and edges of the graph */
} MGraph;
/********************************/
Typedef struct QNode {
Int elem;
Struct QNode * next;
} QNode, * QueuePtr;/* Team data type */
Typedef struct
{QueuePtr front;/* head pointer */
QueuePtr rear;/* Team tail pointer */
} LinkQueue;
Int InitQueue (LinkQueue * Q)/* initialize queue */
{Q-> front = Q-> rear = (QueuePtr) malloc (sizeof (QNode);/* allocate space */
If (! Q-> front) exit (0 );
(Q-> front)-> next = NULL;
Return 1;
}
Int EnQueue (LinkQueue * Q, int e)/* inserted at the end of the queue */
{QueuePtr p;
P = (QueuePtr) malloc (sizeof (QNode);/* allocate space */
If (! P) exit (0 );
P-> elem = e; p-> next = NULL;
(Q-> rear)-> next = p;
Q-> rear = p;/* reset the team end */
Return 1;
}
Int QueueEmpty (LinkQueue Q)/* empty Team */
{If (Q. front = Q. rear) return 1;
Else return 0 ;}
Int DelQueue (LinkQueue * Q, int * e)/* Delete the first element of the queue */
{QueuePtr p;
If (QueueEmpty (* Q) return 0;
P = (Q-> front)-> next;
* E = p-> elem;
(Q-> front)-> next = p-> next;
If (Q-> rear = p) Q-> rear = Q-> front;
Free (p );
Return 1;
}
/**************************/
Void InitGraph (MGraph * G)/* Initial graph */
{Int I, nu, mu;
Printf ("Number of input vertices and number of (edge) arcs :");
Scanf ("% d", & nu, & mu );
G-> arcs = (ArcCell **) malloc (nu * sizeof (ArcCell *));
For (I = 0; I <nu; I ++)/* allocate the adjacent matrix space */
G-> arcs [I] = (ArcCell *) malloc (nu * sizeof (ArcCell ));
G-> vexs = (VertexType *) malloc (nu * sizeof (VertexType);/* allocate vertex space */
G-> vexnum = nu; G-> arcnum = mu;/* Number of vertices and edges */
}
Void InsertGraph (MGraph * G, int I, VertexType e)
{If (I <0 | I> G-> vexnum) return;
G-> vexs [I]. next = e. next;
Strcpy (G-> vexs [I]. data, e. data );

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.