第五周作业--有向图邻接表示

来源:互联网 发布:大学刷题软件 编辑:程序博客网 时间:2024/05/29 18:22

邻接表

struct arcnode  //定义边表结点{ int adjvex; arcnode *next;}template<class datatype> //定义顶点表结点struct vertexNode{ datatype vertex; arcnode *firstedge;}

未完,待续...

#include<stdio.h>#include<stdlib.h>#define MaxVertexNum 100;typedef struct node //边表节点{ int adjvex; node *next;}EdgeNode;typedef struct //顶点表节点{ char vertex; EdgeNode *firstedge;//指向边表的指针}VertexNode,AdjList[100];typedef struct{ AdjList adjlist; int n,e;}ALGraph;void create(ALGraph *G)//形参是一个指向ALGraph类型的指针{ int i,j,k,w,v; EdgeNode *s; printf("读入顶点数和边数"); scanf("%d,%d",&G->n,&G->e); for(i=0;i<G->n;i++) {  fflush(stdin);  printf("建立顶点表");  G->adjlist[i].vertex=getchar();  G->adjlist[i].firstedge=NULL; } printf("建立边表\n"); for(k=0;k<G->e;k++) { //这里开始读入顶点序号  printf("读入(vi-vj)的顶点对序号");  scanf("%d,%d",&i,&j);  s=(EdgeNode*)malloc(sizeof(EdgeNode));  s->adjvex=j;  s->next=G->adjlist[i].firstedge;  G->adjlist[i].firstedge=s;  s=(EdgeNode*)malloc(sizeof(EdgeNode));  s->adjvex=1;  s->next=G->adjlist[j].firstedge;  G->adjlist[j].firstedge=s; }}int main(){ ALGraph *G=(ALGraph*)malloc(sizeof(ALGraph)); create(G); for(int i=0;i<G->n;i++) {  printf("%d->",i);  while(G->adjlist[i].firstedge !=NULL)  {   printf("%d->",G->adjlist[i].firstedge->adjvex);   G->adjlist[i].firstedge=G->adjlist[i].firstedge->next;  }  printf("\n"); }return 0;}


0 0
原创粉丝点击