Home > Back-end >  Figure subsystem (under the Dev - C function 1 can be done properly, but dropped out of the run wind
Figure subsystem (under the Dev - C function 1 can be done properly, but dropped out of the run wind

Time:02-24

# include "stdio.h"
# include "malloc. H"
# define MAX 100
Typedef char VertexType;
Int visited (MAX);
Typedef struct node
{
Int adjvex;
Struct node * next;
} EdgeNode;
Typedef struct vexnode
{
VertexType data;
EdgeNode * firstedge;
} VHeadNode;
Typedef struct
{
VHeadNode adjlist (MAX).
Int n, e;
} AdjList;
Void CreateAGraph (AdjList * g, int flag)
{
Int I, j, k;
EdgeNode * p;
If (flag==0)
Printf (" \ n will establish an undirected graph, \ n ");
The else
Printf (" \ n will build a directed graph, \ n ");
Printf (" please enter the figure on the number of vertices: ");
The scanf (" % d ", & amp; G - & gt; N);
Printf (" please enter the figure number: ");
The scanf (" % d ", & amp; G - & gt; E);
Printf (" \ n please enter the figure of each vertex information: \ n ");
for(i=0; I{
Printf (" % d a vertex information: ", I + 1);
The scanf (" c "\ n %, & amp; (g - & gt; Adjlist [I]. Data));
G - & gt; Adjlist [I] firstedge=NULL;
}
Printf (" \ n please enter edge information, input format is: serial number 1, number 2 (serial number is 0,,,,) : \ n ");
For (k=0; K{
Printf (" please enter the article % d side: ", k);
The scanf (" \ n % d, % d ", & amp; I, & amp; J);

P=(EdgeNode *) malloc (sizeof (EdgeNode));
P - & gt; Adjvex=j;
P - & gt; Next=g - & gt; Adjlist [I] firstedge;
G - & gt; Adjlist [I] firstedge=p;

If (flag=0)
{
P=(EdgeNode *) malloc (sizeof (EdgeNode));
P - & gt; Adjvex=I;
P - & gt; Next=g - & gt; Adjlist [j] firstedge;
G - & gt; Adjlist [j] firstedge=p;
}
}
}

Void DispAGraph (AdjList * g)
{
int i;
EdgeNode * p;
Printf (" the figure said adjacency list is as follows: \ n \ n ");
for(i=0; I{
Printf (" % 2 d % [c] ", I, g - & gt; Adjlist [I]. Data);
P=g - & gt; Adjlist [I] firstedge;
While (p!=NULL)
{
Printf (" -- -- & gt; [% d] ", p - & gt; Adjvex);
P=p - & gt; Next;
}
printf("\n");
}
}
Void DFS (AdjList * g, int vi)
{
EdgeNode * p;
Printf (" % d ", vi);
Printf (" % c), "the g & gt; Adjlist [vi]. Data);
Visited [vi]=1;
P=g - & gt; Adjlist [vi] firstedge;
While (p!=NULL)
{
If (visited [p - & gt; adjvex]==0)
DFS (g, p - & gt; Adjvex);
P=p - & gt; Next;
}
}
Void BFS (AdjList * g, int vi)
{
Int I, v, visied (MAX);
Int qu (MAX), front=0, rear=0;
EdgeNode * p;
for(i=0; IVisited [I]=0;
Printf (" % d ", vi);
Printf (" % c), "the g & gt; Adjlist [vi]. Data);
Visited [vi]=1;
Rear=MAX (rear + 1) %;
Qu (rear)=vi;
While (front!=rear)
{front=MAX (front + 1) %;
V=qu (front),
P=g - & gt; Adjlist [v] firstedge;
While (p!=NULL)
{if (visited [p - & gt; adjvex]==0)
{visited [p - & gt; adjvex]=1;
Printf (" % d ", p - & gt; Adjvex);
Printf (" % c), "the g & gt; Adjlist [p - & gt; adjvex]. Data);
Rear=MAX (rear + 1) %;
Qu (rear)=p - & gt; Adjvex;
}
P=p - & gt; Next;
}
}
}
Void MenuGraph ()
{
Printf (" \ n figure subsystem ");
Printf (" \ n====================================");
Printf (" \ n | 1. Update the adjacency table | ");
Printf (" \ n | 2. The depth-first traversal | ");
Printf (" \ n | 3. Breadth-first traversal | ");
Printf (" \ n | 0. Return | ");
Printf (" \ n====================================");
Printf (" \ n please input menu (0-3) : ");
}

The main ()
{
Int I, f;
Char ch1 and ch2, a;
AdjList g;
Ch1='y'
While (ch1=='y' | | ch1=='y')
{MenuGraph ();
The scanf (" % c ", & amp; Ch2);
Getchar ();
The switch (ch2)
{
Case '1' :
Printf (" is to establish a directed graph or undirected graph (1) (0), please select 1 or 0 (input) : ");
The scanf (" % d ", & amp; F);
CreateAGraph (& amp; G, f);
DispAGraph (& amp; G);
break;
Case '2' :
Printf (" please enter to begin at the apex of the depth-first traversal sequence number (serial number are numbered starting from 0) : ");
The scanf (" % d ", & amp; F);
Printf (" \ n from vertex % d depth-first traversal sequence is: ", f);
for(i=0; IVisited [I]=0;
DFS (& amp; G, f);
break;
Case '3' :
Printf (" please enter to begin at the apex of the breadth traversal sequence number (serial Numbers starting from 0) : ");
The scanf (" % d ", & amp; I);
Printf (" \ n from vertex % d breadth-first traversal sequence is: ", I);
BFS (& amp; G, I);
break;
Case '0' :
Ch1='n'. break;
Default:
Printf (" input is wrong, please input 3-0 to choose!" );
}
If (ch2!='0')
{printf (" \ n press the enter key to continue, press any key to return to the main menu! \n");
A=getchar ();
If (a!='\ xA');
{
Getchar (); Ch1='n'.
}
}
}
}
  • Related