1、栈
栈的实现
package use;
//实现栈
public class ArrarysStack {
private int maxStack;//定义栈的最大长度
private int [] Stack;//定义数组
private int top=-1;//将初始top设为1
//接受设定最大长度
public ArrarysStack(int maxStack){
this.maxStack=maxStack;
Stack=new int[maxStack];
}
//判断是否栈满
public boolean isFull(){
return this.top==this.maxStack-1;
}
//判断是否栈空
public boolean isEmpty(){
return this.top==-1;
}
//入栈
public void put(int val){
if(isFull()){
throw new RuntimeException("已满");
}
top++;
Stack[top]=val;
}
//弹出栈
public int pop(){
if(isEmpty()){
throw new RuntimeException("此栈是空");
}
int value=Stack[top];
top--;
return value;
}
//打印栈中内容
public void list(){
if(isEmpty()){
throw new RuntimeException("此栈是空,无数据");
}
for(int i=0;i<Stack.length;i++){
System.out.printf("Stack[%d]=%d\n",i,Stack[i]);
}
}
//栈中元素存在的个数
public int length(){
return this.top+1;
}
}
//判断是否是个运算符
public boolean isOper(char v){
return v=='+'||v=='-'||v=='*'||v=='/';
}
//判断运算符的优先级
public int priority(int oper){
if(oper=='*'||oper=='/'){
return 1;
}
else if (oper=='+'||oper=='-'){
return 0;
}
else
{ return -1;}
}
//获取栈顶数据
public int peek(){
return this.Stack[top];
}
//获取栈的容量大小
public int stackLength(){
return this.Stack.length;
}
//计算两个数的运算
public int calculate(int num1,int num2 ,int oper){
int result=0;
switch (oper){
case '+':
result=num1+num2;
break;
case '-':
result=num2-num1;
break;
case '*':
result=num1*num2;
break;
case'/':
result=num2/num1;
break;
default:
break;
}
return result;
}
}
习题一:
需求:判断某一个输入的字符串是否为回文。
package use;
public class TestApp {
public static void main(String [] args){
System.out.println(test("aba"));
}
public static boolean test(String val){
ArrarysStack arrarysStack=new ArrarysStack(10);
int length=val.length();
for (int i=0;i<length;i++){
arrarysStack.put(val.charAt(i));
}
String newVal="";
int length1=arrarysStack.length();
for(int i=0;i<length1;i++){
//是否是空
if(!arrarysStack.isEmpty()){
char pop=(char)arrarysStack.pop();
newVal=newVal+pop;
}
}
if(val.equals(newVal)){
return true;
}
else return false;
}
}
习题二:
使用栈完成类似表达式“string val="4+3+2+1*5”的运算
package use;
public class TestApp2 {
public static void main(String[] args){
String str="4+3+2*2-1";
ArrarysStack numStack=new ArrarysStack(10);
ArrarysStack operStack=new ArrarysStack(10);
//获取字符串长度
int length=str.length();
int temp1=0;
int temp2=0;
int oper1=0;
int result=0;
String values="";
for(int i=0;i<length;i++){
char c=str.charAt(i);
//判断是否为一个运算符
if(operStack.isOper(c)){
//如果不是空栈
if(!operStack.isEmpty()){
//比较运算符的优先级
if(operStack.priority(c)<=operStack.priority(operStack.peek())){
//去符号栈中获取符号,去数字栈中获取数字
temp1=numStack.pop();
temp2=numStack.pop();
oper1=operStack.pop();
result= numStack.calculate(temp1,temp2,oper1);
//把运算结果放入数字栈中
numStack.put(result);
//把运算符放入运算符栈中
operStack.put(c);
}else {
operStack.put(c);
}
}else{
operStack.put(c);
}
}else{
values+=c;
if(i==length-1){
numStack.put(Integer.parseInt(values));
}else {
char data=str.substring(i+1,i+2).charAt(0);
if(operStack.isOper(data)){
numStack.put(Integer.parseInt(values));
values="";
}
}
}
}
while (true){
if(operStack.isEmpty()){
break;
}
temp1=numStack.pop();
temp2=numStack.pop();
oper1=operStack.pop();
result=numStack.calculate(temp1,temp2,oper1);
numStack.put(result);
}
int res=numStack.pop();
System.out.println(res);
}
}