📄 a.cpp
字号:
#include<iostream.h>
#define maxnode 7
int a[maxnode]={0};
struct node
{
int mark;
node*next;
};
struct Graph
{
int n,e;
node graphvertex[maxnode];
};
Graph*creatgraph()
{
Graph*graph=new Graph;
if(graph==NULL)
{
cout<<"memory is not available"<<endl;
return NULL;
}
cout<<"please put in the node of the graph "<<endl;
cin>>graph->n;
cout<<"please put in the edge of the graph"<<endl;
cin>>graph->e;
for(int i=0;i<graph->n;i++)
{
graph->graphvertex[i].mark=i;
graph->graphvertex[i].next=NULL;
}
for(int j=0;j<graph->e;j++)
{
node*temp=new node;
if(temp==NULL)
{
cout<<"no memory available"<<endl;
return NULL;
}
temp->mark=0;
temp->next=NULL;
int a,b;
cout<<"please put in the edge from a to b "<<endl;
cin>>a>>b;
temp->mark=b;
temp->next=graph->graphvertex[a].next;
graph->graphvertex[a].next=temp;
}
return graph;
}
degree(Graph *graph)
{
node*p;
for (int i=0;i<graph->n;i++)
{
p=graph->graphvertex[i].next;
while(p)
{
a[p->mark]++;
p=p->next;
}
}
for (int j=0;j<graph->n;j++)
cout<<"the degree of" <<j<<"is"<<a[j]<<endl;
}
int b[maxnode]={0};
int count=0;
push(int i)
{
if(count<maxnode)
b[count++]=i;
}
int pop()
{
if(count>0)
return b[--count];
}
bool bobuqueue(Graph*graph)
{
for (int i=0;i<graph->n;i++)
{
if(a[i]==0)
push(i);
}
int num=0;
while(count)
{
int j=pop();
node*p=graph->graphvertex[j].next;
cout<<j<<" ";
num++;
while(p)
{
a[p->mark]--;
if(a[p->mark]==0)
push(p->mark);
p=p->next;
}
}
if(num<graph->n)
return false;
else
return true;
}
main()
{
Graph *graph;
graph=creatgraph();
degree(graph);
bool b;
b=bobuqueue(graph);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -