Stacks and Queues Flashcards

1
Q

Stacks

A

A stack of data. Last-in first-out ordering. It uses pop, push, peek and isEmpty. No constant-time access to ith item, but constant-time for adds and removes. Good for recursive algorithms.

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

pop()

A

Remove the top item from a stack

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

push(item)

A

Add an item to the top of the stack

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

peek()

A

Return the top of the stack or first in the queue

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

isEmpty()

A

Return true if and only if the stack/queue is empty.

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

Implementing a stack

A

A Stack class contains a static StackNode inner class with data and next fields. The Stack class contains a top field, and a pop, push, peek and isEmpty method. They can be implemented as linked list if items are added and removed from the same side: null .– node 1 .– node 2 .– … .– top

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

implement pop()

A

Ensure stack is not empty. Collect top node’s data. Set top to top.next. Return popped data.

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

implement push()

A

Create new node. Set new node next to top. Set top to new node.

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

implement peek()

A

Ensure stack or queue is not empty. return top.data or first.data.

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

implement isEmpty

A

return true if top or first is null.

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

Queue

A

First-in first-out ordering. Items are removed from the data structure in the same order that they are added. It uses add, remove, peek, and isEmpty methods. Good for caches and breadth first searches.

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

add(item)

A

Add an item to the end of the queue

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

remove()

A

Remove the first item from the queue

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

Implementing a queue

A

A queue class contains a inner static QueueNode class with a data and next field. The Queue class has first and last fields and add, remove, peek and isEmpty methods. A queue can be implemented as a linked list with items added and removed from opposite sides: first–>1–>2–>…–>last–>null.

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

Implement add(item) (Linked Lists)

A

Create new node. If last not null, last.next is new node. Last becomes new node. If first is null, first becomes last.

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

Implement remove() (Linked List)

A

Ensure queue is not empty. Collect first.data. Set first equal to first.next. If first is now null, set last to null. Return collected data.