C++--十字連結串列
阿新 • • 發佈:2018-11-25
#include<iostream.h> #define max 100 typedef struct arcnode{ int tailvex,headvex; arcnode *tlink,*hlink; int info; }arcnode; typedef struct{ char data; arcnode *firstin,*firstout; }box; class olgraph{ box xlist[max]; int n,e; public: void create(int m,int k); int locatex(char x); int indegree(char x); int outdegree(char x); void addarc(char u,char v,int w); }; int olgraph::locatex(char x){ for(int i=0;i<n;i++) if(x==xlist[i].data)return i; return -1; } void olgraph::create(int m,int k){ for(int i=0;i<m;i++){ cin>>xlist[i].data; xlist[i].firstin=xlist[i].firstout=NULL; } n=m;e=k; char u,v; int t,h,w; arcnode *p; for(i=0;i<k;i++){ cin>>u>>v>>w; t=locatex(u); h=locatex(v); p=new arcnode; p->tailvex=t; p->headvex=h; p->info=w; p->tlink=xlist[t].firstout; xlist[t].firstout=p; p->hlink=xlist[h].firstin; xlist[h].firstin=p; } } int olgraph::outdegree(char x){ int t=locatex(x); arcnode *p=xlist[t].firstout; int count=0; while(p){ count++; p=p->tlink; } return count; } int olgraph::indegree(char x){ int t=locatex(x); arcnode *p=xlist[t].firstin; int count=0; while(p){ count++; p=p->hlink; } return count; } void main(void){ olgraph g; g.create(4,4); cout<<g.outdegree('a')<<endl; cout<<g.indegree('d')<<endl; }