问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
import java.util.ArrayList;
import java.util.Date;
import java.util.Stack;
public class DoubleStack<E> {
public int bottom1;
public int bottom2;
public int top1;
public int top2;
private E[] date;
public int m = 10;
public int capacity;
Stack<Integer> s = new Stack<Integer>();
public DoubleStack() {
date = (E[]) new DoubleStack[m];
capacity = m;
top1 = -1;
top2 = capacity;
}
private boolean isFull(int no) {
if (top1 == top2 - 1)
throw new RuntimeException("栈满!");
return false;//非空栈返回false;
}
private boolean isEmpty(int no) {
if (no == 1) {
if (top1 == -1) {
throw new RuntimeException("此栈为空");
}
if (no == 2) {
if (top2 == capacity) {
throw new RuntimeException("此栈为空");
}
}
}
return true;
}
public boolean Push(int no, E e) {
if (top1 == top2 - 1) {
throw new RuntimeException("栈满!");
}
if (no == 1) {
top1++;
date[top1] = e;
System.out.println("元素" + e + "成功入栈1");
} else if (no == 2) {
top2--;
date[top2] = e;
System.out.println("元素" + e + "成功入栈2");
}
return true;
}
public E Pop(int no) {
if (no == 1) {
if (top1 == -1)
throw new RuntimeException("栈1为空");
return (E) date[top1--];
} else if (no == 2) {
if (top2 == capacity)
throw new RuntimeException("栈2为空");
return (E) date[top2++];
}
return null;
}
public void Show() {
for (int i = 0; i < 10; i++) {
s.push(i);
}
for (Integer x : s) {
System.out.println(x);
}
}
public String toString() {
String str1 = "栈1:[";
String str2 = "栈2:[";
for (int i = top1; i >= 0; i--) {
if (i == 0)
str1 = str1 + (E) date[i];
else
str1 = str1 + (E) date[i] + ",";
}
str1 += "]";
for (int i = top2; i < capacity; i++) {
if (i == capacity - 1)
str2 = str2 + (E) date[i];
else
str2 = str2 + (E) date[i] + ",";
}
str2 += "]";
return str1 + "\n\r" + str2;
}
}
import java.util.Stack;
public class run {
public static void main(String[] args) {
DoubleStack stack=new DoubleStack();
stack.Push(1,1);
stack.Push(1,3);
stack.Push(1,5);
stack.Push(1,7);
stack.Push(1,9);
stack.Push(2,2);
stack.Push(2,4);
stack.Push(2,6);
}
}