java栈and栈实例(附代码、不是很懂)

栈和队列_栈

数组 链表 树 适用于数据库应用中作数据记录
int[] a =new int[10];
a[1]=100;
int b=a[1];

栈和队列 1.通常情况作为程序员的工具来运用

        2.受限访问
        3.更加抽象(主要通过接口进行定义)

栈就是一组记录,表现形式为先进后出

表示栈的代码

public class StackX{
private long[] stackArray;
private int maxSize;
private int top;//指向最顶层的位置
public StackX(int s){
maxSize=s;
stackArray=new long[maxSize];
top=-1;//因为第一个放进去的数据应该放在数组的0里面,初始没有的话 就给-1
}
//添加数据,永远往上添加
public void push(long j){
stackArray[++top]=j;
}
//查看并删除
public long pop(){
return stackArray[top--];
}
//查看
public long peek(){
return stackArray[top];
}
public boolean isEmpty(){
return top==-1;//如果top==-1则是空的,因为我们初始设置的
}
public boolean isFull(){
return top==(maxSize-1);
}
}

Public class StackApp{
Public static void main(String[]args){
StackX theStack=new StackX(10);
theStack.push(20);
theStack.push(40);
theStack.push(60);
theStack.push(80);
while(theStack.isEmpty()){
long value=theStack.pop();
System.out.print(value+””);
}
System.out.println();
System.ut.println(“theStack.isEmpty=”+theStack.isEmpty());
}
}

Java实例1:单词逆序

public class StackX {
private int maxSize;
private char[] stackArray;//栈数组
private int top;//指针
public StackX(int max) {
maxSize=max;
stackArray=new char[maxSize];
top=-1;//top开始的时候位置是0 所以=-1
}
public void push(char j) {
stackArray[++top]=j;//先让top递增一个再赋值
}
public char pop() {
return stackArray[top--];//弹出
}
public char peek() {
return stackArray[top];//查找
}
public boolean isEmpty() {
//栈是否为空
return top==-1;
}
}

public class Reverser {
private String input;//通过构造函数传过来的input
private String output;
public Reverser(String in) {
input=in;
}
public String doRev() {//反转
int stackSize=input.length();
StackX theStack=new StackX(stackSize);//创建一个栈
for(int j=0;j char ch=input.charAt(j);//拿出来字符串中指定的字符
theStack.push(ch);
}
output="";
while(!theStack.isEmpty()) {
char c=theStack.pop();
output=output+c;

    }
    return output;
}

}

import java.io.*;
public class ReverseApp {
public static void main(String[]args ) throws IOException{//抛出异常
String input,output;
while(true) {
System.out.println("Enter a String:");
System.out.flush();//刷新一下
input=getString();
if(input.equals(""))break;
Reverser theReverser=new Reverser(input);
output=theReverser.doRev();
System.out.println("Reversed:"+output);
StringBuilder sb=new StringBuilder("中华人民共和国");
System.out.println("StringBuilder reverse:"+sb.reverse());
}
}
public static String getString() throws IOException{
InputStreamReader isr=new InputStreamReader(System.in);//输入流的读取器
BufferedReader br=new BufferedReader(isr);//带缓存的读取器
String s=br.readLine();
return s;
}
}

Java实例2:分隔符匹配

public class StackX {
private int maxSize;
private char[] stackArray;//栈数组
private int top;//指针
public StackX(int max) {
maxSize=max;
stackArray=new char[maxSize];
top=-1;//top开始的时候位置是0 所以=-1
}
public void push(char j) {
stackArray[++top]=j;//先让top递增一个再赋值
}
public char pop() {
return stackArray[top--];//弹出
}
public char peek() {
return stackArray[top];//查找
}
public boolean isEmpty() {
//栈是否为空
return top==-1;
}
}

public class BracketChecker {
private String input;
public BracketChecker(String in) {
input=in;
}
//写一个检查的方法
public void check() {
int stackSize=input.length();
StackX theStack=new StackX(stackSize);
//a{b(c[d]e)f} {([ ])}
for(int j=0;j char ch=input.charAt(j);
switch(ch) {
case '{':
case '(':
case '[':
theStack.push(ch);//放入栈里
break;
case '}':
case ')':
case ']':
if(theStack.isEmpty()) {
char chx=theStack.pop();
if((ch=='}'&& chx!='{')||(ch==']'&& chx!='[')||(ch==')'&& chx!='(')
) {
System.out.println("Error:"+ch+"at"+j);
}
}
else {
System.out.println("Error:"+ch+"at"+j);
}
break;
default:
break;
}
}
if(!theStack.isEmpty())System.out.println("Error:missing right delimiter.");
}
}

import java.io.*;

public class BracketApp {
public static void main(String[]args) throws IOException {
String input;
while(true) {
System.out.println("Enter string containing delimiters:");
System.out.flush();
input=getString();
if(input.equals(""))break;
BracketChecker theChecker=new BracketChecker(input);
theChecker.check();
}
}
public static String getString() throws IOException{
InputStreamReader isr=new InputStreamReader(System.in);//输入流的读取器
BufferedReader br=new BufferedReader(isr);//带缓存的读取器
String s=br.readLine();
return s;
}
}

你可能感兴趣的:(java栈and栈实例(附代码、不是很懂))