管理多个堆栈时出现问题

时间:2015-10-14 05:51:33

标签: java algorithm stack

调查管理多个堆栈的解决方案,发布我正在调试的问题和代码。问题是,为什么函数popAt(int index)从下一个子堆栈的底部移位?是否因为子堆栈1顶部的下一个元素(按堆栈顺序),是子堆栈2的底部元素?我不确定这种行为是否正确,以及预期的行为是否在堆栈1的pop元素之后,pop的下一个元素是堆栈1中的元素,它在前一个顶部之下,而不是下一个堆栈的底部?

想象一下(文字)一叠盘子。如果堆栈太高,它可能会倒塌。因此,在现实生活中,当前一个堆栈超过某个阈值时,我们可能会启动一个新堆栈。一个模仿这个的数据结构SetOfStacks。 SetOfStacks应该由几个堆栈组成,并且应该在前一个堆栈超过容量时创建一个新堆栈。 SetOfStacks.push()和SetOfStacks.pop()的行为应该与单个堆栈相同(也就是说,pop()应该返回与只有一个堆栈时相同的值),并且函数popAt(int index)在特定的子堆栈上执行弹出操作。

public class SetOfStacks {
    ArrayList<Stack> stacks = new ArrayList<>();
    public int capacity;
    public SetOfStacks(int capacity) {
        this.capacity = capacity;
    }
    public Stack getLastStack() {
        if (stacks.size() == 0) return null;
        return stacks.get(stacks.size() - 1);
    }
    public void push(int v) { /* see earlier code */
    }
    public int pop() {
        Stack last = getLastStack();
        System.out.println(stacks.size());
        int v = last.pop();
        if (last.size == 0) stacks.remove(stacks.size() - 1);
        return v;
    }
    public int popAt(int index) {
        return leftShift(index, true);
    }
    public int leftShift(int index, boolean removeTop) {
        Stack stack = stacks.get(index);
        int removed_item;
        if (removeTop) removed_item = stack.pop();
        else removed_item = stack.removeBottom();
        if (stack.isEmpty()) {
            stacks.remove(index);
        } else if (stacks.size() > index + 1) {
            int v = leftShift(index + 1, false);
            stack.push(v);
        }
        return removed_item;
    }
 }
 public class Stack {
    private int capacity;
    public Node top, bottom;
    public int size = 0;
    public Stack(int capacity) {
        this.capacity = capacity;
    }
    public boolean isAtCapacity() {
        return capacity == size;
    }
    public void join(Node above, Node below) {
        if (below != null) below.above = above;
        if (above != null) above.below = below;
    }
    public boolean push(int v) {
        if (size >= capacity) return false;
        size++;
        Node n = new Node(v);
        if (size == 1) bottom = n;
        join(n, top);
        top = n;
        return true;
    }
    public int pop() {
        Node t = top;
        top = top.below;
        size--;
        return t.value;
    }
    public boolean isEmpty() {
        return size == 0;
    }
    public int removeBottom() {
        Node b = bottom;
        bottom = bottom.above;
        if (bottom != null) bottom.below = null;
        size--;
        return b.value;
    }
 }
提前谢谢, 林

2 个答案:

答案 0 :(得分:1)

您的代码中的

leftShift()可以通过递增索引递归调用,这就是为什么如果您使用索引1调用它,它可能会从堆栈#2弹出然后(和,如果所有堆栈的大小都是1个元素,它将继续堆栈#3,#4等等:()

答案 1 :(得分:1)

这是我在java中使用linkedList的板堆叠的简单解决方案。

class Stack<T>{
Node top;
NodeTop nodeTop;
int count = 1;
int i = 3;
static class Node<T>{
    private T data;
    private Node next;
    Node(T data){
        this.data = data;
        next = null;
    }
}

static class NodeTop<T>{
    private Node<T> top;
    private NodeTop<T> next;
    NodeTop(Node top){
        this.top = top;
    }
}
public void push(T data){
        if(count > i){
            System.out.println("Starting new row of plates");
            NodeTop temp = new NodeTop(top);
            temp.next = nodeTop;
            nodeTop = temp;
            top = null;
            i = i + 3;
        }
        Node temp = new Node(data);
        temp.next = top;
        top = temp;
        count++;
        System.out.println(data);
}

public void pop(){
    if(top == null){
        System.out.println("Current row does not contains any plates, moving to next row");
        if(nodeTop == null){
            System.out.println("No Plates left");
            return;
        }
        top = nodeTop.top;
        nodeTop = nodeTop.next;
        i = i - 3;
    }
    System.out.println(top.data);
    top = top.next;
    count--;
}

}