ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-02-07 21:02:53  阅读:159  来源: 互联网

标签:column 邻连表 300 AdjacencyNode 36 tempNode next int new


代码来自闵老师”日撸 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

标签:column,邻连表,300,AdjacencyNode,36,tempNode,next,int,new
来源: https://blog.csdn.net/u010619558/article/details/122814443

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有