[java] SetOfStacks 二维链表实现一种指定数据结构
SetOfStacks cc150 请实现一种数据结构SetOfStacks，由多个栈组成，其中每个栈的大小为si...

25
2019/10

# [java] SetOfStacks 二维链表实现一种指定数据结构

## SetOfStacks

cc150

### 代码

package _09_Linear.Stack_QueuePractice;

import org.junit.Test;

import java.util.ArrayList;

public class SetOfStacks {
public ArrayList<ArrayList<Integer>> setOfStack(int[][] ope, int size) {

ArrayList<ArrayList<Integer>> list = new ArrayList<>();
ArrayList<Integer> currStack = new ArrayList<>(size);

for (int[] opAndValue : ope) {
int op = opAndValue[0];
int value = opAndValue[1];

if (op == 1) {//1Push
if (currStack.size() == size) {//当前满了
currStack = new ArrayList<>(size);//创建一个新栈
}
} else {//2pop
if (currStack.size() == 0) {
list.remove(currStack);//站的列表中移除
currStack = list.get(list.size() - 1);//被操作的栈是列表中的上一个栈
}
currStack.remove(currStack.size() - 1);

}

}
return list;
}

@Test
public void test() {
int[][] ope = new int[3][2];
//push
ope[0][0] = 1;
ope[1][0] = 1;
ope[2][0] = 1;
//value
ope[0][1] = 1;
ope[1][1] = 2;
ope[2][1] = 3;

ArrayList<ArrayList<Integer>> ans = setOfStack(ope, 1);
for (ArrayList<Integer> a:ans) {

for (int i = 0; i < a.size(); i++) {
System.out.println(a.get(i));
}
System.out.println("+++");

}
}

}

### ans

1
+++
2
+++
3
+++

百度已收录

Last modification：October 26th, 2019 at 12:23 pm