JAVA队列类代码 java实现队列功能
怎样用java代码实现一个队列
class StackT {
创新互联建站是一家专业提供元谋企业网站建设,专注与成都网站建设、成都做网站、HTML5、小程序制作等业务。10年已为元谋众多企业、政府机构等服务。创新互联专业的建站公司优惠进行中。
private VectorT v;
public Stack(){
v = new VectorT();
}
public T pop(){
if (v.size()==0) return null;
return v.get(v.size()-1);
}
public void push(T t){
v.add(t);
}
public boolean isEmpty(){
return v.size()==0;
}
}
class QueueT{
private VectorT v;
public Queue(){
v = new VectorT();
}
//入队列
public void enqueue(T t){
v.add(t);
}
//出队列
public T dequeue(){
if (v.size()==0) return null;
return v.get(0);
}
public boolean isEmpty(){
return v.size() == 0;
}
}
如何使用java代码实现栈和队列
如何使用java代码实现栈和队列
import java.util.Scanner;
import java.util.Stack;
/**
* @author Owner
*
*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n= sc.nextInt();//3条测试数据数据
StackCharacter stack = null;
while(n!=0){
//从控制台读入一个测试字符串[]() [(])
String str = sc.next();
//如果该输入字符串为奇数,说明不匹配
if(str.length() % 2 == 1){
System.out.println("No");
}else{
//说明字符是偶数
stack = new StackCharacter();
//遍历第一条测试字符串[]() [(])
for(int i=0;istr.length();i++){
if(stack.isEmpty()){
//如果栈是空的
stack.push(str.charAt(i));
}else if(stack.peek() == '[' str.charAt(i) == ']' || stack.peek() == '(' str.charAt(i) == ')'){
//说明此时栈中字符不是空的,并且符合,
stack.pop();
}else{
stack.push(str.charAt(i));
}
}
if(stack.isEmpty()){
//如果栈是空的,说明a href=";tn=44039180_cprfenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3mhDvP10Ln19hrANbP1fk0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3ErHnLP1nsPHDznHTkn1fYPHnz" target="_blank" class="baidu-highlight"括号/a匹配
System.out.println("Yes");
}else{
//说明栈不为空,a href=";tn=44039180_cprfenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3mhDvP10Ln19hrANbP1fk0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3ErHnLP1nsPHDznHTkn1fYPHnz" target="_blank" class="baidu-highlight"括号/a不匹配
System.out.println("No");
}
}
n--;
}
}
}
用java语言设计一个先进先先出的队列类
代码仅供参考,如有问题,欢迎追问!
Basket:
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
/**
* 定义装苹果的篮子
*
* @author stone
* @version 2013-05-21
*/
public class Basket {
// 篮子,能够容纳Integer.MAX_VALUE个苹果
BlockingQueueObject basket = new LinkedBlockingQueueObject(Integer.MAX_VALUE);
// 生产苹果,放入篮子
public void produce(Object obj) throws InterruptedException {
// put方法放入一个苹果,若basket满了,等到basket有位置
basket.put(obj);
}
// 消费苹果,从篮子中取走
public Object consume() throws InterruptedException {
// take方法取出一个苹果,若basket为空,等到basket有苹果为止(获取并移除此队列的头部)
return basket.take();
}
}
BaksetTest:
import org.apache.log4j.Logger;
public class BaksetTest {
static Logger logger = Logger.getLogger(BaksetTest.class);
static Basket basket = new Basket();
public static void main(String[] args) throws Exception {
try{
Consume con = new Consume(basket);
Thread thread = new Thread(con);
thread.start();
Integer num = 0;
while(true){//循环放入数据到队列
num++;
basket.produce(num);
Thread.sleep(1*1000);
if(num1000000) num =0;
}
} catch( Exception e ){
logger.error("error : ", e);
}
}
}
//消费线程,用来消费队列里堆积数据,此线程为阻塞状态
class Consume implements Runnable {
Logger logger = Logger.getLogger(BaksetTest.class);
Basket basket;
public Consume (Basket basket){
this.basket = basket;
}
@Override
public void run() {
try{
while(true){//等待从队列取出数据
System.out.println("取出:" + basket.consume());
}
} catch ( Exception e ){
logger.error("error : ", e);
}
}
}
当前名称:JAVA队列类代码 java实现队列功能
文章起源:http://hbruida.cn/article/dohcchc.html