1、栈
package test;
public class Stack {
private int array[];
private int max;
private int top;
public Stack(int max){
this.max=max;
array=new int[max];
top = 0;
}
public void push(int value){
if(isFull()){
System.out.println("full, can not insert");
return;
}
array[top++]=value;
}
public int pop(){
return array[--top];
}
private boolean isEmpty(){
if(top == 0){
return true;
}
return false;
}
private boolean isFull() {
// TODO Auto-generated method stub
if(top == max){
return true;
}
return false;
}
public void display(){
while(!isEmpty()){
System.out.println(pop());
}
}
public static void main(String[] args) {
Stack s = new Stack(5);
s.push(1);
s.push(3);
s.push(5);
s.push(7);
s.push(9);
s.display();
}
2、OJ题:单词逆序(错误)
package test;
import java.util.Scanner;
public class Stack {
private int array[];
private int max;
private int top;
public Stack(int max){
this.max=max;
array=new int[max];
top = 0;
}
public void push(int value){
if(isFull()){
System.out.println("full, can not insert");
return;
}
array[top++]=value;
}
public int pop(){
return array[--top];
}
private boolean isEmpty(){
if(top == 0){
return true;
}
return false;
}
private boolean isFull() {
// TODO Auto-generated method stub
if(top == max){
return true;
}
return false;
}
public void display(){
while(!isEmpty()){
System.out.println(pop());
}
}
public String reverse(String in){
String out = "";
for (int i = 0; i < in.length(); i++) {
char c = in.charAt(i);
push(c);
}
while(!isEmpty()){
out += pop();
}
return out;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
Stack s = new Stack(str.length());
System.out.println(s.reverse(str));
}
}