The graph is based on the Adjacency table method as the storage method, the depth-first search and the breadth-first search (both non-recursive) of the graph
# include <stdio.h>
# include <stdlib.h>
# define TRUE 1
# define FALSE 0
# define Error-1
# define OK 1
# define Max_vertex_num 20
int Visited[max_vertex_num];
typedef char VertexData;
typedef ENUM{DG,DN,UDG,UDN} Graphkind;
typedef struct arcnode{//ARC node structure
int Adjvex;
struct Arcnode *nextarc;
Otherinfo info;
}arcnode;
typedef struct vertexnode{//table header node structure
VertexData data;
Arcnode *firstarc;
}vertexnode;
typedef struct {
Vertexnode Vertex[max_vertex_num];
int vexnum,arcnum;
Graphkind kind;
}adjlist;
Knowledge ******************************* about Stacks
typedef struct NODE
{
int num;
struct node *next;
}linkstacknode, *linkstack;
int Initstack (Linkstack *s)//initialization stack
{
(*s) = (linkstack) malloc (sizeof (Linkstacknode));
(*s)->next = NULL;
if ((*s) = NULL)
return True;
Else
return False;
}
int Push (linkstack S, int x)//into the stack
{
Linkstack temp;
temp = (linkstack) malloc (sizeof (Linkstacknode));
if (temp = = NULL)
return False;
Temp->num = x;
Temp->next = s->next;
S->next = temp;
return True;
}
int Pop (linkstack S, int *x)//out Stack
{
Linkstack temp;
temp = s->next;
if (temp = = NULL)
return False;
*x = temp->num;
S->next = temp->next;
Free (temp);
return True;
}
int IsEmpty (Linkstack S)//Judging if the stack is empty
{
if (S->next = = NULL)
return True;
Else
return False;
}
//*************************************************************************
About the team knowledge **********************************************
typedef struct NODE
{
int data;
struct Node *next;
}linkqueuenode;
typedef struct
{
Linkqueuenode *front;
Linkqueuenode *rear;
}linkqueue;
int Initqueue (Linkqueue *q)//The initialization of the chain queue
{
Q->front = (Linkqueuenode *) malloc (sizeof (Linkqueuenode));
if (q->front! = NULL)
{
Q->rear = q->front;
Q->front->next = NULL;
return (True);
}
Else
return False;
}
int Enterqueue (linkqueue *q, int x)//Chain queue Enqueue
{
Linkqueuenode *newnode;
NewNode = (Linkqueuenode *) malloc (sizeof (Linkqueuenode));
if (NewNode! = NULL)
{
Newnode->data = x;
Newnode->next = NULL;
Q->rear->next = NewNode;
Q->rear = NewNode;
return True;
}
Else
return False;
}
int Deletequeue (linkqueue *q, int *x)//Chain team list
{
Linkqueuenode *p;
if (Q->front = = q->rear)
return False;
p = q->front->next;
Q->front->next = p->next;
if (q->rear = = p)
Q->rear = q->front;
*x = p->data;
Free (p);
return True;
}
int empty (Linkqueue *q)//Determine if the link stack is empty
{
if (Q->front = = q->rear)
return True;
Else
return False;
}
//*************************************************************************************
int Firstadjvertex (adjlist *g, int v)//First adjacency point for V
{
int W;
Arcnode *p;
p = g->vertex[v].firstarc;
if (p = = NULL)
return Error;
W = p->adjvex;
Return w;
}
Int Nextadjvertex (adjlist *g, int v, int w) //Seeking v next adjacency point relative to W
{
int k;
arcnode *p;
p = g->vertex[v].firstarc;
while (P->adjvex! = w)
{
p = P->nextarc;
}
p = P->nextarc;
if (p = = NULL)
return Error;
else
{
k = P->adjvex;
return K;
}
}
int Locatevertex (adjlist *g, VertexData v) //Seek vertex position function
{
int j = error,k;
for (k = 0; k < g->vexnum; k++)
if (g->vertex[k].data = = v)
{
j = K;break;
}
return J;
}
Void Crtadjlist (adjlist *g) //Create adjacency list
{
int n,e,i,j,k;
char VT,VH;
arcnode *p;
printf ("Please enter the number of vertices of the graph and the number of arcs \ n");
scanf ("%d%d", &n, &e);
g->vexnum = n;
g->arcnum = e;
printf ("Please enter the vertex information \ n");
getchar ();
for (i = 0; i < n; i++)
{
scanf ("%c", & (G->vertex[i].data));
g->vertex[i].firstarc = NULL;
}
printf ("Please enter two vertices of arc \ n");
for (k = 0; k < e; k++)
{
getchar ();
scanf ("%c%c", &VT, &VH);
i = Locatevertex (G,VT);
j = Locatevertex (G,VH);
p = (Arcnode *) malloc (sizeof (Arcnode));
p->adjvex = j;
p->nextarc = g->vertex[i].firstarc;
g->vertex[i].firstarc = p;
}
}
void Depthfirstsearch (adjlist *g, int v0)//Depth-First search
{
Linkstacknode *s;
int V, W;
Initstack (&s);
Push (S, V0);
while (! IsEmpty (S))
{
Pop (S, &v);
if (!visited[v])
{
printf ("%c", g->vertex[v].data);
VISITED[V] = True;
W = Firstadjvertex (g,v);
while (w! =-1)
{
if (!visited[w])
Push (S, W);
W = Nextadjvertex (g,v,w);
}
}
}
}
void Traversegraph (Adjlist *g)//Deep Search
{
int VI;
for (vi = 0; vi < g->vexnum; vi++)
VISITED[VI] = False; Initializing an array of flags
for (vi = 0; vi < g->vexnum; vi++)
if (!visited[vi])
Depthfirstsearch (G,VI);
}
void Breadthfirstsearch (adjlist *g, int v0)//Breadth First Search
{
Linkqueue Q;
int V, W;
printf ("%c", g->vertex[v0].data);
Visited[v0] = True;
Initqueue (&Q);
Enterqueue (&Q,V0);
while (! Empty (&Q))
{
Deletequeue (&q, &v);
W = Firstadjvertex (g, v);
while (w! =-1)
{
if (!visited[w])
{
printf ("%c", g->vertex[w].data);
VISITED[W] = True;
Enterqueue (&Q,W);
}
W = Nextadjvertex (g,v,w);
}
}
}
void Breadthtraversegraph (Adjlist *g)//Wide Search
{
int VI;
for (vi = 0; vi < g->vexnum; vi++)
VISITED[VI] = False; Initializing an array of flags
for (vi = 0; vi < g->vexnum; vi++)
if (!visited[vi])
Breadthfirstsearch (G,VI);
}
int main (void)
{
Adjlist G;
Crtadjlist (&G);
printf ("Depth-first search traversal output \ n");
Traversegraph (&G);
printf ("\ n breadth-first search traversal output \ n");
Breadthtraversegraph (&G);
printf ("\ n");
return 0;
}
Depth-first search and breadth-first search for graphs