#include<iostream.h> typedef struct node { int adjvex; node *next; char data; bool tag1; }*ptrn; class linklist { protected: int curposition; ptrn curptr; int count; public: ptrn head; void sethead(ptrn &head1) { head=head1; curposition=0; curptr=head; count=0; } ptrn getelemptr(int position) { if(curposition>position) { curposition=0; curptr=head; } for(;curposition<position;curposition++) curptr=curptr->next; return curptr; } int getcurposition() { return curposition; } int length() { ptrn temp; temp=head; while(1) { if(temp!=0) { count++; temp=temp->next; } else break; } return count; } int getelem(int position,int n)//获取链中第position位置的元素,其下标为n { if(position<1||position>length()+1) cout<<"越界"<<endl; else { ptrn tem; tem=getelemptr(position); n=tem->adjvex; return n; } } }; typedef struct adjlistgraphvexnode { char ch; linklist *adjlink; }*pt; class graph { private: int vexnum; bool tag; char *data1; ptrn *vextable; public: pt *vexarray; graph(int size) { vexnum=size; tag=false; data1=new char[vexnum]; vextable=new ptrn[vexnum]; vexarray=new pt[vexnum]; } void inputelem() { cout<<"请输入所有顶点的数据"<<endl; for(int i=0;i<vexnum;i++) { cin>>data1[i]; } } void inputgraph() { ptrn temp,newnode; int n,m; for(int i=0;i<vexnum;i++) { vextable[i]=new node; temp=vextable[i]; temp->data=data1[i]; temp->adjvex=i; temp->tag1=tag; while(1) { cout<<"若第"<<i<<"个顶点有邻接点请按1,否则按0"<<endl; cin>>n; if(n==1) { newnode=new node; cout<<"请输入顶点"<<i<<"邻接点的数据的下标"<<endl; cin>>m; newnode->data=data1[m]; newnode->adjvex=m; newnode->tag1=tag; temp->next=newnode; temp=temp->next; } else { temp->next=0;break; } } } } void outputgraph() { ptrn temp; for(int i=0;i<vexnum;i++) { temp=vextable[i]; while(1) { cout<<temp->adjvex<<temp->data<<" "; temp=temp->next; if(temp==0)break; } cout<<endl; } } int indexhelp(linklist *la,int v)//定位顶点v在链表中的位置 { int curpos,adjv; curpos=la->getcurposition(); adjv=la->getelem(curpos,adjv); if(adjv==v)return curpos; else { curpos=1; for(curpos=1;curpos<=la->length();curpos++) { adjv=la->getelem(curpos,adjv); if(adjv==v)break; } return curpos; } } int firstadjvex(int v)//返回顶点V的第一个邻接点 { if(v<0||v>=vexnum) {cout<<"越界"<<endl;} else { pt newnode=new adjlistgraphvexnode; vexarray[v]=newnode; vexarray[v]->adjlink=new linklist; ptrn e=vextable[v]; vexarray[v]->ch=e->data; vexarray[v]->adjlink->sethead(e); if(vexarray[v]->adjlink->head==0) {return -1;} else { int adjv; adjv=vexarray[v]->adjlink->getelem(1,adjv); return adjv; } } } int nextadjvex(int v1,int v2)//返回顶点V1的相对于V2的下一个邻接点 { if(v1<0||v1>=vexnum)cout<<"v1不合法"<<endl; if(v2<0||v2>=vexnum)cout<<"v2不合法"<<endl; if(v1==v2)cout<<"两者不能相等"<<endl; else { pt newnode=new adjlistgraphvexnode; vexarray[v1]=newnode; vexarray[v1]->adjlink=new linklist; ptrn e=vextable[v1]; vexarray[v1]->adjlink->sethead(e); if(vexarray[v1]->adjlink->head==0) { cout<<"没有以"<<v1<<"顶点为头结点的链表"<<endl; return -1; } else { int cur=indexhelp(vexarray[v1]->adjlink,v2); if(cur<vexarray[v1]->adjlink->length()) { int adjv; adjv=vexarray[v1]->adjlink->getelem(cur+1,adjv); return adjv; } else { cout<<v1<<"的邻接点没有"<<v2<<endl; return -1; } } } } }; void main() { graph g(5); g.inputelem(); g.inputgraph(); g.outputgraph(); cout<<"第2个顶点的第一个邻接点为"<<g.firstadjvex(2)<<endl; cout<<"第2个顶点相对于第一个邻接点的下一个邻接点为"<<g.nextadjvex(2,1)<<endl; }