class StackT {
創(chuàng)新互聯(lián)建站是一家專業(yè)提供元謀企業(yè)網(wǎng)站建設,專注與成都網(wǎng)站建設、成都做網(wǎng)站、HTML5、小程序制作等業(yè)務。10年已為元謀眾多企業(yè)、政府機構等服務。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進行中。
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代碼實現(xiàn)棧和隊列
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條測試數(shù)據(jù)數(shù)據(jù)
StackCharacter stack = null;
while(n!=0){
//從控制臺讀入一個測試字符串[]() [(])
String str = sc.next();
//如果該輸入字符串為奇數(shù),說明不匹配
if(str.length() % 2 == 1){
System.out.println("No");
}else{
//說明字符是偶數(shù)
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--;
}
}
}
代碼僅供參考,如有問題,歡迎追問!
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);
//?生產(chǎn)蘋果,放入籃子
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){//循環(huán)放入數(shù)據(jù)到隊列
num++;
basket.produce(num);
Thread.sleep(1*1000);
if(num1000000)?num?=0;
}
}?catch(?Exception?e?){
logger.error("error?:?",?e);
}
}
}
//消費線程,用來消費隊列里堆積數(shù)據(jù),此線程為阻塞狀態(tài)
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){//等待從隊列取出數(shù)據(jù)
System.out.println("取出:"?+?basket.consume());
}
}?catch?(?Exception?e?){
logger.error("error?:?",?e);
}
}
}