中易网

求图的邻接表类(java语言写)

答案:2  悬赏:70  
解决时间 2021-02-26 17:34
因需要要用道图的邻接表类,可是我之前写的是图的邻接矩阵,也没时间写一个了,哪位大哥大姐之前写过了,借我用用好不好,感激不尽(java语言的)。

在线等
最佳答案
我前几天的作业
不过是用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();
}
}
全部回答
百度hi把答案发给你了!
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
(聊城服务区)蒋官屯派出所高速公路服务区警务
我电脑打开两个窗口是点击后面的窗口时不能全
黔南州永欣电梯工程有限责任公司怎么去啊,有
促进国有资产保值增值推动国有资本什么,有效
高铁票丢了,需要报销,高铁不给补票,证明,
原装芭比娃娃的耳环拆不下来,有这种情况吗?
娘后地址在哪,我要去那里办事
科学教子最美作文 500
东妮发艺地址好找么,我有些事要过去
遵义哪里有回族餐厅
得来品炸鸡啤酒店怎么去啊,有知道地址的么
蕉岭县梅州中国体育彩票地址是什么,有没有知
全国哪里的天气最好
富裕粮行我想知道这个在什么地方
e+h有浮球液位计吗,若没有进口品牌哪个有的
推荐资讯
洗衣液、洗衣粉、肥皂哪种洗衣服不容易褪色
小柴胡颗粒能长久吃吗
长白山在吉林省哪个市
墨尔本大学传媒硕士好吗
旅游能给人带来什么感觉?
睢宁光华小区有产权吗
康乐街/新建街南巷(路口)我想知道这个在什么
诺点奶茶店地址有知道的么?有点事想过去
乌衣港阅读答案,有事下面说。
hellowhat isyournaem是什么意思
消费者信心指数对黄金价格有什么影响
用聂组词,还有尽( )尽( )(越多越好,不
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?