komghao
幼苗
共回答了12个问题采纳率:100% 举报
#include "stdio.h"
#define MAX 50
typedef struct LNode{
int data;
struct LNode *next;
}LNode;
void OutputDegree(int matrix[MAX][MAX], int n);
void Insert(LNode *head, int data);
void list(LNode *head);
void createAdjList(int matrix[MAX][MAX], int n, LNode *head[]);
void main()
{
int matrix1[MAX][MAX] = {{0,1,1},{1,0,1},{0,0,0}};
int matrix2[MAX][MAX] = {{0,1,0,1,0},{1,0,1,0,0},{0,1,0,1,1},{1,0,1,0,0},{0,0,1,0,0}};
int n; /* 顶点数 */
LNode *head[MAX];
int i;
n = 3;
printf("List degree of all vertex : n");
OutputDegree(matrix1, n);
for(i=0; idata = i+1;
}
createAdjList(matrix1, n, head);
printf("Adjancency List : n");
for(i=0; idata);
list(head[i]);
printf("n");
}
n = 5;
printf("List degree of all vertex : n");
OutputDegree(matrix2, n);
for(i=0; idata = i+1;
}
createAdjList(matrix2, n, head);
printf("Adjancency List : n");
for(i=0; idata);
list(head[i]);
printf("n");
}
}
/* 输出顶点的度 */
void OutputDegree(int matrix[MAX][MAX], int n)
{
int InDegree[MAX]; /* 入度 */
int OutDegree[MAX]; /* 出度 */
int i, j;
for(i=0; inext = NULL;
if(pre == NULL)
{
head->next = temp;
return;
}
for(; pre->next!=NULL; pre=pre->next);
pre->next = temp;
}
void list(LNode *head)
{
LNode *curr;
for(curr=head->next; curr!=NULL; curr=curr->next)
{
printf("%dt", curr->data);
}
}
/* 根据邻接矩阵构建邻接表 */
void createAdjList(int matrix[MAX][MAX], int n, LNode *head[])
{
int i, j;
for(i=0; i
1年前
5