首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >arrayImpOfStack.java如何反转输出中的数字?

arrayImpOfStack.java如何反转输出中的数字?
EN

Stack Overflow用户
提问于 2021-03-16 21:04:28
回答 1查看 74关注 0票数 0

我需要编写arrayImpOfStack.java,然后编写一个main方法来读取一系列数字,并使用堆栈操作以相反的顺序打印它们

如何反转输出中的数字?

我有两节课

类别1

代码语言:javascript
复制
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
} 

我有main类

代码语言:javascript
复制
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();
  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-16 21:31:38

您可以递归地执行此操作:

代码语言:javascript
复制
public static void printStackInReverse(stackAr s){
    if(!s.isEmpty()){
        int e = s.Top();
        s.pop();
        printStackInReverse(s);
        System.out.println(e);
        s.push(e);
    }
}

在您的案例中:

代码语言:javascript
复制
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);
}

完整示例:

代码语言:javascript
复制
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);
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66655808

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档