#include "stdio.h"
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶,將通過(guò)不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名注冊(cè)、虛擬主機(jī)、營(yíng)銷軟件、網(wǎng)站建設(shè)、柘榮網(wǎng)站維護(hù)、網(wǎng)站推廣。
#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; /* 頂點(diǎn)數(shù) */
LNode *head[MAX];
int i;
n = 3;
printf("List degree of all vertex : \n");
OutputDegree(matrix1, n);
for(i=0; in; i++)
{
head[i] = (LNode *)malloc(sizeof(LNode));
head[i]-data = i+1;
}
createAdjList(matrix1, n, head);
printf("Adjancency List : \n");
for(i=0; in; i++)
{
printf("%d : ", head[i]-data);
list(head[i]);
printf("\n");
}
n = 5;
printf("List degree of all vertex : \n");
OutputDegree(matrix2, n);
for(i=0; in; i++)
{
head[i] = (LNode *)malloc(sizeof(LNode));
head[i]-data = i+1;
}
createAdjList(matrix2, n, head);
printf("Adjancency List : \n");
for(i=0; in; i++)
{
printf("%d : ", head[i]-data);
list(head[i]);
printf("\n");
}
}
/* 輸出頂點(diǎn)的度 */
void OutputDegree(int matrix[MAX][MAX], int n)
{
int InDegree[MAX]; /* 入度 */
int OutDegree[MAX]; /* 出度 */
int i, j;
for(i=0; in; i++)
{
InDegree[i] = OutDegree[i] = 0;
for(j=0; jn; j++)
{
InDegree[i] += matrix[j][i];
OutDegree[i] += matrix[i][j];
}
}
for(i=0; in; i++)
{
printf("vertex %d : In - %d, Out - %d\n", i+1, InDegree[i], OutDegree[i]);
}
}
void Insert(LNode *head, int data)
{
LNode *pre = head-next;
LNode *temp;
temp = (LNode*)malloc(sizeof(LNode));
temp-data = data;
temp-next = 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("%d\t", curr-data);
}
}
/* 根據(jù)鄰接矩陣構(gòu)建鄰接表 */
void createAdjList(int matrix[MAX][MAX], int n, LNode *head[])
{
int i, j;
for(i=0; in; i++)
{
for(j=0; jn; j++)
{
if(matrix[i][j] != 0)
Insert(head[i], j+1);
}
}
}