堆栈的数组实现
来源:互联网 发布:淘宝展现词怎么刷 编辑:程序博客网 时间:2024/05/16 07:24
用数组来模拟堆栈的各种运算操作
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;/** * *//** * @author Administrator *新浪微博:ouc大飞 */public class Stack { /** * @param args * @throws IOException * @throws NumberFormatException */ public static void main(String[] args) throws NumberFormatException, IOException { // TODO Auto-generated method stub BufferedReader buf; int value; StackByArray stack=new StackByArray(10); buf=new BufferedReader(new InputStreamReader(System.in)); System.out.println("输入10个数字:"); for(int i=0;i<10;i++){ value=Integer.parseInt(buf.readLine()); stack.push(value); } System.out.println("============"); while(!stack.empty()){ System.out.println("堆栈弹出的顺序为:"+stack.pop()); } }}class StackByArray{//以数组模拟堆栈的类声明 private int stack[]; //在类中声明数组 private int top;//指向堆栈顶端的索引 public StackByArray(int stack_size){ stack=new int[stack_size];//建立索引 top=-1; } //类方法push public boolean push(int data){ if(top>=stack.length){//判断堆栈顶端索引是否大于数组长度 System.out.println("堆栈已满,无法再加入"); return false; }else { stack[++top]=data;//将数据存入堆栈 return true; } } //类方法empty public boolean empty(){ if(top==-1) return true; else return false; } //类方法pop //从堆栈中取出数据 public int pop(){ if(empty()) return -1; else return stack[top--]; }}
0 0
- 堆栈的数组实现
- 堆栈的数组实现
- 静态数组实现的堆栈
- 动态数组实现的堆栈
- 4.2 堆栈的数组实现
- 基于数组的堆栈实现
- 堆栈的数组实现(完整版)
- 静态数组实现堆栈
- 用数组实现堆栈
- 动态数组实现堆栈
- 数组实现堆栈
- 堆栈(数组实现)
- 堆栈用数组实现
- 数组实现堆栈
- 两个数组实现堆栈
- 数组实现堆栈操作
- 用数组实现堆栈
- 使用数组实现堆栈
- sqlserver 命令行执行过程
- Java map结构通过value来追踪key
- C++堆的实现
- 今天安装了麒麟系统
- 【c++】c++11中生成随机数
- 堆栈的数组实现
- static方法不能直接访问类内的非static变量和不能调用this,super语句分析
- Html---表单
- PHP时间戳函数
- HTTP 状态代码表示什么意思?
- jQuery之动画基础隐藏和显示
- jquery validate插件使用
- Phaser 场景的启动与切换
- JS原型设计模式(一)