|
@@ -10,29 +10,24 @@ public class Stack {
|
|
|
public static final int MAX_SIZE =20;
|
|
public static final int MAX_SIZE =20;
|
|
|
private int capacity; //栈的最大容量
|
|
private int capacity; //栈的最大容量
|
|
|
private int size; //栈现有的元素数
|
|
private int size; //栈现有的元素数
|
|
|
- private String elementType; //栈元素的类型,有char,String,int,double四种类型
|
|
|
|
|
- private ArrayList<Object> content;
|
|
|
|
|
|
|
+ private ArrayList<Integer> content;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 指定最大容量的构造方法
|
|
* 指定最大容量的构造方法
|
|
|
* @param capacity
|
|
* @param capacity
|
|
|
- * @param elementType
|
|
|
|
|
* @param content
|
|
* @param content
|
|
|
*/
|
|
*/
|
|
|
- public Stack(int capacity,String elementType, ArrayList<Object> content) {
|
|
|
|
|
|
|
+ public Stack(int capacity,ArrayList<Integer> content) {
|
|
|
this.capacity = capacity;
|
|
this.capacity = capacity;
|
|
|
- this.elementType = elementType;
|
|
|
|
|
this.content = content;
|
|
this.content = content;
|
|
|
this.size=content.size();
|
|
this.size=content.size();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 不指定最大容量的构造方法
|
|
* 不指定最大容量的构造方法
|
|
|
- * @param elementType
|
|
|
|
|
* @param content
|
|
* @param content
|
|
|
*/
|
|
*/
|
|
|
- public Stack( String elementType, ArrayList<Object> content) {
|
|
|
|
|
- this.elementType = elementType;
|
|
|
|
|
|
|
+ public Stack(ArrayList<Integer> content) {
|
|
|
this.content = content;
|
|
this.content = content;
|
|
|
this.capacity=MAX_SIZE;
|
|
this.capacity=MAX_SIZE;
|
|
|
this.size=content.size();
|
|
this.size=content.size();
|
|
@@ -42,18 +37,25 @@ public class Stack {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private boolean isFull(){
|
|
private boolean isFull(){
|
|
|
- return false;
|
|
|
|
|
|
|
+ return this.capacity==this.size;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
private boolean isEmpty(){
|
|
private boolean isEmpty(){
|
|
|
- return true;
|
|
|
|
|
|
|
+ return size==0 &&capacity>0;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* push操作成功返回true,否则返回false
|
|
* push操作成功返回true,否则返回false
|
|
|
* @return
|
|
* @return
|
|
|
*/
|
|
*/
|
|
|
- public boolean push(){
|
|
|
|
|
- return false;
|
|
|
|
|
|
|
+ public boolean push(int value){
|
|
|
|
|
+ boolean re =true;
|
|
|
|
|
+ if(this.isFull()){
|
|
|
|
|
+ re=false;
|
|
|
|
|
+ }else{
|
|
|
|
|
+ content.add(value);
|
|
|
|
|
+ }
|
|
|
|
|
+ return re;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -61,11 +63,17 @@ public class Stack {
|
|
|
* @return
|
|
* @return
|
|
|
*/
|
|
*/
|
|
|
public boolean pop(){
|
|
public boolean pop(){
|
|
|
- return false;
|
|
|
|
|
|
|
+ boolean re=true;
|
|
|
|
|
+ if(this.isEmpty()){
|
|
|
|
|
+ re=false;
|
|
|
|
|
+ }else{
|
|
|
|
|
+ content.remove(this.size-1);
|
|
|
|
|
+ }
|
|
|
|
|
+ return re;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public void makeEmpty(){
|
|
public void makeEmpty(){
|
|
|
-
|
|
|
|
|
|
|
+ content.clear();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -83,11 +91,7 @@ public class Stack {
|
|
|
return size;
|
|
return size;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public String getElementType() {
|
|
|
|
|
- return elementType;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public ArrayList<Object> getContent() {
|
|
|
|
|
|
|
+ public ArrayList<Integer> getContent() {
|
|
|
return content;
|
|
return content;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -101,11 +105,7 @@ public class Stack {
|
|
|
this.size = size;
|
|
this.size = size;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public void setElementType(String elementType) {
|
|
|
|
|
- this.elementType = elementType;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public void setContent(ArrayList<Object> content) {
|
|
|
|
|
|
|
+ public void setContent(ArrayList<Integer> content) {
|
|
|
this.content = content;
|
|
this.content = content;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -117,7 +117,6 @@ public class Stack {
|
|
|
return "Stack{" +
|
|
return "Stack{" +
|
|
|
"capacity=" + capacity +
|
|
"capacity=" + capacity +
|
|
|
", size=" + size +
|
|
", size=" + size +
|
|
|
- ", elementType='" + elementType + '\'' +
|
|
|
|
|
", content=" + content +
|
|
", content=" + content +
|
|
|
'}';
|
|
'}';
|
|
|
}
|
|
}
|