데이터 구조그림토폴로지 정렬

"head.h"
#include<iostream>
using namespace std;

#define MAX_VEX_NUM 20

class ArcNode//   
{
public:
	ArcNode();
	int tailnode;
	ArcNode *next;
};

ArcNode::ArcNode()
{
	next=NULL;
}

class VexNode//    
{
public:
	VexNode();
	char name;
	int indegree;
	ArcNode *firstarc;
};

VexNode::VexNode()
{
	indegree=0;
	firstarc=NULL;
}

class VexBox//    
{
public:
	VexBox();
	VexNode vexbox[MAX_VEX_NUM];
	int vexnum;
};

VexBox::VexBox()
{
	vexnum=0;
}

class TPSort//     
{
public:
	void GetTPSquence();//    
private:
	void GetVex();//      
	void GetArc();//     
	void TPSorting();//    
	VexBox v;
};

void TPSort::GetTPSquence()//    
{
	GetVex();//      
	GetArc();//     
	TPSorting();//    
}

void TPSort::GetVex()//      
{
	cout<<"Please Input The Name Of Each Vertex :"<<endl<<endl;
	char name;
	while(cin>>name)
	{
		v.vexbox[v.vexnum++].name=name;
	}
	cin.clear();
}

void TPSort::GetArc()//     
{
	cout<<"Please Input The Tail And Head Of Each Arc :"<<endl<<endl;
	int vex1,vex2;
	ArcNode *newnode,*p;
	while(cin>>vex1>>vex2)
	{
		newnode=new ArcNode;
		newnode->tailnode=vex2;
		v.vexbox[vex2].indegree++;
		if((p=v.vexbox[vex1].firstarc)==NULL)
		{
			v.vexbox[vex1].firstarc=newnode;	
		}
		else
		{
			while(p->next!=NULL)
				p=p->next;
			p->next=newnode;
		}
	}
	cin.clear();
}

void TPSort::TPSorting()//    
{
	int count=0;
	bool find=false;
	ArcNode *p;
	while(1)
	{
		for(int i=0;i<v.vexnum;i++)
		{
			if(v.vexbox[i].indegree==0)//      
			{
				v.vexbox[i].indegree--;
				cout<<v.vexbox[i].name<<endl;
				find=true;
				count++;
				p=v.vexbox[i].firstarc;
				while(p!=NULL)//         
				{
					v.vexbox[p->tailnode].indegree--;
					p=p->next;
				}
				break;
			}//if
		}//for
		if(find==false)
			break;
		else
			find=false;
	}//while
	if(count==v.vexnum)
		cout<<"This Is A Acycline Graph :"<<endl<<endl;//   
	else
		cout<<"This Is A Cycling Graph :"<<endl<<endl;//  
}//TPSorting

"main.cpp"
#include"head.h"

int main()
{
	TPSort tp;
	tp.GetTPSquence();
	system("pause");
}

좋은 웹페이지 즐겨찾기