Java 如何在不固定大小的情况下在main中创建包含整数值的堆栈 静态堆栈排序(堆栈){ 堆栈辅助=新堆栈(); INTE; 如果(!s.isEmpty())辅助推送(s.pop()); 而(!s.isEmpty()){ e=s.peek(); 如果(e>auxiliar.peek()&&eauxiliar.peek()&&e>s.peek())| |(es.peek()){ 辅助推(s.pop()); s、 推(e); s、 推(辅助pop()); } else if(e
向堆栈添加值的一种方法如下:Java 如何在不固定大小的情况下在main中创建包含整数值的堆栈 静态堆栈排序(堆栈){ 堆栈辅助=新堆栈(); INTE; 如果(!s.isEmpty())辅助推送(s.pop()); 而(!s.isEmpty()){ e=s.peek(); 如果(e>auxiliar.peek()&&eauxiliar.peek()&&e>s.peek())| |(es.peek()){ 辅助推(s.pop()); s、 推(e); s、 推(辅助pop()); } else if(e,java,stack,Java,Stack,向堆栈添加值的一种方法如下: static Stack<Integer> sort(Stack<Integer> s){ Stack<Integer> auxiliar = new Stack<Integer>(); int e; if(!s.isEmpty()) auxiliar.push(s.pop()); while (!s.isEmpty()){ e=s.peek();
static Stack<Integer> sort(Stack<Integer> s){
Stack<Integer> auxiliar = new Stack<Integer>();
int e;
if(!s.isEmpty()) auxiliar.push(s.pop());
while (!s.isEmpty()){
e=s.peek();
if(e>auxiliar.peek() && e<s.peek())
auxiliar.push(e);
else if ((e>auxiliar.peek() && e>s.peek())|| (e<auxiliar.peek() &&
e>s.peek())){
auxiliar.push(s.pop());
s.push(e);
s.push(auxiliar.pop());
}
else if (e<auxiliar.peek()&&e<s.peek()){
s.push(auxiliar.pop());
s.push(e);
}
else auxiliar.push(e);
}
return auxiliar;
}
void printStack(Stack<Integer> s){
System.out.print("[");
while (!s.isEmpty()){
System.out.print(s.pop()+" ");
}
System.out.print("]");
}
public static void main(String[] args) {
Stack<Integer> a = new Stack<Integer>();
a = {2,3,8,6,4}; //In this line I do not know how to give values to the stack.
// I don't know if I have to make a function outside of main or if
// I can do it like I was trying
SortedStack element = new SortedStack();
element.sort(a);
}
Stack s=新堆栈();
s、 addAll(Arrays.asList(2,3,4,6,4));
使用此方法,它将按从左到右的顺序推进,因此4将位于堆栈顶部,2将位于底部。向堆栈添加值的一种方法如下:
static Stack<Integer> sort(Stack<Integer> s){
Stack<Integer> auxiliar = new Stack<Integer>();
int e;
if(!s.isEmpty()) auxiliar.push(s.pop());
while (!s.isEmpty()){
e=s.peek();
if(e>auxiliar.peek() && e<s.peek())
auxiliar.push(e);
else if ((e>auxiliar.peek() && e>s.peek())|| (e<auxiliar.peek() &&
e>s.peek())){
auxiliar.push(s.pop());
s.push(e);
s.push(auxiliar.pop());
}
else if (e<auxiliar.peek()&&e<s.peek()){
s.push(auxiliar.pop());
s.push(e);
}
else auxiliar.push(e);
}
return auxiliar;
}
void printStack(Stack<Integer> s){
System.out.print("[");
while (!s.isEmpty()){
System.out.print(s.pop()+" ");
}
System.out.print("]");
}
public static void main(String[] args) {
Stack<Integer> a = new Stack<Integer>();
a = {2,3,8,6,4}; //In this line I do not know how to give values to the stack.
// I don't know if I have to make a function outside of main or if
// I can do it like I was trying
SortedStack element = new SortedStack();
element.sort(a);
}
Stack s=新堆栈();
s、 addAll(Arrays.asList(2,3,4,6,4));
使用此方法,它将按从左到右的顺序推进,因此4将位于堆栈顶部,2将位于底部