Queue And Stack Flashcards
What is a Queue?
Is a first in first out (FIFO) data structure
- First one added is the first one out
What is a Stack?
Is a last in first out (LIFO) data structure
- Last one added is the first one out
Show me how to implement a Queue
import java.util.LinkedList;
import java.util.Queue;
Queue<Integer> queue = new LinkedList<>();</Integer>
Show me how to implement a Stack
import java.util.Stack;
Stack<Integer> stack = new Stack<>();</Integer>
Difference between a queue and a priority queue?
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
What helper functions are there for a Queue?
offer(E e)
poll()
peek()
remove()
element()
What does offer(E e) do for a queue?
Adds an element to the queue, returns true or false
What does poll() do for a queue?
Retrieves and removes the head of the queue, returns null if empty
What does peek() do for a queue?
Retrieves but does not remove the head of the queue
What does remove() do for a queue?
Retrieves and removes the head of the queue
What does element() do for a queue?
Retrieves but does not remove the head of the queue, throws exception if empty
What are the helper functions for a stack?
push(E e)
pop()
peek()
empty()
search(Object o)
What does push(E e) do for a stack?
What does pop() do for a stack?
Removes and returns the top element
What does peek() do for a stack?
Returns the top element without removing it
What does empty() do for a stack?
Checks if the stack is empty
What does search(Object o) do for a stack?
Returns position of the element or -1 if not found