日撸代码300行:第36天(邻连表)

本文介绍了使用Java实现邻接表数据结构,用于表示图,并实现了广度优先遍历算法。代码包括邻接节点类、邻接表类的构造以及广度优先遍历的方法。作者在学习过程中感到新知识掌握难度增加,计划在学习完当前阶段后进行复习巩固。
摘要由CSDN通过智能技术生成

代码来自闵老师”日撸 Java 三百行(31-40天)“,链接:https://blog.csdn.net/minfanphd/article/details/116975772

今天深度优先遍历自己没有写,最近几天感觉继续新的知识学习难度有点儿大。等学完这个小结(40天),要留两天时间好好消化一下前面学习的东西。现在理解新内容的代码越来越吃力,并且感觉之前学习的内容掌握的也不牢固,需要巩固巩固了。

package datastructure.graph;

import datastructure.queue.*;

public class AdjacencyList {
	class AdjacencyNode{
		/**
		 * The column index.
		 */
		int column;
		
		/**
		 * The next adjacent node.
		 */
		AdjacencyNode next;
		
		/**
		 * The first constructor of AdjacencyNode
		 */
		public AdjacencyNode(int paraColumn) {
			// TODO Auto-generated constructor stub
			column = paraColumn;
			next = null;
		}//The first constructor of AdjacencyNode
		
	}//of AdjacencyNode
	
	/**
	 * The number of nodes. This member variable may be redundant
	 * since it is always equal to headers.length.
	 */
	int numNodes;
	
	/**
	 * The header for each row.
	 */
	AdjacencyNode[] headers;
	
	/**
	 * *************************************************************
	 * The first constructor of AdjacencyList.
	 * @param paraMatrix   The matrix indicating the graph.
	 * *************************************************************
	 */
	public AdjacencyList(int[][] paraMatrix) {
		// TODO Auto-generated constructor stub
		numNodes = paraMatrix.length;
		
		headers = new AdjacencyNode[numNodes];
		AdjacencyNode tempNode, tempPreviousNode;
		
		for (int i = 0; i < numNodes; i++) {
			
			headers[i] = new AdjacencyNode(-1);
			tempPreviousNode = headers[i];
			
			for (int j = 0; j < numNodes; j++) {
				if (paraMatrix[i][j] == 0) {
					continue;
				}//of if
				
				// Create a new node.
				tempNode = new AdjacencyNode(j);
				
				//Link.
				tempPreviousNode.next = tempNode;
				tempPreviousNode = tempNode;
			}//of for j
		}//of for i
	}//The first constructor of AdjacencyList.
	
	/**
	 * *********************************************************************
	 * Overrides the method claimed in Object, the superclass of any class.
	 * *********************************************************************
	 */
	public String toString() {
		String resultString = "";
		
		AdjacencyNode tempNode;
		for (int i = 0; i < headers.length; i++) {
			tempNode = headers[i].next;
			
			while (tempNode !=null) {
				resultString += " (" + i + ", " + tempNode.column + ")";
				tempNode = tempNode.next;
			}//of while
			resultString += "\r\n";
		}//of for i
		return resultString;
	}//of toString
	
	/**
	 * ***********************************************************************
	 * Breadth first traversal.
	 * 
	 * @param paraStartIndex   The start index.
	 * @return   The sequence of the visit.
	 * ***********************************************************************
	 */
	public String breadthFirstTraversal(int paraStartIndex) {
		//Declare variables.
		String resultString = "";
		CircleObjectQueue tempQueue = new CircleObjectQueue();
		boolean[] tempVisitArrays = new boolean[numNodes];
		
		//Initialize the queue.
		tempVisitArrays[paraStartIndex] = true;
		resultString += (paraStartIndex + ", ");
		tempQueue.enqueue((Integer) paraStartIndex);
		
		int tempIndex;
		Integer tempInteger = (Integer) tempQueue.dequeue();
		while (tempInteger != null) {
			AdjacencyNode tempNode = headers[tempInteger].next;
			tempIndex = tempInteger.intValue();
			
			while (tempNode != null) {
				if (!tempVisitArrays[tempNode.column]) {
					tempQueue.enqueue((Integer) tempNode.column);
					resultString += (tempNode.column + ", ");
					tempVisitArrays[tempNode.column] = true;
				}//of if
				
				tempNode = tempNode.next;
			}//of while
			
			// Take out one from the head.
			tempInteger = (Integer) tempQueue.dequeue();
		}//of while
		return resultString;
	}//of breadthFirstTraversal
	
	/**
	 * *******************************************************************
	 * Unit test for breadthFirstTraversal. The same as the one in class Graph.
	 * *******************************************************************
	 */
	public static void breadthFirstTraversalTest() {
		// Test an undirected graph.
		int[][] tempMatrix = {{0,1,1,0}, {1,0,0,1}, {1,0,0,1}, {0,1,1,0}};
		Graph tempGraph = new Graph(tempMatrix);
		System.out.println(tempGraph);
		
		String tempSequence = "";
		try {
			tempSequence = tempGraph.breadthFirstTraversal(2);
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e);
		}//of try
		
		System.out.println("The breadth first order of visit: " + tempSequence);
	}//of breadthFirstTraversalTest
	
	/**
	 * *********************************************************************
	 * The entrance of program.
	 * 
	 * @param args    Not used now.
	 * *********************************************************************
	 */
	public static void main(String args[]) {
		int[][] tempMatrix = { { 0, 1, 0 }, { 1, 0, 1 }, { 0, 1, 0 } };
		AdjacencyList tempTable = new AdjacencyList(tempMatrix);
		System.out.println("The data are:\r\n" + tempTable);
		
		breadthFirstTraversalTest();
	}//of main

}//of class AdjacencyList

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值