C语言邻接表爱吃窝窝头2023-11-302023-11-30#include <stdio.h>#include <stdlib.h>#define N 20typedef struct link{ int data;//代表数据域 struct link *next;//代表指针域,指向后继元素}link;//link为节点名,每个节点都是一个link结构体link *b[N];//头节点//创建void creat(int n){ int u,v; link *p; for(u=0;u<n;u++) b[u]=NULL; scanf("%d%d",&u,&v); while(u>=0) { p=(link*)malloc(sizeof(link)); p->data=v; p->next=b[u]; b[u]=p; scanf("%d%d",&u,&v); }}//遍历void output(int n){ link *p; int u; for(u=0;u<n;u++) { printf("%5d",u); p=b[u]; while(p) { printf("%5d",p->data); p=p->next; } printf("\n"); }}void main(){ creat(6); printf("邻接表为:\n"); output(6);}