-
Notifications
You must be signed in to change notification settings - Fork 5
/
ExtendingStack.java
32 lines (27 loc) · 1.13 KB
/
ExtendingStack.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
import java.util.ArrayList;
import java.util.NoSuchElementException;
public class ExtendingStack extends ArrayList<Character> implements ICharStack {
// question: why don't we implement isEmpty? Isn't it required by the interface?
@Override
public void push(char c) {
// add to the end (find the arraylist method that adds to the end)
}
@Override
public char peek() throws NoSuchElementException {
if(isEmpty())
throw new NoSuchElementException();
// return the last char
}
@Override
public char pop() throws NoSuchElementException {
char ret = peek();
// remove the last char (find the arraylist method to remove at a certain position)
return ret;
}
}
// question: how does this array backed implementation differ
// from our linked implementation? Do they use different amounts of
// cpu time or memory? What must happen when the array gets full but we
// add another character? Is the memory used for the Nodes in the
// linked version stored all together or scattered? Would this effect how
// fast it is to retrieve data from these memory locations?