使用一个数组实现两个堆栈

时间:2015-07-22 12:00:51

标签: java arrays stack

我仍然不知道在哪里实现第二个Stack。我应该再上一堂课吗?我不太清楚如何完成。我会一直在寻找。任何帮助,将不胜感激!我也无法判断我的pop()方法是否正常工作。我打印出了堆栈 输出: 真正 五 10 15 20 25 三十 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());






    }

}

2 个答案:

答案 0 :(得分:0)

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

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

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

我想知道我是否说得太多了。也许只是堆栈可能会向上和向下增长,并且(模数),并且具有固定的开始。

答案 1 :(得分:0)

试试这个:

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());

    }

}