1/29
Flashcards covering key concepts and methods related to Stacks and Queues.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What principle does a stack use for organizing its elements?
Last-In-First-Out (LIFO)
What operation is used to add an element to a stack?
push
What operation is used to remove an element from a stack?
pop
What method would you use to check if a stack is empty?
empty()
How do you access the top element of a stack without removing it?
peek()
What is the method used for removing and getting the top element from a stack?
pop()
What is the method to insert an item onto the top of a stack?
push(E item)
What does the search(Object o) method return in a stack?
The 1-based position of the object in the stack.
What is the main characteristic of a queue?
First-In-First-Out (FIFO) order.
What operation adds an element to the end of a queue?
enqueue
What operation removes an element from the front of a queue?
dequeue
Which Java class is used to implement a queue in this lecture?
LinkedList
What method can you use to retrieve, but not remove, the head of a queue?
peek()
What happens if you call add(E e) when a queue is full?
It throws an IllegalStateException.
What method retrieves and removes the head of a queue?
poll()
What principle does a stack use for organizing its elements?
Last-In-First-Out (LIFO). An example is a stack of plates; the last plate added is the first one to be removed.
What operation is used to add an element to a stack?
push. For instance, pushing an item onto a stack of books.
What operation is used to remove an element from a stack?
pop. For example, popping the top book off a stack.
What method would you use to check if a stack is empty?
empty(). This checks if there are any elements in the stack.
How do you access the top element of a stack without removing it?
peek(). For example, you can peek at the top book without taking it off the stack.
What is the method used for removing and getting the top element from a stack?
pop(). This method allows you to retrieve and remove the top element of the stack.
What is the method to insert an item onto the top of a stack?
push(E item). For instance, push a number onto a stack of numbers.
What does the search(Object o) method return in a stack?
The 1-based position of the object in the stack. For example, if searching for 'A' in a stack containing [B, A, C], it returns 2.
What is the main characteristic of a queue?
First-In-First-Out (FIFO) order. An example is a line of people; the first person in line is the first to be served.
What operation adds an element to the end of a queue?
enqueue. For example, adding a person to the end of a line.
What operation removes an element from the front of a queue?
dequeue. For instance, the first person in line is removed from the queue.
Which Java class is used to implement a queue in this lecture?
LinkedList. This class provides the functionality needed for queue operations.
What method can you use to retrieve, but not remove, the head of a queue?
peek(). This allows you to see who is at the front of the line without serving them.
What happens if you call add(E e) when a queue is full?
It throws an IllegalStateException. This occurs when trying to add an item to a capacity-restricted queue.
What method retrieves and removes the head of a queue?
poll(). This method serves the person