【问题标题】:Implement two stacks using one array使用一个数组实现两个堆栈
【发布时间】:2015-07-22 12:00:51
【问题描述】:

我仍然不知道在哪里实现第二个堆栈。我应该再上一节课吗?我不太确定如何完成。我会继续寻找。任何帮助,将不胜感激!我也无法判断我的 pop() 方法是否有效。我打印了堆栈。
输出: 真的 5 10 15 20 25 30 35 假的

public class twoStack {
    int maxSize = 10;
    int top;
    int top2;
    int arr[];

    public twoStack(int x) 
    {
        maxSize = x;
        arr = new int[maxSize];
        top = 0;
        top2 = maxSize;
    }

        //push pop empty peek
    public boolean empty()
    {
        if(top == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public boolean empty2()
    {
        if(top2 == maxSize)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public void push(int x)
    {
        if (top<maxSize)
        {
        arr[top] = 10;
        top++;
        }
        else 
        {
            System.out.print("Stack overflow");
        }
    }
    public void push2(int x)
    {
        if(top2<0)
        {
            arr[top2] = 0;
                    top2--;
        }
        else
        {
            System.out.print("Stack Overflow");
        }
    }
    @SuppressWarnings("null")
    public Object pop()
    {
        if(!this.empty())
        {
            int temp = (int) this.peek();
            arr[top-1]=(Integer) null ;
            top--;
            return temp;
        }
        else
        {
            return null;
        }
    }
    @SuppressWarnings("null")
    public Object pop2()
    {
        if(!this.empty2())
        {
            int temp = (int) this.peek();
            arr[top+1]=(Integer) null;
            top++;
            return temp;
        }
        else
        {
            return null;
        }
    }
    public Object peek()
    {
        if (!this.empty())
        {
        return arr[top-1];
        }
        else
        {
            return null;
        }
    }
    public Object peek2()
    {
        if(!this.empty2())
        {
            return arr[top+1];
        }
        else
        {
            return null;
        }
    }

}



//mainstack
package twoStack;

import java.util.Stack;

public class mainStack {

    public static void main(String[] args) {
//MM(main method)
        Stack<Integer> myStack= new Stack<Integer>();
        System.out.println(myStack.empty());
        myStack.push(5);
        System.out.println(myStack.peek());
        myStack.push(10);
        System.out.println(myStack.pop());
        myStack.push(15);
        System.out.println(myStack.peek());
        myStack.push(20);
        System.out.println(myStack.peek());
        myStack.push(25);
        System.out.println(myStack.pop());
        myStack.push(30);
        System.out.println(myStack.peek());
        myStack.push(35);
        System.out.println(myStack.peek());
        myStack.push(40);
        System.out.println(myStack.empty());






    }

}

【问题讨论】:

  • 用预期的结果编写你的测试。使用调试器单步调试您的代码,看看哪里出了问题。
  • 很多代码有问题,很难决定从哪里开始。如:将E推入ints的数组中;任何地方都没有创建数组;没有显示具体类(只有一个抽象类); ...它一直在继续。
  • @MarkoTopolnik 嗨!你把我引向了正确的方向,谢谢。我找到了另一本书,现在我能够实现一个有效的完整堆栈。我不知道如何将同一个数组中的第二个堆栈放在一起。另外我不知道如何编辑我的问题以显示我的更新。抱歉,我对此很陌生!
  • 您可以将所有奇数索引分配给一个堆栈,将所有偶数索引分配给另一个堆栈

标签: java arrays stack


【解决方案1】:

如果您有一个最大大小的数组,则可能有两个堆栈:堆栈从某个方向的固定位置增长并在那里收缩。起始位置是固定的。

  • 为两个堆栈选择一个固定的底部索引
  • 从底部索引向上开始第一个堆栈 ++
  • 从底部索引向下开始第二个堆栈 --

使用完整的未分配空间,即 ++ 和 -- 以数组大小为模。 如果两个堆栈指针相遇,则两个堆栈都已满。

不知道是不是我说的太多了。也许只是堆栈可能会向上和向下以及围绕(模)增长,并且具有固定的起点。

【讨论】:

  • 哦!这就说得通了。我会继续努力并在今晚提供更新。谢谢@Joop Eggen
【解决方案2】:

试试这个:

public class TwooStacksInAnArray {

    int[] array;
    int headOne,headTwo;

    public TwooStacksInAnArray(int n){
        array=new int[n];
        headOne=-1;
        headTwo=array.length;

    }

    public void pushX(int data){
        if(headTwo-headOne>1)
            array[++headOne]=data;
        else
            System.out.println("No space to fill data on stack1 ");
    }
    public void pushY(int data){
        if(headTwo-headOne>1)
            array[--headTwo]=data;
        else 
            System.out.println("No space to fill data on stack2 ");

    }
    public int popX(){
        if(headOne>-1)
            return array[headOne--];
        else {
            System.out.println("underflow stack1");
            return 0;
        }
    }
    public int popY(){
        if(headTwo<array.length)
            return array[headTwo++];
        else{
            System.out.println("underflow stack2");
            return 0;
        }
    }
    public boolean isEmptyX(){
        return (headOne==-1);
    }
    public boolean isEmptyY(){
        return (headTwo==(array.length));
    }

}


public class stackDriver {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        TwooStacksInAnArray twostack=new TwooStacksInAnArray(10);
        twostack.pushX(10);
        twostack.pushY(9);
        twostack.pushX(100);
        twostack.pushY(99);
        System.out.println("Poped element from stack 1: "+twostack.popX());
        System.out.println("Poped element from stack 2: "+twostack.popY());

    }

}

【讨论】:

    猜你喜欢
    • 2015-08-07
    • 2014-04-21
    • 2010-09-09
    • 2010-10-15
    • 2017-12-21
    • 2012-01-01
    • 2012-09-02
    • 2015-08-17
    • 2019-02-22
    相关资源
    最近更新 更多