作者:别拿明天会好做借口 | 来源:互联网 | 2023-09-23 13:44
#include
#include
#include
using namespace std;
const int MaxSize=100;
//深度优先
class Tree
{
private:
int visited[MaxSize];
int data[MaxSize][MaxSize];
int spot,vertex;
int str[MaxSize];
int num,front,area,d;
public:
Tree(int n,int m)
{
spot=n;
vertex=m;
visited[spot]={0};
num=frOnt=area=d=0;
for(int i=0;i {
for(int j=0;j {
data[i][j]=0;
}
}
str[MaxSize]={0};
}
~Tree(){}
void build()
{
int index=-1;
int i,j;
while(++index {
cin>>i>>j;
data[i][j]=1;
data[j][i]=1;
}
}
void print(int v)//用递归的方法
{
visited[v]=1;
cout< for(int i=0;i {
if(data[v][i]==1&&visited[i]!=1)
{
print(i);
}
}
}
void push(int v)
{
if(!isfull())
{
str[front++]=v;
d=front;
num++;
}else{
cout<<"isfull"< }
}
void pop()
{
if(!isempty())
{
cout< --num;
}else{
cout<<"isempty"< }
}
bool isfull()
{
return num==MaxSize;
}
bool isempty()
{
return num==0;
}
int gettop()
{
return str[--d];
}
void DFS(int v)
{
visited[v]=1;
cout< push(v);
while(!isempty())
{
int flag=0;
v=gettop();
for(int i=0;i {
if(visited[i]!=1&&data[v][i]==1)
{
visited[i]=1;
push(i);
cout< flag=1;
break;
}
if(flag)
{
pop();
}
}
}
}
};
int main()
{
int spot,vertex;
cin>>spot>>vertex;
Tree p(spot,vertex);
p.build();
// p.print(0);
p.DFS(0);
}