-
Notifications
You must be signed in to change notification settings - Fork 0
/
StackAsMyArrayList.java
49 lines (39 loc) · 1.12 KB
/
StackAsMyArrayList.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
public class StackAsMyArrayList<E>
{
MyArrayList<E> theStack;
public StackAsMyArrayList()
{ theStack = new MyArrayList<E>();
}
public void push(E newElement) //insert at end of array!
{
if (!theStack.checkSpace())
throw new IndexOutOfBoundsException
("Stack out of bounds");
theStack.add(theStack.getSize(),newElement);
}
public E pop() //remove end of array
{
E temp = null;
boolean isDone = false;
if (theStack.getSize() > 0)
temp=theStack.remove(theStack.getSize()-1);
return temp; // temp will be null in special case of empty list
}
public E peek() //remove end of array
{
E temp = null;
if (theStack.getSize() > 0)
temp=theStack.get(theStack.getSize()-1);
return temp; // temp will be null in special case of empty list
}
public String toString()
{
return theStack.toString();
}
public int getStackSize() {
return theStack.getSize();
}
public boolean checkStackUniform() {
return theStack.checkUniform();
}
}//end class