真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

java鄰接表代碼實(shí)現(xiàn),java圖的鄰接表

請(qǐng)編寫(xiě)一個(gè)完整的程序,建立有向圖的鄰接表存儲(chǔ)結(jié)構(gòu),要求:

給你一個(gè)鄰接表的完整程序:

目前創(chuàng)新互聯(lián)已為近1000家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)絡(luò)空間、網(wǎng)站托管運(yùn)營(yíng)、企業(yè)網(wǎng)站設(shè)計(jì)、天水網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。

#include iostream.h

struct node

{

int data;

node *next;

};

class list

{

public:

list(){head=NULL;};

void MakeEmpty();

int Length();

void Insert(int x,int i);//將x插入到第i個(gè)結(jié)點(diǎn)(不含頭結(jié)點(diǎn))的之后

void Insertlist(int a,int b);//將節(jié)點(diǎn)b插入a之前

int Delete(int x);

int Remove(int i);

int Find(int x);

void Display();

private:

node *head;

};

void list::Display()

{

node *current=head;

while (current!=NULL)

{

coutcurrent-data" ";

current=current-next;

}

coutendl;

}

void list::MakeEmpty()

{

head=NULL;

}

int list::Length()

{int n=1;

node *q=head;

if(q==NULL)

n=1;

else

while(q!=NULL)

{

n++;

q=q-next;

}

return n;

}

int list::Find(int x)//在鏈表中查找數(shù)值為x的結(jié)點(diǎn),成功返回1,否則返回0

{

node *p=head;

while(p!=NULLp-data!=x)

p=p-next;

if(p-data==x)

return 1;

else

return 0;

}

void list::Insert (int x,int i)//將x插入到第i個(gè)結(jié)點(diǎn)(不含頭結(jié)點(diǎn))的之后;

{

node *p;//p中放第i個(gè)結(jié)點(diǎn)

node *q;//q中放i后的結(jié)點(diǎn)

node *h;//h中存要插入的結(jié)點(diǎn)

h=new node;

h-data =x;

p=head;

if(p-next !=NULL) //鏈表不是只有一個(gè)結(jié)點(diǎn)或者空鏈表時(shí)候

{

int n=1;

while(p-next !=NULL)

{

n++;

p=p-next ;

}// 得到鏈表的結(jié)點(diǎn)的個(gè)數(shù)

p=head;//使p重新等于鏈?zhǔn)?/p>

if(i==n)//i=n時(shí),直接加在最后面就行了

{

while(p-next !=NULL)

p=p-next;

p-next=h;

h-next =NULL;

}

else if(ini1)//先找到第i個(gè)結(jié)點(diǎn),用p存第i個(gè)結(jié)點(diǎn),用q存i后的結(jié)點(diǎn),用h存要插入的結(jié)點(diǎn)

{

for(int j=1;ji;j++)

p=p-next;//找到第i個(gè)結(jié)點(diǎn),用p存第i個(gè)結(jié)點(diǎn)

q=p-next;//q存i后的結(jié)點(diǎn)

p-next=h;

h-next=q;

}

else

cout"超出鏈表結(jié)點(diǎn)個(gè)數(shù)的范圍"endl;

}

else

cout"這個(gè)鏈表是空鏈表或者結(jié)點(diǎn)位置在首位"endl;

}

void list::Insertlist(int a,int b)//將b插入到結(jié)點(diǎn)為a之前

{

node *p,*q,*s;//p所指向的結(jié)點(diǎn)為a,s所指為要插入的數(shù)b,q所指向的是a前的結(jié)點(diǎn)

s=new node;

s-data=b;

p=head;

if(head==NULL)//空鏈表的時(shí)候

{

head=s;

s-next=NULL;

}

else

if(p-data==a)//a在鏈?zhǔn)讜r(shí)候

{

s-next=p;

head=s;

}

else

{

while(p-data!=ap-next!=NULL)//使p指向結(jié)點(diǎn)a,q指向a之前的結(jié)點(diǎn)

{

q=p;

p=p-next;

}

if(p-data==a)//若有結(jié)點(diǎn)a時(shí)候

{

q-next=s;

s-next=p;

}

else//沒(méi)有a的時(shí)候

{

p-next=s;

s-next=NULL;

}

}

}

int list::Delete(int x)//刪除鏈表中值為x的結(jié)點(diǎn),成功返回1,否則返回0;

{

node *p,*q;

p=head;

if(p==NULL)

return 0;

if(p-data==x)

{

head=p-next;

delete p;

return 1;

}

else

{

while(p-data!=xp-next!=NULL)

{ q=p;

p=p-next;

}

if(p-data==x)

{

q-next =p-next;

delete p;

return 1;

}

else

return 0;

}

}

int list::Remove(int i)

{

node *p,*q;

p=head;

if(p!=NULL)

{ int n=1;

while(p-next !=NULL)

{

n++;

p=p-next ;

}//得到鏈表結(jié)點(diǎn)的個(gè)數(shù)

p=head;

if(i==n)//i結(jié)點(diǎn)在結(jié)尾的時(shí)候

{

while(p-next!=NULL)

{

q=p;

p=p-next;

}

q-next=NULL;

delete p;

return 1;

}

else if(ini1)//i結(jié)點(diǎn)在中間的時(shí)候

{

for(int j=1;ji;j++)

{

q=p;//q中放i前的結(jié)點(diǎn)

p=p-next ;//p中放第i個(gè)結(jié)點(diǎn)

}

q-next=p-next;

delete p;

return 1;

}

else if(i==1)//i結(jié)點(diǎn)在首位的時(shí)候

{

q=p-next;

head=q;

delete p;

return 1;

}

else

return 0;

}

else

return 0;

}

void main()

{

list A;

int data[10]={1,2,3,4,5,6,7,8,9,10};

A.Insertlist(0,data[0]);

for(int i=1;i10;i++)

A.Insertlist(0,data[i]);

A.Display();

menu:cout"1.遍歷鏈表"'\t'"2.查找鏈表"'\t'"3.插入鏈表"endl;

cout"4.刪除鏈表"'\t'"5.鏈表長(zhǎng)度"'\t'"6.置空鏈表"endl;

int m;

do

{

cout"請(qǐng)輸入你想要進(jìn)行的操作(選擇對(duì)應(yīng)操作前面的序號(hào)):"endl;

cinm;

}while(m1||m6);//當(dāng)輸入的序號(hào)不在包括中,讓他重新輸入

switch(m)

{

case 1:

{

A.Display ();

goto menu;

};break;

case 2:

{

cout"請(qǐng)輸入你想要找到的結(jié)點(diǎn):"endl;

int c;

cinc;//輸入你想要找到的結(jié)點(diǎn)

if(A.Find (c)==1)

{

cout"可以找到"cendl;

A.Display ();//重新顯示出鏈表A

}

else

{

cout"鏈表中不存在"cendl;

A.Display ();//重新顯示出鏈表A

}

goto menu;

};break;

case 3:

{

cout"請(qǐng)選擇你要插入的方式(選擇前面的序號(hào)進(jìn)行選擇)"endl;

cout"1.將特定的結(jié)點(diǎn)加入到特定的結(jié)點(diǎn)前"'\t'"2.將特定的結(jié)點(diǎn)加到特定的位置后"endl;

int b1;

do

{

cout"請(qǐng)輸入你想要插入的方式(選擇前面的序號(hào)進(jìn)行選擇):"endl;

cinb1;

}while(b11||b12);//當(dāng)輸入的序號(hào)不在包括中,讓他重新輸入

if(b1==1)

{

cout"請(qǐng)輸入你想要插入的數(shù)和想要插入的結(jié)點(diǎn)(為此結(jié)點(diǎn)之前插入):"endl;

int a1,a2;

cina1a2;

A.Insertlist (a1,a2);//將a1插入到結(jié)點(diǎn)為a2結(jié)點(diǎn)之前

cout"此時(shí)鏈表為:"endl;

A.Display ();//重新顯示出鏈表A

}

else

{

cout"請(qǐng)輸入你想要插入的數(shù)和想要插入的位置(為此結(jié)點(diǎn)之后插入):"endl;

int a1,a2;

cina1a2;

A.Insert (a1,a2);//將a1插入到結(jié)點(diǎn)位置為a2的結(jié)點(diǎn)之后

cout"此時(shí)鏈表為:"endl;

A.Display ();//重新顯示出鏈表A

}

goto menu;

};break;

case 4:

{

cout"請(qǐng)選擇你要?jiǎng)h除的方式(選擇前面的序號(hào)進(jìn)行選擇)"endl;

cout"1.刪除特定的結(jié)點(diǎn)"'\t'"2.刪除特定位置的結(jié)點(diǎn)"endl;

int b1;

do

{

cout"請(qǐng)輸入你想要插入的方式(選擇前面的序號(hào)進(jìn)行選擇):"endl;

cinb1;

}while(b11||b12);//當(dāng)輸入的序號(hào)不在包括中,讓他重新輸入

if(b1==1)

{

cout"請(qǐng)輸入你想要?jiǎng)h除的結(jié)點(diǎn):"endl;

int a;

cina;//輸入你想要?jiǎng)h除的結(jié)點(diǎn)

if(A.Delete (a)==1)

{

cout"成功刪除"aendl;

cout"刪除后的鏈表為:"endl;

A.Display ();

}

else

{

cout"此鏈表為:"endl;

A.Display ();//重新顯示出鏈表A

cout"鏈表中不存在"aendl;

}

}

else

{

cout"請(qǐng)輸入你想要?jiǎng)h除的結(jié)點(diǎn)位置:"endl;

int b;

cinb;//輸入你想要?jiǎng)h除的結(jié)點(diǎn)的位置

if(A.Remove(b)==1)

{

cout"成功刪除第"b"個(gè)結(jié)點(diǎn)"endl;

cout"刪除后的鏈表為:"endl;

A.Display ();//重新顯示出鏈表A

}

else

{

cout"當(dāng)前鏈表的結(jié)點(diǎn)個(gè)數(shù)為:"A.Length ()endl;

cout"您輸入的結(jié)點(diǎn)位置越界"endl;

}

}

goto menu;

};break;

case 5:

{

cout"這個(gè)鏈表的結(jié)點(diǎn)數(shù)為:"A.Length ()endl;

goto menu;

};break;

case 6:

{

A.MakeEmpty ();

cout"這個(gè)鏈表已經(jīng)被置空"endl;

goto menu;

};break;

}

}

評(píng)論(3)|1

sunnyfulin |六級(jí)采納率46%

擅長(zhǎng):C/C++JAVA相關(guān)Windows數(shù)據(jù)結(jié)構(gòu)及算法百度其它產(chǎn)品

按默認(rèn)排序|按時(shí)間排序

其他1條回答

2012-04-23 17:41121446881|六級(jí)

我寫(xiě)了一個(gè)C語(yǔ)言的,只給你兩個(gè)結(jié)構(gòu)體和一個(gè)初始化函數(shù):

#include "stdio.h"

#include "malloc.h"

struct adjacentnext//鄰接表項(xiàng)結(jié)構(gòu)體

{

int element;

int quanvalue;

struct adjacentnext *next;

};

struct adjacenthead//鄰接表頭結(jié)構(gòu)體

{

char flag;

int curvalue;

int element;

struct adjacenthead *previous;

struct adjacentnext *son;

};

//初始化圖,用鄰接表實(shí)現(xiàn)

struct adjacenthead *mapinitialnize(int mapsize)

{

struct adjacenthead *ahlists=NULL;

struct adjacentnext *newnode=NULL;

int i;

int x,y,z;

ahlists=malloc(sizeof(struct adjacenthead)*mapsize);

if(ahlists==NULL)

return NULL;

for(i=0;imapsize;i++)

{

ahlists[i].curvalue=0;

ahlists[i].flag=0;

ahlists[i].previous=NULL;

ahlists[i].son=NULL;

ahlists[i].element=i+1;

}

scanf("%d%d%d",x,y,z);//輸入源結(jié)點(diǎn),目的結(jié)點(diǎn),以及源結(jié)點(diǎn)到目的結(jié)點(diǎn)的路權(quán)值

while(x!=0y!=0)//x,y至少有一個(gè)零就結(jié)束

{

newnode=malloc(sizeof(struct adjacentnext));

newnode-element=y;

newnode-quanvalue=z;

newnode-next=ahlists[x-1].son;

ahlists[x-1].son=newnode;

scanf("%d%d%d",x,y,z);

}

return ahlists;//返回鄰接表頭

}

新手求助,這是我找的Java鄰接表的代碼,第三行ArcNode next怎么理解,Java中不是沒(méi)有指針嗎??????

理解為一個(gè)ArcNode型的變量。其實(shí)java中任何一個(gè)非基本數(shù)據(jù)類(lèi)型的變量名就相當(dāng)于一個(gè)指針,只是一般不這么說(shuō)罷了。

求代碼,java實(shí)驗(yàn),題目如圖

import?java.util.Scanner;

import?java.util.Stack;

public?class?DFS

{

//?存儲(chǔ)節(jié)點(diǎn)信息

private?char[]?vertices;

//?存儲(chǔ)邊信息(鄰接矩陣)

private?int[][]?arcs;

//?圖的節(jié)點(diǎn)數(shù)

private?int?vexnum;

//?記錄節(jié)點(diǎn)是否已被遍歷

private?boolean[]?visited;

//?初始化

public?DFS(int?n)

{

vexnum?=?n;

vertices?=?new?char[n];

arcs?=?new?int[n][n];

visited?=?new?boolean[n];

for(int?i?=?0;?i??vexnum;?i++)

{

for(int?j?=?0;?j??vexnum;?j++)

{

arcs[i][j]?=?0;

}

}

}

//?添加邊(無(wú)向圖)

public?void?addEdge(int?i,?int?j)

{

//?邊的頭尾不能為同一節(jié)點(diǎn)

if(i?==?j)

return;

arcs[i?-?1][j?-?1]?=?1;

arcs[j?-?1][i?-?1]?=?1;

}

//?設(shè)置節(jié)點(diǎn)集

public?void?setVertices(char[]?vertices)

{

this.vertices?=?vertices;

}

//?設(shè)置節(jié)點(diǎn)訪問(wèn)標(biāo)記

public?void?setVisited(boolean[]?visited)

{

this.visited?=?visited;

}

//?打印遍歷節(jié)點(diǎn)

public?void?visit(int?i)

{

System.out.print(vertices[i]?+?"?");

}

//?從第i個(gè)節(jié)點(diǎn)開(kāi)始深度優(yōu)先遍歷

private?void?traverse(int?i)

{

//?標(biāo)記第i個(gè)節(jié)點(diǎn)已遍歷

visited[i]?=?true;

//?打印當(dāng)前遍歷的節(jié)點(diǎn)

visit(i);

//?遍歷鄰接矩陣中第i個(gè)節(jié)點(diǎn)的直接聯(lián)通關(guān)系

for(int?j?=?0;?j??vexnum;?j++)

{

//?目標(biāo)節(jié)點(diǎn)與當(dāng)前節(jié)點(diǎn)直接聯(lián)通,并且該節(jié)點(diǎn)還沒(méi)有被訪問(wèn),遞歸

if(arcs[i][j]?==?1??visited[j]?==?false)

{

traverse(j);

}

}

}

//?圖的深度優(yōu)先遍歷(遞歸)

public?void?DFSTraverse(int?start)

{

//?初始化節(jié)點(diǎn)遍歷標(biāo)記

for(int?i?=?0;?i??vexnum;?i++)

{

visited[i]?=?false;

}

//?從沒(méi)有被遍歷的節(jié)點(diǎn)開(kāi)始深度遍歷

for(int?i?=?start?-?1;?i??vexnum;?i++)

{

if(visited[i]?==?false)

{

//?若是連通圖,只會(huì)執(zhí)行一次

traverse(i);

}

}

}

//?圖的深度優(yōu)先遍歷(非遞歸)

public?void?DFSTraverse2(int?start)

{

//?初始化節(jié)點(diǎn)遍歷標(biāo)記

for(int?i?=?0;?i??vexnum;?i++)

{

visited[i]?=?false;

}

StackInteger?s?=?new?StackInteger();

for(int?i?=?start?-?1;?i??vexnum;?i++)

{

if(!visited[i])

{

//?連通子圖起始節(jié)點(diǎn)

s.add(i);

do

{

//?出棧

int?curr?=?s.pop();

//?如果該節(jié)點(diǎn)還沒(méi)有被遍歷,則遍歷該節(jié)點(diǎn)并將子節(jié)點(diǎn)入棧

if(visited[curr]?==?false)

{

//?遍歷并打印

visit(curr);

visited[curr]?=?true;

//?沒(méi)遍歷的子節(jié)點(diǎn)入棧

for(int?j?=?vexnum?-?1;?j?=?0;?j--)

{

if(arcs[curr][j]?==?1??visited[j]?==?false)

{

s.add(j);

}

}

}

}?while(!s.isEmpty());

}

}

}

public?static?void?main(String[]?args)

{

Scanner?sc?=?new?Scanner(System.in);

int?N,?M,?S;

while(true)

{

System.out.println("輸入N?M?S,分別表示圖G的結(jié)點(diǎn)數(shù),邊數(shù),搜索的起點(diǎn):");

String?line?=?sc.nextLine();

if(!line.matches("^\\s*([1-9]\\d?|100)(\\s+([1-9]\\d?|100)){2}\\s*$"))

{

System.out.print("輸入錯(cuò)誤,");

continue;

}

String[]?arr?=?line.trim().split("\\s+");

N?=?Integer.parseInt(arr[0]);

M?=?Integer.parseInt(arr[1]);

S?=?Integer.parseInt(arr[2]);

break;

}

DFS?g?=?new?DFS(N);

char[]?vertices?=?new?char[N];

for(int?i?=?0;?i??N;?i++)

{

vertices[i]?=?(i?+?1?+?"").charAt(0);

}

g.setVertices(vertices);

for(int?m?=?0;?m??M;?m++)

{

System.out.println("輸入圖G的第"?+?(m?+?1)?+?"條邊,格式為“i?j”,其中i,j為結(jié)點(diǎn)編號(hào)(范圍是1~N)");

String?line?=?sc.nextLine();

if(!line.matches("^\\s*([1-9]\\d?|100)\\s+([1-9]\\d?|100)\\s*$"))

{

System.out.print("輸入錯(cuò)誤,");

m--;

continue;

}

String[]?arr?=?line.trim().split("\\s+");

int?i?=?Integer.parseInt(arr[0]);

int?j?=?Integer.parseInt(arr[1]);

g.addEdge(i,?j);

}

sc.close();

System.out.print("深度優(yōu)先遍歷(遞歸):");

g.DFSTraverse(S);

System.out.println();

System.out.print("深度優(yōu)先遍歷(非遞歸):");

g.DFSTraverse2(S);

}

}

java中如何把圖用鄰接表表示出來(lái)

package my.graph;

import java.util.ArrayList;

import java.util.Iterator;

import my.queue.*;

import my.stack.StackX;

/**

* 鄰接表表示

* @author xiayi

*

*/

public class Graph {

private int MAX_VERTS = 20;

private Vertex vertexList[];

private boolean is = false;//是否為有向圖

private int nVerts = 0;

private StackX stackX;

private Vertex dfs[];

private Vertex bfs[];

private Queue queue;

public Graph(){

vertexList = new Vertex[MAX_VERTS];

dfs = new Vertex[MAX_VERTS];

bfs = new Vertex[MAX_VERTS];

}

public Graph(int n){

vertexList = new Vertex[n];

dfs = new Vertex[n];

bfs = new Vertex[n];

}

public Graph(int n, boolean is){

this.is = is;

vertexList = new Vertex[n];

dfs = new Vertex[n];

bfs = new Vertex[n];

}

//////////////////////////////////////////////

public boolean isIs() {

return is;

}

public void setIs(boolean is) {

this.is = is;

}

public Vertex[] getVertexList() {

return vertexList;

}

public Vertex[] getDfs() {

return dfs;

}

public Vertex[] getBfs() {

return bfs;

}

////////////////////////////////////////////////////

/**

* 添加頂點(diǎn)

*/

public void addVertex(Vertex vertex){

vertex.setIndex(nVerts);

vertexList[nVerts] = vertex;

nVerts++;

}

/**

* 添加邊

*/

public void addEdge(int start, int end){

vertexList.addAdj(vertexList);

if (!is) {vertexList.addAdj(vertexList);}

}

/**

* 返回節(jié)點(diǎn)個(gè)數(shù)

* @return

*/

public int getVertsCount(){

return vertexList.length;

}

/**

* 深度優(yōu)先迭代器

* @return

*/

public Iterator dfsIterator(){

dfs();

return new DfsIterator();

}

/**

* 廣度優(yōu)先迭代器

* @return

*/

public Iterator bfsIterator(){

bfs();

return new BfsIterator();

}

////////////////////////////////////////////////////////

public void displayGraph(){

ArrayListVertex next = null;

for (int i = 0; i vertexList.length; i++) {

printVertx(vertexList[i]);

}

}

public void printVertx(Vertex vertex){

ArrayListVertex next = vertex.getAdj();

if(next == null){ System.out.println(vertex.toString()+" 無(wú)連接點(diǎn)");}

else{

System.out.print(vertex.toString()+"有鄰接點(diǎn):");

for (int i = 0; i next.size(); i++) {

System.out.print("頂點(diǎn)"+next.get(i).label+", ");

}

System.out.println();

}

}

///////////////////////////////////////////////////////////

public void dfs(){

stackX = new StackX(MAX_VERTS);

vertexList[0].isVisted = true;

dfs[0] = vertexList[0];

stackX.push(vertexList[0]);

int dfsIndex = 0;

Vertex vertex;

while(!stackX.isEmpty()){

vertex = getAdjVertex((Vertex)stackX.peek());

if(vertex == null){

stackX.pop();

}else{

vertex.isVisted = true;

dfs[++dfsIndex]=vertex;

stackX.push(vertex);

}

}

for (int i = 0; i getVertsCount(); i++) {

vertexList[i].isVisted = false;

}

}

public void bfs() {

queue = new Queue(MAX_VERTS);

vertexList[0].isVisted = true;

bfs[0] = vertexList[0];

queue.insert(vertexList[0]);

int bfsIndex = 0;

Vertex vertex;

while(!queue.isEmpty()){

Vertex vertex2 = (Vertex)queue.remove();

while((vertex = getAdjVertex(vertex2))!=null){

vertex.isVisted = true;

bfs[++bfsIndex] = vertex;

queue.insert(vertex);

}

}

for (int i = 0; i getVertsCount(); i++) {

vertexList[i].isVisted = false;

}

}

/**

* 得到一個(gè)鄰接點(diǎn)

* @param vertex

* @return

*/

public Vertex getAdjVertex(Vertex vertex){

ArrayListVertex adjVertexs = vertex.getAdj();

for (int i = 0; i adjVertexs.size(); i++) {

if(!adjVertexs.get(i).isVisted){

return adjVertexs.get(i);

}

}

return null;

}

/////////////////////////////////////////////////////////////

private abstract class GraphIterator implements Iterator{

int count = 0;

public GraphIterator(){

}

public boolean hasNext() {

return count != getVertsCount()-1;

}

public Object next() {

// TODO Auto-generated method stub

return null;

}

public void remove() {

// TODO Auto-generated method stub

}

}

//深度優(yōu)先迭代

private class DfsIterator extends GraphIterator{

public DfsIterator(){

super();

}

public Vertex next() {

return dfs[count++];

}

}

//廣度優(yōu)先迭代

private class BfsIterator extends GraphIterator{

public BfsIterator(){

super();

}

public Object next() {

return bfs[count++];

}

}

/////////////////////////////////////////////////////////

public static void main(String[] args) {

int nVerts = 10;

int c = 'A'-1;

Vertex vertex;

Graph myGraph = new Graph(nVerts, false);

for (int i = 0; i nVerts; i++) {

c++;

vertex = new Vertex((char)(c));

myGraph.addVertex(vertex);

}

myGraph.addEdge(0, 1);

myGraph.addEdge(0, 4);

myGraph.addEdge(1, 2);

myGraph.addEdge(2, 3);

myGraph.addEdge(4, 5);

myGraph.addEdge(4, 6);

myGraph.addEdge(5, 8);

myGraph.addEdge(6, 7);

myGraph.addEdge(7, 8);

myGraph.addEdge(8, 9);

System.out.println("深度優(yōu)先迭代遍歷:");

for (Iterator iterator = myGraph.dfsIterator(); iterator.hasNext();) {

vertex = (Vertex) iterator.next();

System.out.println(vertex.toString());

}

System.out.println("/n廣度優(yōu)先迭代遍歷:");

for (Iterator iterator = myGraph.bfsIterator(); iterator.hasNext();) {

vertex = (Vertex) iterator.next();

System.out.println(vertex.toString());

}

}

}

class Vertex{

public char label;

public boolean isVisted;

public int index;

private ArrayListVertex next = null;

public Vertex(char lab) // constructor

{

label = lab;

isVisted = false;

}

//為節(jié)點(diǎn)添加鄰接點(diǎn)

public void addAdj(Vertex ver){

if(next == null) next = new ArrayListVertex();

next.add(ver);

}

public ArrayListVertex getAdj(){

return next;

}

public void setIndex(int index){

this.index = index;

}

public String toString(){

return "頂點(diǎn) "+label+",下標(biāo):"+index+".";

}

}

代碼來(lái)自:


網(wǎng)頁(yè)標(biāo)題:java鄰接表代碼實(shí)現(xiàn),java圖的鄰接表
本文URL:http://weahome.cn/article/dssscij.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部