栈的先进后出的算法演示程序

上传人:M****1 文档编号:492295568 上传时间:2023-12-11 格式:DOC 页数:5 大小:19.02KB
返回 下载 相关 举报
栈的先进后出的算法演示程序_第1页
第1页 / 共5页
栈的先进后出的算法演示程序_第2页
第2页 / 共5页
栈的先进后出的算法演示程序_第3页
第3页 / 共5页
栈的先进后出的算法演示程序_第4页
第4页 / 共5页
栈的先进后出的算法演示程序_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《栈的先进后出的算法演示程序》由会员分享,可在线阅读,更多相关《栈的先进后出的算法演示程序(5页珍藏版)》请在金锄头文库上搜索。

1、栈的先进后出的算法演示程序楼主ysycrazy(风中狂)2006-03-29 15:08:25 在 Java / J2SE / 基础类 提问/*栈的先进后出的算法演示程序,请各位大侠指教。*/ public class ThreadTest public static void main(String args) /从命令行参数中获得该栈的大小 MyStack st = new MyStack(new Integer(args0).intValue(); new Thread(new Producer(st).start(); new Thread(new Customer(st).start

2、(); class MyStack /栈 char data ; private int index = 0; private boolean bFull = false; public MyStack(int size) data = new charsize; public synchronized void pushData(char c) /往栈中推数据 if (bFull) trythis.wait();catch(Exception e); dataindex=c; try Thread.sleep(300); catch(InterruptedException ie) ie.p

3、rintStackTrace(); index+; System.out.println(把字符+c+推入栈中); if (index = data.length) bFull = true; if (bFull) this.notify(); public synchronized char popData() /从栈中取数据 if (!bFull) trythis.wait();catch(Exception e)e.printStackTrace(); char c; index-; try Thread.sleep(300); catch(InterruptedException ie

4、) ie.printStackTrace(); c=dataindex; System.out.println(从栈中取出字符+c+); if (index=0) bFull = false; if (!bFull) this.notify(); return c; class Producer implements Runnable MyStack st; public Producer(MyStack st) this.st=st; public void run() while (true) st.pushData(char)(Math.random() * 26 + A); class

5、 Customer implements Runnable MyStack st; public Customer(MyStack st) this.st=st; public void run() while (true) st.popData(); JAVA堆栈问题1 悬赏分:0 - 解决时间:2006-9-12 10:10class Stack private int stck=new int10; private int tos; Stack() tos=-1; void push(int item) if(tos=9) System.out.println(Stack is full

6、); else stck+tos=item; int pop() if(tos0) System.out.println(Stack underflow); return 0; else return stcktos-; class TestStack public static void main(String args) Stack mystack1 = new Stack(); Stack mystack2 = new Stack(); for(int i=0;i10;i+)mystack1.push(i); for(int i=10;i20;i+)mystack2.push(i); S

7、ystem.out.println(出栈1); for(int i=0;i10;i+) System.out.println(mystack1.pop(); System.out.println(出栈2); for(int i=0;i10;i+) System.out.println(mystack2.pop(); 前面方法里压栈只写到9,而后面都出来10到20,为什么还可以提问者: 腾睿 - 助理 二级 最佳答案因为测试程序构造了两个栈,第一个栈压进了0-9,第二个栈压进了10-19, 然后把两个栈里的数据全部弹出。于是输出结果就是: 出栈1 9 8 7 6 5 4 3 2 1 0 出栈2 19 18 17 16 15 14 13 12 11 10

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 建筑/环境 > 建筑资料

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号