-
Notifications
You must be signed in to change notification settings - Fork 0
/
PDStack.java
95 lines (81 loc) · 1.67 KB
/
PDStack.java
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
Created on July 10, 2007
@author Stan Green
This class implements a type of stack that pushes down one item at a time. The stack has a max size.
When an item is put on the stack that would violate the max, the object on the bottom of the stack is
deleted. The deleted item is returnd from the push method. An object can be retrieved base on the array postion
in the stack.
*/
import java.util.*;
import java.io.*;
public class PDStack implements Serializable
{
private static final long serialVersionUID = 3424907626762634404L;
private Vector<Object> ioVector;
private int iiMax;
PDStack()
{
iiMax = 30;
ioVector = new Vector<Object>(iiMax);
}
PDStack(int aiMax)
{
iiMax = aiMax;
ioVector = new Vector<Object>(iiMax);
}
public void remove(int aiItem)
{
ioVector.remove(aiItem);
}
public void setMax(int aiMax)
{
if(iiMax > aiMax) //Making the array smaller.
{
int liVectorSize = ioVector.size();
if(liVectorSize > aiMax)
{
for(int i = liVectorSize -1; i >= aiMax; i--)
{
ioVector.remove(i);
}
iiMax = aiMax;
}
else
{
iiMax = aiMax;
}
}
else
{
iiMax = aiMax;
}
}
public int getMax()
{
return iiMax;
}
public int getSize()
{
return ioVector.size();
}
public Object push(Object aObject)
{
Object loObject = null;
if(ioVector.size() == iiMax)
{
loObject = ioVector.elementAt(iiMax - 1);
ioVector.remove(iiMax - 1);
}
ioVector.insertElementAt(aObject,0);
return loObject;
}
public Object get(int aiPostion)
{
Object loObject = null;
if(aiPostion + 1 <= ioVector.size())
{
loObject = ioVector.elementAt(aiPostion);
}
return loObject;
}
}