使用两个链表实现栈

用两个队列实现栈:

入栈的思路:就是把即将放入的数据放到存有数据的哪个队列里面去;
出栈的思路:就是先把有数据的那个队列中所有的数据依次出队,除过最后一个元素外其他元素放到另一个队列中去,
然后把最后一个元素出队。(front+1)% 队列的长度==rear时表示队列中只有一个数据;


//队列类
class Queue {
    private int front; //对头
    private int rear;// 队尾
    private int[] elem;
    private int size;  //使用空间

    public Queue(int s) {
        this.front = 0;
        this.rear = 0;
        elem = new int[s];
        this.size=elem.length;
    }

    public Queue() {
        this(10);
    }

    public int[] getElem() {
        return elem;
    }
    //得到队列大小
    public int getSize() {
        return size;
    }

    //得到头指针
    public int getFront() {
        return front;
    }

    //得到尾指针
    public int getRear() {
        return rear;
    }

    //是否满
    public boolean isFull() {
        return (rear + 1) % elem.length == front;
    }

    //入队;
    public void push(int val) {
        if (isFull()) {
            return;
        }
        elem[rear] = val;
        rear = (this.rear + 1) % elem.length;

    }

    //是否空
    public boolean isEmpty() {
        return rear == front;
    }

    //出队
    public void pop() {
        if (isEmpty()) {
            return;
        }
        int num = elem[front];
        elem[front] = -1;
        front = (front + 1) % elem.length;

    }
    //得到front的节点内容
    public int getFrontData() {
        return elem[front];
    }

    //打印函数
    public void show() {

        for (int i = front; i != rear; i = (i + 1) % elem.length) {
            System.out.print(elem[i] + "  ");
        }
        System.out.println();
    }
}





//用两个队列实现栈
 /*
 *  入栈的思路:就是把即将放入的数据放到存有数据的哪个队列里面去;
 *  出栈的思路:就是先把有数据的那个队列中所有的数据依次出队,除过最后一个元素外其他元素放到另一个队列中去,
 *             然后把最后一个元素出队。(front+1)% 队列的长度==rear时表示队列中只有一个数据;
 * */

class stack {

    private Queue queue1;
    private Queue queue2;

    public stack() {
        queue1 = new Queue();
        queue2 = new Queue();
    }

    private Queue EmptyQueue;
    private Queue NotEmptyQueue;

    //
    private void fun() {
 //这个方法是为了找出来哪个队列是空的,哪个队列是有值的

        if (queue1.isEmpty() && !queue2.isEmpty()) {
            EmptyQueue = queue1;
            NotEmptyQueue=queue2;
        } else if (!queue1.isEmpty() && queue2.isEmpty()){
            EmptyQueue=queue2;
            NotEmptyQueue = queue1;
        } else {
            EmptyQueue = queue1;
            NotEmptyQueue=queue2;
        }
    }

    //入栈
    public void push(int val) {
        fun();
        NotEmptyQueue.push(val);
    }

    // 出栈
    public void pop() {
        fun();
        if(NotEmptyQueue.isEmpty()) {
            throw  new UnsupportedOperationException("栈为空");
        }
        //把这个队列的元素放到另一个队列然后出队,留最后一个节点
        while (((NotEmptyQueue.getFront()+1)%NotEmptyQueue.getSize() !=NotEmptyQueue.getRear())){
            EmptyQueue.push(NotEmptyQueue.getFrontData());  //空的队列把这不为空的队列的第一个节点入队
            NotEmptyQueue.pop();                            //不为空的队列出队
        }
        NotEmptyQueue.pop(); //把目标节点出队

    }

    //得到栈顶元素
    public int getTop() {
        fun();
        if(NotEmptyQueue.isEmpty()) {
            throw  new UnsupportedOperationException("栈为空");
        }
        return NotEmptyQueue.getRear()-1>=0 ? NotEmptyQueue.getElem()[NotEmptyQueue.getRear()-1] : NotEmptyQueue.getElem()[NotEmptyQueue.getSize()-1];
    }

    //打印函数
    public void show() {
        fun();
        int  i=NotEmptyQueue.getFront();
        while ((i)%NotEmptyQueue.getSize()!=NotEmptyQueue.getRear()){
            System.out.print(NotEmptyQueue.getElem()[i]+" ");
            i=(i+1)%NotEmptyQueue.getSize();
        }
        System.out.println( );
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41884976/article/details/84865560
今日推荐