Stacks and Queues

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/29

flashcard set

Earn XP

Description and Tags

Flashcards covering key concepts and methods related to Stacks and Queues.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

30 Terms

1
New cards

What principle does a stack use for organizing its elements?

Last-In-First-Out (LIFO)

2
New cards

What operation is used to add an element to a stack?

push

3
New cards

What operation is used to remove an element from a stack?

pop

4
New cards

What method would you use to check if a stack is empty?

empty()

5
New cards

How do you access the top element of a stack without removing it?

peek()

6
New cards

What is the method used for removing and getting the top element from a stack?

pop()

7
New cards

What is the method to insert an item onto the top of a stack?

push(E item)

8
New cards

What does the search(Object o) method return in a stack?

The 1-based position of the object in the stack.

9
New cards

What is the main characteristic of a queue?

First-In-First-Out (FIFO) order.

10
New cards

What operation adds an element to the end of a queue?

enqueue

11
New cards

What operation removes an element from the front of a queue?

dequeue

12
New cards

Which Java class is used to implement a queue in this lecture?

LinkedList

13
New cards

What method can you use to retrieve, but not remove, the head of a queue?

peek()

14
New cards

What happens if you call add(E e) when a queue is full?

It throws an IllegalStateException.

15
New cards

What method retrieves and removes the head of a queue?

poll()

16
New cards

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.

17
New cards

What operation is used to add an element to a stack?

push. For instance, pushing an item onto a stack of books.

18
New cards

What operation is used to remove an element from a stack?

pop. For example, popping the top book off a stack.

19
New cards

What method would you use to check if a stack is empty?

empty(). This checks if there are any elements in the stack.

20
New cards

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.

21
New cards

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.

22
New cards

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.

23
New cards

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.

24
New cards

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.

25
New cards

What operation adds an element to the end of a queue?

enqueue. For example, adding a person to the end of a line.

26
New cards

What operation removes an element from the front of a queue?

dequeue. For instance, the first person in line is removed from the queue.

27
New cards

Which Java class is used to implement a queue in this lecture?

LinkedList. This class provides the functionality needed for queue operations.

28
New cards

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.

29
New cards

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.

30
New cards

What method retrieves and removes the head of a queue?

poll(). This method serves the person