Queue And Stack Flashcards

1
Q

What is a Queue?

A

Is a first in first out (FIFO) data structure

  • First one added is the first one out
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a Stack?

A

Is a last in first out (LIFO) data structure

  • Last one added is the first one out
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Show me how to implement a Queue

A

import java.util.LinkedList;
import java.util.Queue;

Queue<Integer> queue = new LinkedList<>();</Integer>

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

Show me how to implement a Stack

A

import java.util.Stack;

Stack<Integer> stack = new Stack<>();</Integer>

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

Difference between a queue and a priority queue?

A

Queue is based on first in first out, elements are added to the queue based on the insertion

Priority queue sorts them in some way

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

What helper functions are there for a Queue?

A

offer(E e)
poll()
peek()
remove()
element()

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

What does offer(E e) do for a queue?

A

Adds an element to the queue, returns true or false

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

What does poll() do for a queue?

A

Retrieves and removes the head of the queue, returns null if empty

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

What does peek() do for a queue?

A

Retrieves but does not remove the head of the queue

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

What does remove() do for a queue?

A

Retrieves and removes the head of the queue

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

What does element() do for a queue?

A

Retrieves but does not remove the head of the queue, throws exception if empty

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

What are the helper functions for a stack?

A

push(E e)
pop()
peek()
empty()
search(Object o)

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

What does push(E e) do for a stack?

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

What does pop() do for a stack?

A

Removes and returns the top element

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

What does peek() do for a stack?

A

Returns the top element without removing it

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

What does empty() do for a stack?

A

Checks if the stack is empty

17
Q

What does search(Object o) do for a stack?

A

Returns position of the element or -1 if not found