LIFO and FIFO:
- LIFO: Stands for Last In, First Out. The last element added to a collection is the first one to be removed. Think of it like a stack of plates โ you remove the top one first.
- FIFO: Stands for First In, First Out. The first element added is the first one to be removed. It's like a queue of people waiting in line โ the person who arrives first is the first to leave.
Stack:
- Definition: A stack is a data structure that follows the LIFO principle. Elements are added to and removed from the top of the stack.
- Use cases: Use a stack when you need to keep track of function calls (call stack in recursion), undo mechanisms, or in parsing expressions (like in a calculator).
- Common implementations: Arrays and linked lists are common implementations of stacks.
Queue:
- Definition: A queue is a data structure that follows the FIFO principle. Elements are added at the rear (enqueue) and removed from the front (dequeue) of the queue.
- Use cases: Use a queue when you want to maintain the order of elements and process them in a first-come, first-served manner. Examples include task scheduling, print queue management, or breadth-first search algorithms.
- Common implementations: Similar to stacks, arrays and linked lists are commonly used to implement queues. There are also specialized implementations like priority queues.
Common Use Cases:
- Stacks: Function calls (call stack), undo mechanisms, expression parsing, backtracking algorithms.
- Queues: Task scheduling, print job management, breadth-first search, handling requests in a web server.
Global Variables:
- Proper Usage: Using global variables should be minimized due to the potential for unintended side effects and difficulties in debugging and maintaining code. If you must use global variables, consider the following:
- Clearly document the variables and their purpose.
- Avoid using global variables for mutable state if possible.
- Use all capital letters for global constants to distinguish them from regular variables.
- Be cautious of unintended interactions with other parts of the code.
Remember, excessive use of global variables can make code less modular and harder to understand. It's often better to pass necessary variables explicitly through function parameters or encapsulate them in appropriate data structures.