⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 作业题4.txt

📁 中南的数据结构试验1
💻 TXT
字号:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#define   MAX_VERTEX_NUM   20
#define   NULL   0
#define   N       8
typedef  struct arcnode{
int adjvex;
struct arcnode *nextarc;
int weight;
}arcnode;
typedef struct vnode{
char data[N];
arcnode *firstarc;
}vnode,adjlist[MAX_VERTEX_NUM];
typedef struct{
vnode adjlist[MAX_VERTEX_NUM];
int vexnum,arcnum;
}algraph;
void create(algraph G);
void inforg(algraph G);
int locatevex(algraph G,char *v);
void main()
{
  algraph G;
  int sum;
  clrscr();
  create(G);
  inforg(G);
}
void create(algraph G)
{
  int i,j,k;
  char v1[N],v2[N];
  arcnode *p,*q;
  printf("\ninput the num of G.vexnum and G.arcnum:");
  scanf("%d%d",&G.vexnum,&G.arcnum);
  for(i=0;i<G.vexnum;i++) {
       printf("\ninput the data of G:");
       if(i==0) getchar();
       gets(G.adjlist[i].data);
       G.adjlist[i].firstarc=NULL;
  }
  for(i=0;i<G.arcnum;i++) {
       printf("\ninput the end data  of one arc:");
       if(i) getchar();
       gets(v1);
       printf("\ninput the head data  of one arc:");
       gets(v2);
       k=locatevex(G,v1);
       j=locatevex(G,v2);
       p=(arcnode *)malloc(sizeof(arcnode));
       p->adjvex=j;
       printf("input the weight of arc:");
       scanf("%d",&p->weight);
       p->nextarc=NULL;
       if(!G.adjlist[k].firstarc) {
	   G.adjlist[k].firstarc=p;
	   q=p;
       }
       else {
	   q->nextarc=p;
	   p->nextarc=NULL;
	   q=p;
       }
  }
}
int locatevex(algraph G,char *v)
{
  int i;
  for(i=0;i<G.vexnum;i++)
       if(!strcmp(v,G.adjlist[i].data)) return(i);
       return(-1);
}
void inforg(algraph G)
{
  int i,j;
  arcnode *p;
  for(i=0;i<G.vexnum;i++) {
       p=G.adjlist[i].firstarc;
       for(j=0;G.adjlist[i].data[j]!='\0';j++)
		 printf("%c",G.adjlist[i].data[j]);
       printf("->");
       while(p){
	   for(j=0;G.adjlist[p->adjvex].data[j]!='\0';j++)
		 printf("%c",G.adjlist[p->adjvex].data[j]);
	   printf("(the weight of this arc is %d)",p->weight);
	   printf("->");
	   p=p->nextarc;
       }
       printf("NULL\n");
    }
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -