标签:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#define MAX 20
typedef struct node
{
struct node * nextarc;
char Info;
}ArcNode,*pArcNode;
typedef struct
{
int kind; //类型
int Degree; //度数
int VerCount;//顶点数目
int ArcCount; //弧的数目
pArcNode Adj;
}AdjList,*pAdjList;
void CreatAdj(pAdjList myAdjList)
{
int i,j;
char tail, head;
pArcNode p,q;
myAdjList->Degree = 0;
printf("Please input the kind of Adjacency List :1-Digraph 2-Undigraph");//表的种类
scanf("%d", &myAdjList->kind);
if (myAdjList->kind != 1 && myAdjList->kind != 2)
return ;
printf("Please input the num of Vertex:");//顶点个数
scanf("%d", &myAdjList->VerCount);
printf("Pleaase input the num of Arc");//弧的个数
scanf("%d", &myAdjList->ArcCount);
myAdjList->Degree = 0; //度数初始化为0
myAdjList->Adj = (pArcNode)malloc(myAdjList->VerCount *sizeof(ArcNode));
for (i = 0; i < myAdjList->VerCount; i++)
myAdjList->Adj[i].nextarc = NULL;
printf("Please input the Graph: \n");
printf("Please input all of the Arc:\n");
fflush(stdin);
for (i = 0; i < myAdjList->ArcCount; i++)
{
scanf("%c", &myAdjList->Adj[i].Info);
fflush(stdin);
}
printf("Please input the Ver:\n");
for (i = 0; i < myAdjList->ArcCount; i++)
{
printf("Please input tail of Arc%d :", i);
scanf("%c", &tail);
p = (pArcNode)malloc(sizeof(ArcNode));
p->nextarc = NULL;
fflush(stdin);
printf("Please input head of Arc%d :", i);
scanf("%c", &p->Info);
getchar();
for ( j = 0; j < myAdjList->VerCount; j++)
{
if (myAdjList->Adj[j].Info == tail)
{
q = &myAdjList->Adj[j];
while (q->nextarc != NULL)
q = q->nextarc;
q->nextarc = p;
}
}
}
}
void Print(pAdjList myAdjList)
{
int i = 0, j = 0, count = 0;
pArcNode p1,p2;
for (i = 0; i < myAdjList->ArcCount; i++)
{
p1 = &myAdjList->Adj[i];
p2 = p1;
while (p1->nextarc != NULL)
{
printf("%c--%c\n",p2->Info,p1->nextarc->Info);
count++;
p1 = p1->nextarc;
}
if (count>myAdjList->Degree)
myAdjList->Degree = count;
count = 0;
}
if (myAdjList->Degree == 0)
printf("Not Creat\n");
else
printf("Degree is %d\n", myAdjList->Degree+1);
}
bool Judge(char tail, char head, pAdjList myAdjList)
{
bool findhead=false,findtail = false;
int i = 0;
pArcNode p;
for (i = 0; i < myAdjList->VerCount; i++)
{
if (tail == myAdjList->Adj[i].Info)
findtail = true;
if (findtail == true)
{
p = &myAdjList->Adj[i];
while (p)
{
if (p->Info == head)
{
findhead = true;
return true;
}
else
p = p->nextarc;
}
return
false;
}
}
return false;
}
int main()
{
AdjList myAdjList;
char tail, head;
bool find=false;
int choice = 0;
while (1)
{
printf("Please input your choice : 1-Creat 2-Print 3-Judge");
scanf("%d", &choice);
switch (choice)
{
case 1:
CreatAdj(&myAdjList);
break;
case 2:
Print(&myAdjList);
break;
case 3:
printf("Please input the tail:");
fflush(stdin);
scanf("%c", &tail);
fflush(stdin);
printf("Please input the head:");
scanf("%c", &head);
getchar();
find=Judge(tail,head,&myAdjList);
if (find == true)
printf("Have Found\n");
else
printf("Not Found\n");
break;
default:
return 0;
}
}
system("pause");
return 0;
}
标签:
原文地址:http://www.cnblogs.com/da-peng/p/5003997.html