Stacks Flashcards

1
Q

Name the three core stack methods.

A

Push, pop, and peek.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Fill in the blank:

public interface PureStack<E>{
void \_\_(E item);
\_\_ pop();
E \_\_\_();
boolean \_\_();
int \_\_();
}</E>

A

push; E; peek; isEmpty; size

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Stacks may be implemented using what two data structures?

A

Arrays and linked lists.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

For a array-backed stack, the push, pop, and peek operations have what runtime?

A

O(1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

For a linked list-backed stack, the push, pop, and peek operations have what runtime?

A

O(1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

The stack method push() is analogous to what List method?

A

add()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

The stack method peek() is analogous to what List method?

A

get(size() - 1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

The stack method pop() is analogous to what List method?

A

remove(size() - 1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

The stack method push() is analogous to what Deque method?

A

addFirst()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

The stack method pop() is analogous to what Deque method?

A

removeFirst()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

The stack method peek() is analogous to what Deque method?

A

peekFirst()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Define “overflow”.

A

Trying to push onto a full stack.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Define “underflow”.

A

Trying to pop on an empty stack.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Write the pop() method for an array-based stack.

A

@Override
public E pop()
{
if (top == 0) throw new NoSuchElementException();
E ret = data[–top];
data[top] = null;
return ret;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Write the push() method for an array-based stack.

A

@Override
public void push(E item)
{
checkCapacity();
data[top++] = item;
}

/**
* Ensures that the backing array has space to store at least
* one additional element.
*/
private void checkCapacity()
{
if (top == data.length)
{
// create a copy of the data array with double the capacity
data = Arrays.copyOf(data, data.length * 2);
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly