fw12013年12月13日1:17:54 - multi-stack -java
2013-12-13 14:19
204 查看
package test_kepler;
import java.util.ArrayList;
import java.util.Stack;
// the last stack is important ; try to implement it via queue the first queue will be very important
public class wtfStack {
static int max_stack_size = 10;
ArrayList<Stack> stacks = new ArrayList<Stack>();
public wtfStack()
{
Stack stack = new Stack();
stacks.add(stack);
}
public Stack getLast()
{
return (Stack)stacks.get(stacks.size()-1);
}
public void push(int k)
{
if(getLast().size() == max_stack_size )
{
Stack newStack = new Stack();
stacks.add(newStack);
}
getLast().push(k);
}
public Object pop()
{
if(stacks.size()!=0)
{
if(getLast().size() == 0)
{
stacks.remove(stacks.size()-1);
}
return getLast().pop();
}
else
{
return null;
}
}
//print all the element in the stacks;
void print()
{
for(int i = 0;i<stacks.size();++i)
{
Stack stack_temp = stacks.get(i);
for (Object x : stack_temp)
{
System.out.println(x);
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
wtfStack ws = new wtfStack();
Stack test_stack = new Stack();
for(int i = 0;i<20;++i)
{
ws.push(i);
test_stack.push(i);
}
System.out.println("for the advanced");
ws.print();
System.out.println("for the naieve");
for(Object x : test_stack)
{
System.out.println(x);
}
for(int j = 0;j<15;++j)
{
System.out.println("advan");
System.out.println(ws.pop());
System.out.println("normal");
System.out.println(test_stack.pop());
}
// ws.print();
}
}
import java.util.ArrayList;
import java.util.Stack;
// the last stack is important ; try to implement it via queue the first queue will be very important
public class wtfStack {
static int max_stack_size = 10;
ArrayList<Stack> stacks = new ArrayList<Stack>();
public wtfStack()
{
Stack stack = new Stack();
stacks.add(stack);
}
public Stack getLast()
{
return (Stack)stacks.get(stacks.size()-1);
}
public void push(int k)
{
if(getLast().size() == max_stack_size )
{
Stack newStack = new Stack();
stacks.add(newStack);
}
getLast().push(k);
}
public Object pop()
{
if(stacks.size()!=0)
{
if(getLast().size() == 0)
{
stacks.remove(stacks.size()-1);
}
return getLast().pop();
}
else
{
return null;
}
}
//print all the element in the stacks;
void print()
{
for(int i = 0;i<stacks.size();++i)
{
Stack stack_temp = stacks.get(i);
for (Object x : stack_temp)
{
System.out.println(x);
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
wtfStack ws = new wtfStack();
Stack test_stack = new Stack();
for(int i = 0;i<20;++i)
{
ws.push(i);
test_stack.push(i);
}
System.out.println("for the advanced");
ws.print();
System.out.println("for the naieve");
for(Object x : test_stack)
{
System.out.println(x);
}
for(int j = 0;j<15;++j)
{
System.out.println("advan");
System.out.println(ws.pop());
System.out.println("normal");
System.out.println(test_stack.pop());
}
// ws.print();
}
}
相关文章推荐
- fw 3.6 - stack sort- java version - 2013年12月16日23:23:32
- fw - dfs: stack/recursive ; bfs : stack/recursive- java version - 2013年12月20日18:37:18
- java数据结构之——Stack(栈)
- Error处理:Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack tra
- 两个郁闷的异常: java.lang.StackOverflowError和java.net.SocketException: Broken pipe
- Java容器:Stack,Queue,PriorityQueue和BlockingQueue
- 深入Java虚拟机:JVM中的Stack和Heap
- Java里的堆(heap)栈(stack)和方法区(method)
- Java stack转arraylist
- TextWatcher 出现 java.lang.StackOverflowError 的解决有关问题
- Java集合的Stack、Queue、Map的遍历
- java.lang.Exception: DEBUG STACK TRACE for PoolBackedDataSource.close().
- spring mvc restful java.lang.Stackoverflowerror
- java.lang.StackOverflowError: null at thymeleaf.util.JavaScriptUtils.printObject(ScripUtils.java:353
- STACK_TRACE=java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- java学习笔记(六) collections--stack
- Android -- java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
- Can not find a java.io.InputStream with the name [inputStream] in the invocation stack
- Java里的堆(heap)栈(stack)和方法区(method)
- java数据结构——5栈(Stack)