[英]arrayImpOfStack.java how to reverse number in output?
我需要編寫 arrayImpOfStack.java 然后編寫一個 main 方法來讀取一系列數字並使用堆棧操作以相反的順序打印它們
如何反轉 output 中的數字?
我有 2 個 class
class 1
class stackAr
{
int elements [];
int top; // is the index of the cell containing the last elements added to the stack.
stackAr(int maxlength) {top = maxlength; elements = new int[maxlength]; };
// initially the value of the variable top = maxlength which means the stack is empty
// So the variable top must be decremented before pushing new element, which means
// the first element is pushed at cell numbered maxlength-1
// the second element is pushed at the cell numbered maxlength-2
// the third element is pushed at the cell numbered maxlength-3
// an so on the stack is full when top = 0
void push(int x)
{ if (top == 0) System.out.println("the Stack is Full ");
else elements[--top] = x;
}
Boolean isEmpty() // Note the stack is empty when top = elements.length which is the maxlength
{
if (top == elements.length) return true; else return false;
}
void pop() // pop increment the variable top to ignore the last element added to the stack
{
if (!isEmpty()) top++ ;
else System.out.println("Stack is Empty ");
}
int Top() // return the last element added to the stack
{if (!isEmpty()) return elements[top];
else {System.out.println("Stack is Empty "); return top;}
}
void MakeNull() {top = elements.length;} // make the stack empty
}
我有 class 主要
public static void main(String[] args) {
stackAr s = new stackAr(20);
s.push(1);
s.push(9);
s.push(2);
s.push(10);
while(!s.isEmpty())
{
System.out.println(s.Top());
s.pop();
}
您可以遞歸地執行此操作:
public static void printStackInReverse(stackAr s){
if(!s.isEmpty()){
int e = s.Top();
s.pop();
printStackInReverse(s);
System.out.println(e);
s.push(e);
}
}
在你的情況下:
public static void main(String[] args) {
stackAr s = new stackAr(20);
s.push(1);
s.push(9);
s.push(2);
s.push(10);
printStackInReverse(s);
}
完整示例:
class stackAr
{
int elements [];
int top; // is the index of the cell containing the last elements added to the stack.
stackAr(int maxlength) {top = maxlength; elements = new int[maxlength]; };
void push(int x)
{ if (top == 0) System.out.println("the Stack is Full ");
else elements[--top] = x;
}
Boolean isEmpty() // Note the stack is empty when top = elements.length which is the maxlength
{
return top == elements.length;
}
void pop() // pop increment the variable top to ignore the last element added to the stack
{
if (!isEmpty()) top++ ;
else System.out.println("Stack is Empty ");
}
int Top() // return the last element added to the stack
{if (!isEmpty()) return elements[top];
else {System.out.println("Stack is Empty "); return top;}
}
void MakeNull() {top = elements.length;} // make the stack empty
public static void printStackInReverse(stackAr s){
if(!s.isEmpty()){
int e = s.Top();
s.pop();
printStackInReverse(s);
System.out.println(e);
s.push(e);
}
}
public static void main(String[] args) {
stackAr s = new stackAr(20);
s.push(1);
s.push(9);
s.push(2);
s.push(10);
printStackInReverse(s);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.