#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <conio.h>
#include <iostream>
#define MAX_VERTEX_NUM 20/* Maximum vertex number * *
#define INFINITY 32767//MAX. * *
#define STACK_INIT_SIZE 100//stack maximum space
#define STACKINCREMENT 20//stack of additional space units
#define OK 1
#define ERROR 0
#define OVERFLOW-2
#define OUTPLACE-1
typedef int STATUS;
typedef int VERTEXTYPE;
typedef int INFOTYPE;
typedef int SELEMTYPE;
typedef struct{
Selemtype *base; /* Stack Bottom pointer * *
Selemtype *top; /* Stack top pointer/*
int stacksize; * * The size of the stack * *
}sqstack;
typedef struct arcnode{
int Adjvex; /* The position of the vertex to which the arc is directed * *
struct Arcnode *nextarc; * * point to the arrow on the ARC
InfoType info; /* The ARC related information * *
}arcnode;
typedef struct vnode{
Vertextype data; /* Vertex Information * *
Arcnode *firstarc; /* A pointer to the first arc attached to the vertex/*
}vnode,adjlist[max_vertex_num];
typedef struct{
Adjlist vertices;
int vexnum,arcnum; /* Graph current number of vertices and arcs * *
int kind; /* Diagram of the kind of Mark * *
}algraph;
int Indegree[max_vertex_num];
Status Initstack (Sqstack &s)//initialization stack
{
S.base= (Selemtype *) malloc (stack_init_size*sizeof (Selemtype));
if (! s.base) exit (OVERFLOW);
S.top=s.base;
S.stacksize=stack_init_size;
return OK;
}
Status GetTop (sqstack &s,selemtype &e)//take top element of stack
{
if (s.top==s.base) return ERROR;
e=* (s.top-1);
return OK;
}
Status Push (sqstack &s,selemtype E1)//Stack operation
{
if (s.top-s.base>=s.stacksize) {
S.base= (Selemtype *) realloc (s.base,
(s.stacksize+stackincrement) *sizeof (Selemtype));
if (! s.base) exit (OVERFLOW);
S.top=s.base+s.stacksize;
S.stacksize+=stackincrement;
}
*s.top++=e1;
return OK;
}
Status Pop (sqstack &s,selemtype &e)//out stack operation
{
if (s.top==s.base) return ERROR;
e=* (--s.top);
return OK;
}
Status stackempty (Sqstack S)//NULL operation
{
if (s.top==s.base)
return 1;
Else
return 0;
}
int Locatevex (algraph &g,vertextype v)/* return vertex v in G subscript */
{
int i;
for (i=0;i<g.vexnum;i++)
if (v==g.vertices[i].data) return i;
return-1;
}
Algraph build_alg_dn ()/* Create a forward network * *
{Algraph G;
int i,j,k,v1,v2;
Arcnode *p=null,*q=null;
for (i=0;i<max_vertex_num;i++)
G.vertices[i].firstarc=null; /* Initialize * *
printf ("Please enter the number of vertices:");
scanf ("%d", &g.vexnum); /* Input Vertex number * *
if (g.vexnum<0)
{
printf ("Error/n");
return G;
}
printf ("Please enter the number of edges:");
scanf ("%d", &g.arcnum); /* Input Edge number * *
if (g.arcnum<0)
{
printf ("Error/n");
return G;
}
for (i=0;i<g.vexnum;i++)/* Input vertex sequence * *
{
printf ("Please enter vertex information:");
scanf ("%d", &g.vertices[i].data);
}
for (i=0;i<g.arcnum;i++)//input Edge information * *
{
printf ("Please enter the edge of the information (V1,V2):");
scanf ("%d,%d,%d", &v1,&v2);
J=locatevex (G,V1), K=locatevex (G,V2); /* Get the position of V1 and V2 in G * *
if (j<0| | k<0| | J==K)
{
printf ("error!! /n ");
I--;continue;
}/* If the vertex input error, continue * *
p= (Arcnode *) malloc (sizeof (Arcnode)); /* Allocation Node * *
p->adjvex=k; p->nextarc=null;
if (! G.VERTICES[J].FIRSTARC) g.vertices[j].firstarc=p; /*p is the first arc of V1.
else {
for (Q=G.VERTICES[J].FIRSTARC;Q->NEXTARC;Q=Q->NEXTARC); * * Find the last node of the list/
q->nextarc=p;
}
}
return G;
}
Status insert_vertex_algraph (algraph &g)/* Insert a vertex * *
{
Char A;
char c;
c= ' Y ';
while (c== ' Y ')
{
printf ("Please enter vertex information:");
scanf ("%d", &a);
G.vertices[g.vexnum++].data=a; /* insert vertices, top points plus 1*/
printf ("Do you still want to insert vertices?") (y/n)? ");
scanf ("%s", &c);
}
return OK;
}
Status insert_arc_algraph (algraph &g)/* Insert a side */
{
int j,k;
Char v1,v2,c;
Arcnode *p,*q;
c= ' Y ';
while (c== ' y ')/* to determine whether to insert edges * *
{
printf ("Please enter the edge of the information (V1,V2):");
scanf ("%d,%d,%d", &v1,&v2); /* Input Edge information (vertex) * *
J=locatevex (G,V1); K=locatevex (G,V2);
if (j<0| | k<0| | j==k) {printf ("error!! /n "); Continue;} * * If the vertex does not belong to G, continue * *
p= (Arcnode *) malloc (sizeof (Arcnode)); /* Assign a node * *
p->adjvex=k; p->nextarc=null;
if (! G.VERTICES[J].FIRSTARC) g.vertices[j].firstarc=p; * * If V1 has no side, insert as First edge * * *
else/* If v1 has edges, insert the new edge into the last node.
{
for (Q=G.VERTICES[J].FIRSTARC;Q->NEXTARC;Q=Q->NEXTARC);
q->nextarc=p;
}
g.arcnum++;
printf ("Do you want to insert the edge?") ( y/n) ");
scanf ("%s", &c);
}
return OK;
}
Status Findindegree (algraph g,int indegree[])//The degree of each vertex of a direction graph
{
int i,k;
Arcnode *p;
for (i=0;i<g.vexnum;i++)
{
if (G.VERTICES[I].FIRSTARC)
{
P=g.vertices[i].firstarc;
k=p->adjvex;
Indegree[k]=1;
while (P->NEXTARC)
{
p=p->nextarc;
k=p->adjvex;
++INDEGREE[K];
}//while
}//if
}//for
return OK;
}
Status Topologicalsort (algraph &g)
{//Output direction graph acyclic graph topology has the direction sequence, if has the loop, returns the error.
int i,count,k;
Arcnode *p;
Sqstack S;
Findindegree (G,indegree); The degree of entry to each vertex
Initstack (S);
for (i=0;i<g.vexnum;i++)
if (!indegree[i]) Push (s,i); 0 into the vertex of the stack
count=0;
while (! Stackempty (S))//Stack not empty
{
Pop (S,i);
printf ("%d", g.vertices[i].data);
count++; Output i vertex, Count plus one
for (P=G.VERTICES[I].FIRSTARC;P;P=P->NEXTARC)
{
k=p->adjvex;
if (!) ( --indegree[k]) Push (s,k); Each adjacent point of vertex of I is reduced by one, if zero, the stack
}//for
}//while
if (count<g.vexnum) return ERROR; If the number of vertices in the output is less than the number of vertices in the graph, there is a loop within the graph, returning an error
else return OK;
}
int main ()
{
int i;
int c=1;
Algraph G;
Arcnode *p;
printf ("This program will create a map stored in the adjacency table, can be inserted into the vertex or edge of the operation,/n can be output to the graph of information or the output of the direction of the map of the topological forward sequence." /n Please press any key to continue/n ");
Getch ();
System ("CLS");
printf ("1.) Create a forward graph: (First please perform this step!) /n ");
System ("CLS");
printf ("======== Create a forward graph ==========/n");
printf ("All inputs are integers, and the edges are entered by which vertex the edge is entered by, and/n two vertices are separated by", "). /n ");
G=BUILD_ALG_DN ();
printf (Press any key to continue selecting the operation.) /n ");
Getch ();
while (c!=0)
{
System ("CLS");
printf ("1. Insert vertex/n");
printf ("2. Insert Edge/n");
printf ("3. Output to graph information (vertex and Edge)/n");
printf ("4. Output the topology of the direction Graph/n");
printf ("0. Exit/n");
printf ("Please select the action you want to perform:");
scanf ("%d", &c);
Switch (c)
{
Case 1:
System ("CLS");
printf ("======== insert vertex ===========/n");
Insert_vertex_algraph (G); /* Insert Vertex/*
printf (Press any key to continue selecting the operation.) /n ");
Getch ();
Break
Case 2:
System ("CLS");
printf ("======== insert Edge ==========/n");
Insert_arc_algraph (G); Insert Edge
printf (Press any key to continue selecting the operation.) /n ");
Getch ();
Break
Case 3:
System ("CLS");
printf ("======== Output to graph =========/n");
for (i=0;i<g.vexnum;i++)//output vertex information
{
printf ("%d", g.vertices[i].data);
}
printf ("n");
for (i=0;i<g.vexnum;i++)//output Edge information
{
for (P=G.VERTICES[I].FIRSTARC;P;P=P->NEXTARC)
printf ("%d-->%d", g.vertices[i].data,g.vertices[p->adjvex].data);
}
printf ("n");
printf ("/n" Press any key to continue selecting the operation.) /n ");
Getch ();
Break
Case 4:
System ("CLS");
printf ("======== the topological ===========/n sequence of the output direction graph");
if (! Topologicalsort (G))
printf ("There is a loop, error/n" in the N-direction graph);
printf ("/n" Press any key to continue selecting the operation.) /n ");
Getch ();
Break
Case 0:
Break
Default
printf ("/n) your choice is wrong." /n ");
Getch ();
Break
}//switch
}//while
printf ("/n Welcome next time ... ^_^/n");
Getch ();
return 0;
}
I am a junior computer professional student,
Please advise ...