Java数据结构——栈
class Stack{ private int maxSize; //栈的长度 private long[] stackArray; //创建栈的数组的引用 private int top; //创建栈顶的引用 public Stack(int s) { //构造函数 this.maxSize = s; stackArray = new long[maxSize]; //创建对象 top = -1; //栈顶等于-1 } public void push(long j){ //入栈操作 stackArray[++top] = j; //先把top=-1自加成0,再入栈 } public long pop(){ return stackArray[top--]; //弹出当前栈顶的元素后,再自减 } public long peek(){ return stackArray[top]; //返回当前栈顶的元素 } public boolean isEmpty(){ //栈顶为-1,即栈为空 return (top == -1); } public boolean isFull(){ //栈顶为maxSize-1,即栈为满 return (top == maxSize-1); } } public class Stack_demo { public static void main(String[] args) { // TODO 自动生成的方法存根 Stack stack_demo = new Stack(10); stack_demo.push(50); stack_demo.push(60); stack_demo.push(70); stack_demo.push(80); while(!stack_demo.isEmpty()){ //当栈不为空 long value = stack_demo.pop(); System.out.print(value+"、"); } } }
class Stack_Char{ private int maxSize; //栈的长度 private char[] stackArray; //创建栈的数组的引用 private int top; //创建栈顶的引用 public Stack_Char(int s) { //构造函数 this.maxSize = s; stackArray = new char[maxSize]; //创建对象 top = -1; //栈顶等于-1 } public void push(char j){ //入栈操作 stackArray[++top] = j; //先把top=-1自加成0,再入栈 } public char pop(){ return stackArray[top--]; //弹出当前栈顶的元素后,再自减 } public char peek(){ return stackArray[top]; //返回当前栈顶的元素 } public boolean isEmpty(){ //栈顶为-1,即栈为空 return (top == -1); } public boolean isFull(){ //栈顶为maxSize-1,即栈为满 return (top == maxSize-1); } } class Reverse{ private String input; private String output; public Reverse(String input) { //构造函数 super(); this.input = input; } public String DoReverse(){ int stackSize = input.length(); Stack_Char stack_demo = new Stack_Char(stackSize); for(int i=0;i<stackSize;i++){ //把每一个字母入栈 char ch = input.charAt(i); stack_demo.push(ch); } output = ""; //给output赋值,否则会变成nullXXX while( !stack_demo.isEmpty() ){ //把每一个字母出栈 char ch = stack_demo.pop(); this.output += ch; } return this.output; } } public class Word_Reverse { public static void main(String[] args) { // TODO 自动生成的方法存根 String input,output; input = "part"; System.out.println("输入的字符串:"+input); Reverse rev = new Reverse(input); output = rev.DoReverse(); System.out.println("输出的字符串:"+output); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; class BracketsChecker{ private String input; public BracketsChecker(String input) { //构造函数 super(); this.input = input; } public void Check(){ int stackSize = input.length(); Stack_Char stack_demo = new Stack_Char(stackSize); for(int i=0;i<input.length();i++){ char ch = input.charAt(i); //遍历每一个字符 switch(ch){ case '{': case '[': case '(': stack_demo.push(ch); //遇到'{[('就入栈 break; case '}': case ']': case ')': if( !stack_demo.isEmpty()){ char chx = stack_demo.pop(); //遇到'}])'弹出堆栈 if( (chx=='{' && ch!='}') || (chx=='[' && ch!=']') || (chx=='(' && ch!=')')){ System.out.println("Error:右括号不应该是"+ch+" at "+i); } } else{ System.out.println("Error:只有右括号"+ch+" at "+i); } break; default:break; } } if( !stack_demo.isEmpty()){ //如果栈不为空的话,证明缺少右括号 System.out.println("Error:缺少右括号"); } } } public class Brackets_demo { public static void main(String[] args) throws Exception{ // TODO 自动生成的方法存根 String input; // input = "{[(]}"; System.out.print("输入字符串:"); input = getString(); System.out.println("输入的字符串为:"+input); BracketsChecker che = new BracketsChecker(input); che.Check(); } public static String getString() throws IOException{ InputStreamReader isr = new InputStreamReader(System.in); //把输入的字节流转换成字符流 BufferedReader br = new BufferedReader(isr); //只能接收字符输入流的实例 String str = br.readLine(); //一次性从缓冲区中读取内容 return str; } }
本文只发表于博客园和tonglin0325的博客,作者:tonglin0325,转载请注明原文链接:https://www.cnblogs.com/tonglin0325/p/5332264.html