Stacks Flashcards

1
Q

What is a stack?

A

A stack is an abstract data type that follows the Last In, First Out (LIFO) principle. It is a collection of elements where the addition of new elements and the removal of existing elements occur only at one end, called the top. The element that was added last is the first one to be removed.

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

How does the Last In, First Out (LIFO) principle work in a stack?

A

The Last In, First Out (LIFO) principle means that the most recently added element to the stack is the first one to be removed. When new elements are added to the stack, they are placed on top, and when elements are removed, they are always taken from the top.

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

What are the two primary operations performed on a stack?

A

The two primary operations performed on a stack are:

Push: This operation adds an element to the top of the stack.
Pop: This operation removes the element from the top of the stack.

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

Can elements be inserted or removed from the middle of a stack interface?

A

No, a stack interface follows the Last In First Out (LIFO) principle, which means elements can only be inserted or removed from the top of the stack. It does not support operations to insert or remove elements from the middle of the stack

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

Where is a stack interface commonly used?

A

Stack interfaces are commonly used in programming and software development. They are particularly useful in scenarios where the order of operations is important, such as expression evaluation, function calls, undo/redo functionality, and backtracking algorithms.

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

What is the behavior of the isEmpty operation in a stack interface?

A

The isEmpty operation in a stack interface checks whether the stack contains any elements and returns a boolean value based on the result.

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

What does the isEmpty operation return?

A

The isEmpty operation returns true if the stack has zero elements, indicating that it is empty. If the stack contains one or more elements, the isEmpty operation returns false, indicating that the stack is not empty.

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

How can the isEmpty operation be used in practice?

A

The isEmpty operation is often used to check the state of a stack before performing certain operations. For example, if you want to implement an undo feature, you can use the isEmpty operation to determine if there are any actions to be undone. If the stack is empty, it means there is nothing to undo. Similarly, the isEmpty operation can be used to check if a stack is empty before executing a pop operation to avoid errors.

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

Can the isEmpty operation be used on other data structures?

A

The isEmpty operation is specific to the stack data structure and may not be available in other data structures. However, other data structures may provide similar operations with different names to check if they are empty, such as size() returning zero or hasNext() returning false.

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

Is the isEmpty operation efficient?

A

The isEmpty operation in a stack interface typically has a constant time complexity of O(1). It does not depend on the number of elements in the stack and can quickly determine if the stack is empty or not.

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

What is the behavior of the “push” operation in a stack data structure?

A

The “push” operation adds an item to the top of the stack.

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

What is the behavior of the “pop” operation in a stack data structure?

A

The “pop” operation removes an item from the top of the stack.

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

What is the behavior of the “peek” operation in a stack data structure?

A

The “peek” operation returns the top element of the stack without removing it.

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

What are the two commonly used implementations for a Stack data structure?

A

The two commonly used implementations for a Stack data structure are using an array/ArrayList or a linked list.

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

What are the advantages of implementing a Stack using an array/ArrayList?

A

Some advantages of using an array/ArrayList for implementing a Stack are:

Simplicity and ease of implementation.
Direct access to elements allows efficient operations.
Push and pop operations can be performed in constant time (O(1)).
Memory-efficient as no extra memory is needed for storing pointers or references.

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

What are the advantages of implementing a Stack using a linked list?

A

Some advantages of using a linked list for implementing a Stack are:

Dynamic memory allocation, allowing flexibility in size.
No predefined size limit, as memory is allocated dynamically.
Insertion and deletion operations can be performed in constant time (O(1)).
Efficient use of memory as nodes can be allocated and deallocated as needed.

17
Q

What are the considerations when implementing a Stack using an array/ArrayList?

A

Considerations when implementing a Stack using an array/ArrayList include:

Predefining the size or dynamically resizing the array when needed.
Costly resizing operation if the stack exceeds the current capacity.
Inefficient memory utilization if the stack size fluctuates significantly.

18
Q

What are the considerations when implementing a Stack using a linked list?

A

Considerations when implementing a Stack using a linked list include:

Extra memory required for storing pointers or references to the next node.
Additional time complexity due to dynamic memory allocation and deallocation.
Slightly slower access times compared to arrays due to the indirect nature of accessing elements through pointers.