-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackPile.java
More file actions
63 lines (50 loc) · 1.47 KB
/
Copy pathStackPile.java
File metadata and controls
63 lines (50 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.util.ArrayList;
public class StackPile extends StackImplementationWithArray {
ArrayList<StackImplementationWithArray> stackpile=new ArrayList<StackImplementationWithArray>();
int capacity;
StackPile(int capacity)
{
super(capacity);
this.capacity=capacity;
}
public void push(int data) throws Exception
{
StackImplementationWithArray currentStack=getStack("push");
currentStack.push(data);
}
public int pop() throws Exception
{
StackImplementationWithArray currentStack=getStack("pop");
//System.out.println(stackpile.size());
return currentStack.pop();
}
public StackImplementationWithArray getStack(String operation) throws Exception
{
if (stackpile.size()==0)
throw new StackEmptyException();
if (stackpile.isEmpty() || (stackpile.get(stackpile.size()-1).top==capacity-1 && operation=="push"))
{
StackImplementationWithArray newStack=new StackImplementationWithArray(capacity);
stackpile.add(newStack);
//System.out.println(stackpile.get(0));
return newStack;
}
if (stackpile.get(stackpile.size()-1).top==-1)
{
//System.out.println(stackpile.size());
stackpile.remove(stackpile.size()-1);
//System.out.println("hi");
}
//System.out.println("hiiiii");
//System.out.println(stackpile.size());
return stackpile.get(stackpile.size()-1);
}
public void display()
{
for (int i=0;i<stackpile.size();i++)
{
stackpile.get(i).display();
System.out.println("*************");
}
}
}