图-邻接表(无向图)
原理:备注:
代码:
#include <stdio.h>
#include <stdlib.h>
#define MAX 4
typedef struct list
{
int datas;
struct list *next;
}list,*lists;
typedef struct like
{
char data;
list *temp;
}like;
void loop_list (list **nice);
void prin_list (list *nice);
void prin_list (list *nice)
{
while(nice != NULL)
{
printf("%d ", nice->datas);
nice = nice->next;
}
}
void loop_list (list **nice)
{
list *temp, *tail;
int number;
while(1)
{
scanf("%d", &number);
if(number >= 0 && number <= 9) //输入9以上的数字结束本函数
{
temp = (lists )malloc(sizeof(list ));
temp->datas = number;
temp->next = NULL;
if(*nice == NULL)
{
*nice = temp;
}
else
{
tail->next = temp;
}
tail = temp;
}
else
{
break;
}
}
}
int main()
{
like diss;
for(int i = 0; i < MAX; i++)
{
scanf("%s", &diss.data);
diss.temp = NULL;
loop_list(&diss.temp);
}
for(int i = 0; i < MAX; i++)
{
printf("%s ", diss.data);
prin_list(diss.temp);
putchar('\n');
}
return 0;
}
执行框运行状态:
{:10_275:}
页:
[1]