#include<string.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
const int MaxSize = 100;
struct ArcNode
{
int data;
ArcNode *next;
};
struct VertexNode
{
int vertex;
ArcNode *firstedge;
};
class List
{
private:
int spot, border;
int index;
VertexNode root[MaxSize];
public:
List()
{
spot = border = index = 0;
}
~List() {}
void push(int n, int e)
{
spot = n;
border = e;
for(int i = 0; i < spot; i++)
{
root[i].vertex = i;
root[i].firstedge = NULL;
}
int i, j;
while(index < border)
{
cin >> i >> j;
ArcNode *s = new ArcNode;
s->data = j;
s->next = root[i].firstedge;
root[i].firstedge = s;
ArcNode *t = new ArcNode;
t->data = i;
t->next = root[j].firstedge;
root[j].firstedge = t;
index++;
}
}
void pop()
{
for(int i = 0; i < spot; i++)
{
cout << root[i].vertex << ":";
ArcNode *s;
s = root[i].firstedge;
while(s != NULL)
{
int j;
j = s->data;
cout << j << " ";
s = s->next;
}
cout << endl;
}
}
void print()
{
int str[spot][spot]={0};
for(int i=0;i<spot;i++)
{
for(int j=0;j<spot;j++)
{
str[i][j]=0;
}
}
for(int i = 0; i < spot; i++)
{
ArcNode *s = new ArcNode;
s = root[i].firstedge;
while(s != NULL)
{
int j;
j = s->data;
str[i][j] = 1;
s = s->next;
}
}
for(int i = 0; i < spot; i++)
{
for(int j = 0; j < spot; j++)
{
cout << str[i][j] << " ";
}
cout << endl;
}
}
};
int main()
{
List p;
cout << "邻接表为:" << endl;
p.push(4, 4);
p.pop();
cout << "邻接矩阵为:" << endl;
p.print();
return 0;
}
#include<iostream>
#include<stdlib.h>
using namespace std;
const int MaxSize = 100;
struct ArcNode
{
int data;
ArcNode *next;
};
struct VertexNode
{
int vertex;
ArcNode *firstedge;
};
class List
{
private:
int spot, border;
int index;
VertexNode root[MaxSize];
public:
List()
{
spot = border = index = 0;
}
~List() {}
void push(int n, int e)
{
spot = n;
border = e;
for(int i = 0; i < spot; i++)
{
root[i].vertex = i;
root[i].firstedge = NULL;
}
int i, j;
while(index < border)
{
cin >> i >> j;
ArcNode *s = new ArcNode;
s->data = j;
s->next = root[i].firstedge;
root[i].firstedge = s;
ArcNode *t = new ArcNode;
t->data = i;
t->next = root[j].firstedge;
root[j].firstedge = t;
index++;
}
}
void pop()
{
for(int i = 0; i < spot; i++)
{
cout << root[i].vertex << ":";
ArcNode *s;
s = root[i].firstedge;
while(s != NULL)
{
int j;
j = s->data;
cout << j << " ";
s = s->next;
}
cout << endl;
}
}
void print()
{
int str[spot][spot]={0};
for(int i=0;i<spot;i++)
{
for(int j=0;j<spot;j++)
{
str[i][j]=0;
}
}
for(int i = 0; i < spot; i++)
{
ArcNode *s = new ArcNode;
s = root[i].firstedge;
while(s != NULL)
{
int j;
j = s->data;
str[i][j] = 1;
s = s->next;
}
}
for(int i = 0; i < spot; i++)
{
for(int j = 0; j < spot; j++)
{
cout << str[i][j] << " ";
}
cout << endl;
}
}
};
int main()
{
List p;
cout << "邻接表为:" << endl;
p.push(4, 4);
p.pop();
cout << "邻接矩阵为:" << endl;
p.print();
return 0;
}
转载请标明出处:邻接表转换成邻接矩阵
文章来源: 邻接表转换成邻接矩阵