当前位置: 首页>>代码示例>>Java>>正文


Java Queue类代码示例

本文整理汇总了Java中sun.misc.Queue的典型用法代码示例。如果您正苦于以下问题:Java Queue类的具体用法?Java Queue怎么用?Java Queue使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


Queue类属于sun.misc包,在下文中一共展示了Queue类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: printLevelOrder

import sun.misc.Queue; //导入依赖的package包/类
public static void printLevelOrder(Node root) throws InterruptedException {
	Queue<Node> qn = new Queue<Node>();
	Node temp;
	
	if(root == null){
		return;
	}
	
	qn.enqueue(root);
	
	while(!qn.isEmpty()){
		temp = qn.dequeue();
		
		System.out.println(temp.data);
		
		if(temp.left != null)
			qn.enqueue(temp.left);
		
		if(temp.right != null)
			qn.enqueue(temp.right);
	}
	
}
 
开发者ID:tsohil,项目名称:interview_practice,代码行数:24,代码来源:PrintLevelOrder.java

示例2: printLevelOrder

import sun.misc.Queue; //导入依赖的package包/类
public static void printLevelOrder(Node root) throws InterruptedException {
	Node temp;
	Queue<Node> qn = new Queue<Node>();
	
	if(root == null){
		return;
	}
	
	qn.enqueue(root);
	
	while(!qn.isEmpty()) {
		temp = qn.dequeue();
		System.out.print(temp.data);
		
		if(temp.left != null)
			qn.enqueue(temp.left);
		
		if(temp.right != null)
			qn.enqueue(temp.right);
	}
}
 
开发者ID:tsohil,项目名称:interview_practice,代码行数:22,代码来源:MirrorOfBinaryTree.java

示例3: printLevelOrderDataInReverse

import sun.misc.Queue; //导入依赖的package包/类
public static void printLevelOrderDataInReverse(Node root) throws InterruptedException {
	Queue<Node> qn = new Queue<Node>();
	Stack<Node> sn = new Stack<Node>();
	Node temp;
	
	if(root == null) {
		return;
	}
	
	qn.enqueue(root);
	
	while(!qn.isEmpty()) {
		temp = qn.dequeue();
					
		if(temp.right != null){
			qn.enqueue(temp.right);
		}
		
		if(temp.left != null){
			qn.enqueue(temp.left);
		}
		
		sn.push(temp);
	}

	while(!sn.isEmpty()) {
		System.out.print(sn.pop().data);
	}
}
 
开发者ID:tsohil,项目名称:interview_practice,代码行数:30,代码来源:LevelOrderDataInReverse.java

示例4: numberOfHalfNodes

import sun.misc.Queue; //导入依赖的package包/类
public static int numberOfHalfNodes(Node root) throws InterruptedException {
	Queue<Node> qn = new Queue<Node>();
	Node temp;
	int count = 0;
	
	if(root == null){
		return 0;
	}
	
	qn.enqueue(root);
	
	while(!qn.isEmpty()) {
		temp = qn.dequeue();
		
		if((temp.left==null && temp.right!=null) || (temp.left!=null && temp.right==null)) {
			count++;
		}
		
		if(temp.left != null){
			qn.enqueue(temp.left);
		}
		
		if(temp.right != null) {
			qn.enqueue(temp.right);
		}
	}
	return count;
}
 
开发者ID:tsohil,项目名称:interview_practice,代码行数:29,代码来源:HalfNodesInBinaryTree.java

示例5: bfs

import sun.misc.Queue; //导入依赖的package包/类
/**
 * BFS for a vertex
 * 
 * @param g - graph to be searched
 * @param start - start vertex for the search operation in graph g
 * @param goal - goal vertex to be searched in graph g
 * @throws InterruptedException
 */
public void bfs(GraphAdjList g, Character start, Character goal) throws InterruptedException {
	Queue<Character> q = new Queue<Character>();
	// Set to keep track of visited nodes
	Set<Character> visited = new HashSet<Character>();
	// Map to hold parent-child relationship among nodes
	Map<Character, Character> parentChildMap = new HashMap<Character, Character>();
	
	Character curr;
	q.enqueue(start);
	visited.add(start);
	
	while(!q.isEmpty()) {
		curr = q.dequeue();
		if(curr==goal) {
			System.out.println("\nGoal found..."+curr);
			System.out.println("visited nodes: "+visited.toString());
			System.out.println("parent child map: "+parentChildMap.toString());
			return;
		}
		
		System.out.print(curr+"->");
		
		for(Character c : g.getEdgeList(curr)) {
			if(!visited.contains(c)) {
				visited.add(c);
				parentChildMap.put(curr, c);
				q.enqueue(c);
			}
		}
		
	}
}
 
开发者ID:tsohil,项目名称:interview_practice,代码行数:41,代码来源:BFS.java


注:本文中的sun.misc.Queue类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。