Stack.java
10年積累的網(wǎng)站設(shè)計、成都做網(wǎng)站經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認識你,你也不認識我。但先網(wǎng)站策劃后付款的網(wǎng)站建設(shè)流程,更有鄆城免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
import?java.util.concurrent.locks.Condition;
import?java.util.concurrent.locks.Lock;
import?java.util.concurrent.locks.ReentrantLock;
public?class?Stack?{
private?int[]?data;
private?int?index;
private?Lock?lock;
private?Condition?moreSpace;
private?Condition?moreEelment;
public?Stack(int?size){
this.data?=?new?int[size];
this.index?=?0;
this.lock?=?new?ReentrantLock();
this.moreSpace?=?lock.newCondition();
this.moreEelment?=?lock.newCondition();
}
public?void?push(int?value){
lock.lock();
try?{
while(index?==?data.length){
moreSpace.await();
}
data[index++]?=?value;
moreEelment.signalAll();
}?catch?(InterruptedException?e)?{
e.printStackTrace();
}?finally?{
lock.unlock();
}
}
public?int?popup(){
lock.lock();
int?value?=?0;
try?{
while(index?==?0){
moreEelment.await();
}
value?=?data[--index];
moreSpace.signalAll();
}?catch?(InterruptedException?e)?{
e.printStackTrace();
}?finally?{
lock.unlock();
}
return?value;
}
}
寫入線程 WriteStack.java
import?java.util.Random;
public?class?WriteStack?implements?Runnable?{
private?Stack?stack;
public?WriteStack(Stack?stack){
this.stack?=?stack;
}
@Override
public?void?run()?{
Random?r?=?new?Random(System.currentTimeMillis());
for(int?i?=?0;i??10;?i++){
int?value?=?r.nextInt(500);
stack.push(value);
System.out.printf("Write:?push?%d?in?stack\n",value);
try?{
Thread.sleep(r.nextInt(500));
}?catch?(InterruptedException?e)?{
e.printStackTrace();
}
}
}
}
讀取線程 ReadStack.java
import?java.util.Random;
public?class?ReadStack?implements?Runnable?{
private?Stack?stack;
public?ReadStack(Stack?stack){
this.stack?=?stack;
}
@Override
public?void?run()?{
Random?r?=?new?Random(System.currentTimeMillis());
for(int?i?=?0;i??10;?i++){
int?value?=?stack.popup();
System.out.printf("Read:?popup?an?element?%d\n",value);
try?{
Thread.sleep(r.nextInt(500));
}?catch?(InterruptedException?e)?{
e.printStackTrace();
}
}
}
}
主測試線程 StackExample.java
public?class?StackExample?{
public?static?void?main(String[]?args)?throws?InterruptedException?{
Stack?stack?=?new?Stack(5);
WriteStack?writeStack?=?new?WriteStack(stack);
ReadStack?readStack?=?new?ReadStack(stack);
Thread?writeThread?=?new?Thread(writeStack);
Thread?readThread?=?new?Thread(readStack);
writeThread.start();
readThread.start();
}
}
public class Stack {
private Object[] stack;
//這個不需要;
//private int top = 0; //初始化棧頂
//這個也不需要;
//寫一個棧出來,最好是可以動態(tài)的,可以自己改變大小的,即數(shù)組的長度;
//private int size = 0; // 初始化大小
//元素個數(shù);
private int size;
//默認長度為10;
public Stack(){
this(10);
}
//也可以自己設(shè)置長度,即容量;
public Stack(int len){
stack = new Object[len];
}
//返回元素個數(shù);
public int size(){
return size;
}
//返回數(shù)組長度,即容量;
public int capacity(){
return stack.length;
}
//實現(xiàn)動態(tài)的數(shù)組;
public void ensureCapacity(){
if(size() == capacity()){
Object[] newStack = new Object[size() * 3 / 2 + 1];
System.arraycopy(stack, 0, newStack, 0, size());
stack = newStack;
}
}
//入棧;
public void push(Object o){
size++;
ensureCapacity();
stack[size - 1] = o;
}
/*
public void push(Object object) {
if (isFull()) {
System.out.println("棧滿! 入棧失敗");
}
stack[top++] = object;
}
*/
//判空;
public boolean isEmpty(){
return size == 0;
}
//出棧;
public Object pop(){
//首先要判空;
if(isEmpty()){
throw new ArrayIndexOutOfBoundsException("不能為空");
}
Object o = stack[--size];
stack[size] = null;
return o;
}
/*
// 出棧
public Object pop() {
Object object = stack[--top];
stack[top] = null;
return object;
}
*/
/*
// 計算棧當前大小
public int size() {
return top;
}
// 判斷是否是空棧
public boolean isEmpey() {
return top == 0;
}
// 判斷是否棧滿
public boolean isFull() {
return top = size;
}
public Stack(int size) {
this.size = size;
}
*/
public static void main(String[] args) {
Stack stack = new Stack(3);
String[] data = new String[] { "a", "b", "c" };
for (int i = 0; i data.length; i++) {
stack.push(data[i]);
System.out.println(data[i] + "");
}
System.out.println("***********");
while (!stack.isEmpty()) {
System.out.println(stack.pop() + "");
}
//}
}
}
你自己對比一下,我是在你的里面修改的
如何使用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--;
}
}
}
Java棧的實現(xiàn)
public
class
MyStack
{
//定義一個堆棧類
int[]
array;
//用int數(shù)組來保存數(shù)據(jù),根據(jù)需要可以換類型
int
s_size;
//定義堆棧的寬度
public
MyStack(int
i){
//定義一個帶參數(shù)構(gòu)造器
array=new
int[i];
//動態(tài)定義數(shù)組的長度
s_size=0;
//堆棧的默認寬度為0
}
public
MyStack(){
//默認構(gòu)造器
this(50);
//默認構(gòu)造器可容納50個元素
}
public
void
push(int
i){
//壓棧
array[this.s_size]=i;
this.s_size++;
}
public
int
pop(){
//從堆棧中取元素,從棧頂開始取
if(this.s_size!=0){
int
t=array[s_size-1];
//用中間變量保存棧頂?shù)脑?/p>
array[s_size-1]=0;
//取完元素該位置設(shè)為0
s_size--;
//棧的大小減1
return
t;
//返回棧頂元素
}else{
System.out.println("This
stack
is
empty");
//當棧為空時顯示提示信息,返回0
return
0;
}
}
public
boolean
isEmpty(){
//判斷棧是否為空
return
this.s_size==0;
}
public
int
top(){
//從棧頂取值,功能和
pop()
方法一樣
if(!this.isEmpty()){
int
t=array[this.s_size-1];
array[this.s_size-1]=0;
this.s_size--;
return
t;
}else{
System.out.println("This
stack
is
empty!");
return
0;
}
}
public
void
printAll(){
//打印出堆棧中的所有元素的值,不是取出,元素依然在堆棧里
if(!this.isEmpty()){
for(int
i=this.s_size
-
1;i=0;i--){
System.out.println(array[i]);
}
}
}
//下面是測試代碼
public
static
void
main(String[]
args){
MyStack
stack=new
MyStack();
stack.push(4);
stack.push(5);
stack.push(6);
stack.push(7);
//System.out.println(stack.isEmpty());
stack.printAll();
System.out.println("===========");
System.out.println(stack.top());
System.out.println(stack.top());
System.out.println(stack.top());
System.out.println(stack.top());
System.out.println(stack.top());
}
}
使用java.util包中的Stack類創(chuàng)建一個棧對象
public Object push(Object data);輸入數(shù)據(jù),實現(xiàn)壓棧
public Object pop();輸出數(shù)據(jù),實現(xiàn)彈棧
public boolean empty()判空
public Object peek();查看棧頂元素
可以去查查API嘛
我也是學(xué)java的,大家一起進步。
分享名稱:java寫一個棧的代碼 java寫一個棧的代碼怎么寫
分享URL:http://aaarwkj.com/article2/docphoc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、做網(wǎng)站、小程序開發(fā)、品牌網(wǎng)站設(shè)計、動態(tài)網(wǎng)站、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)