#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
const int MaxSize = 100;
struct ArcNode
{
int adjvex;
ArcNode *next;
};
struct VertexNode
{
int vertex;
ArcNode *firstedge;
} ;
class Tree
{
private:
int vertex[MaxSize];
int arc[MaxSize][MaxSize];
int vertexNum, arcNum,index;
VertexNode root[MaxSize];
public:
Tree()
{
vertexNum = arcNum = 0;
index=0;
}
void push(int n, int e)
{
vertexNum = n;
arcNum = e;
for(int i = 0; i < n ; i++)
{
for(int j = 0; j < n ; j++)
{
arc[i][j] = 0;
}
}
int i,j;
while(index < e)
{
cin >> i >> j;
index++;
arc[i][j] = arc[j][i] = 1;
}
}
~Tree() {}
void bulid()
{
for(int i=0;i<vertexNum;i++)
{
root[i].vertex=i;
root[i].firstedge=NULL;
for(int j=0;j<vertexNum;j++)
{
if(arc[i][j]==1)
{
ArcNode *s=new ArcNode();
s->adjvex=j;
s->next=root[i].firstedge;
root[i].firstedge=s;
}
}
}
}
void pop()
{
for(int i=0;i<vertexNum;i++)
{
cout<<root[i].vertex<<":";
ArcNode *s;
s=root[i].firstedge;
while(s!=NULL)
{
int j;
j=s->adjvex;
cout<<j<<" ";
s=s->next;
}
cout<<endl;
}
}
void print()
{
for(int i = 0; i < vertexNum; i++)
{
for(int j = 0; j < vertexNum; j++)
{
cout<<arc[i][j]<<" ";
}
cout<<endl;
}
}
};
int main()
{
Tree p;
p.push(4, 4);
cout<<"邻接矩阵为:"<<endl;
p.print();
p.bulid();
cout<<"邻接表为:"<<endl;
p.pop();
}
#include <string.h>
#include <stdlib.h>
using namespace std;
const int MaxSize = 100;
struct ArcNode
{
int adjvex;
ArcNode *next;
};
struct VertexNode
{
int vertex;
ArcNode *firstedge;
} ;
class Tree
{
private:
int vertex[MaxSize];
int arc[MaxSize][MaxSize];
int vertexNum, arcNum,index;
VertexNode root[MaxSize];
public:
Tree()
{
vertexNum = arcNum = 0;
index=0;
}
void push(int n, int e)
{
vertexNum = n;
arcNum = e;
for(int i = 0; i < n ; i++)
{
for(int j = 0; j < n ; j++)
{
arc[i][j] = 0;
}
}
int i,j;
while(index < e)
{
cin >> i >> j;
index++;
arc[i][j] = arc[j][i] = 1;
}
}
~Tree() {}
void bulid()
{
for(int i=0;i<vertexNum;i++)
{
root[i].vertex=i;
root[i].firstedge=NULL;
for(int j=0;j<vertexNum;j++)
{
if(arc[i][j]==1)
{
ArcNode *s=new ArcNode();
s->adjvex=j;
s->next=root[i].firstedge;
root[i].firstedge=s;
}
}
}
}
void pop()
{
for(int i=0;i<vertexNum;i++)
{
cout<<root[i].vertex<<":";
ArcNode *s;
s=root[i].firstedge;
while(s!=NULL)
{
int j;
j=s->adjvex;
cout<<j<<" ";
s=s->next;
}
cout<<endl;
}
}
void print()
{
for(int i = 0; i < vertexNum; i++)
{
for(int j = 0; j < vertexNum; j++)
{
cout<<arc[i][j]<<" ";
}
cout<<endl;
}
}
};
int main()
{
Tree p;
p.push(4, 4);
cout<<"邻接矩阵为:"<<endl;
p.print();
p.bulid();
cout<<"邻接表为:"<<endl;
p.pop();
}