因需要要用道图的邻接表类,可是我之前写的是图的邻接矩阵,也没时间写一个了,哪位大哥大姐之前写过了,借我用用好不好,感激不尽(java语言的)。
在线等
求图的邻接表类(java语言写)
答案:2 悬赏:70
解决时间 2021-02-26 17:34
- 提问者网友:堕落的邪教徒
- 2021-02-26 06:24
最佳答案
- 二级知识专家网友:风格单纯
- 2021-02-26 07:06
我前几天的作业
不过是用c++写的
你改改吧
#include
using namespace std;
class list;
struct node{
friend class list;
friend class Graph;
public:
node(const int v):verice(v){
next=0;
};
int getVerice(){
return verice;}
private:
int verice;
node* next;
};
class list{
friend class Graph;
private:
node *list_node;
int count;
public:
list():count(0),list_node(NULL){}
int addNode(int num);
void delNode(node *x,node *y)
{
if(!y)list_node=list_node->next;
else y->next=x->next;
delete x;
}
void print()
{
node* temp=list_node;
while(temp!=NULL)
{
cout<verice;
temp=temp->next;
}
cout<
}
~list(){
while(list_node!=0)
{
node* temp=list_node;
list_node=list_node->next;
delete list_node;
}
}
};
int list::addNode(int num){
if(list_node==NULL)
{
list_node=new node(num);
return 0;
}
node* temp=list_node;
while(temp->next)
{
if(temp->verice==num)
return 0;
temp=temp->next;
}
temp->next=new node(num);
return 1;
}
class Graph//有向图
{
friend class list;
private:
list *head;
int n;
public:
Graph(const int verices = 0):n(verices)
{
head=new list[n];
}
void addEdge(int v1,int v2){
//if((v1<0||v1>n)&&(v2<0||v2>n))
// return;
head[v1].addNode(v2);
}
void print();
void inverse(Graph*);
int node_num()
{
return n;
}
};
void Graph::inverse(Graph* inv)
{
//.Graph inv(n);
for(int j=0;j
{
node* temp=head[j].list_node;
while(temp!=NULL)
{
inv->addEdge(temp->verice,j);
temp=temp->next;
}
}
// inver=&inv;
// return &inv;
}
void Graph::print(){
for(int vs=0;vs
{
cout<<"与"<
head[vs].print();
}
}
不过是用c++写的
你改改吧
#include
using namespace std;
class list;
struct node{
friend class list;
friend class Graph;
public:
node(const int v):verice(v){
next=0;
};
int getVerice(){
return verice;}
private:
int verice;
node* next;
};
class list{
friend class Graph;
private:
node *list_node;
int count;
public:
list():count(0),list_node(NULL){}
int addNode(int num);
void delNode(node *x,node *y)
{
if(!y)list_node=list_node->next;
else y->next=x->next;
delete x;
}
void print()
{
node* temp=list_node;
while(temp!=NULL)
{
cout<
temp=temp->next;
}
cout<
}
~list(){
while(list_node!=0)
{
node* temp=list_node;
list_node=list_node->next;
delete list_node;
}
}
};
int list::addNode(int num){
if(list_node==NULL)
{
list_node=new node(num);
return 0;
}
node* temp=list_node;
while(temp->next)
{
if(temp->verice==num)
return 0;
temp=temp->next;
}
temp->next=new node(num);
return 1;
}
class Graph//有向图
{
friend class list;
private:
list *head;
int n;
public:
Graph(const int verices = 0):n(verices)
{
head=new list[n];
}
void addEdge(int v1,int v2){
//if((v1<0||v1>n)&&(v2<0||v2>n))
// return;
head[v1].addNode(v2);
}
void print();
void inverse(Graph*);
int node_num()
{
return n;
}
};
void Graph::inverse(Graph* inv)
{
//.Graph inv(n);
for(int j=0;j
node* temp=head[j].list_node;
while(temp!=NULL)
{
inv->addEdge(temp->verice,j);
temp=temp->next;
}
}
// inver=&inv;
// return &inv;
}
void Graph::print(){
for(int vs=0;vs
cout<<"与"<
head[vs].print();
}
}
全部回答
- 1楼网友:夢想黑洞
- 2021-02-26 07:42
百度hi把答案发给你了!
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯