Abstract Data Types (ADT’s) Stack(LIFO) By: Anqa Rehman 11 Green What is stack? In computer science, a "stack" refers to a linear data structure that follows the Last In First Out (LIFO) principle. It's a collection of elements where you can add or remove elements only from the top, similar to a stack of plates. The element added last is the one that gets removed first. Imagine a stack of plates – you put new plates on top and remove them from the top too. The last plate you put on is the first one you can take off. Operations that can be performed by stack • There are various operations that can be performed in stacks:1. Pop() 2. Push() 3. Top() 4. Empty() 5. Size() The two primary operations on a stack are "push," which adds an element to the top of the stack, and "pop," which removes the top element. The top of the stack is the most recently added element. Empty refers to the state of the stack when it doesn't contain any elements. When a stack is empty, there's nothing to remove from it. This condition is often important to consider when working with stacks to prevent errors like trying to remove elements from an empty stack, which is known as "underflow." In a stack, "size" refers to the number of elements currently stored in the stack. It tells you how many items are in the stack at a given time. Real-World Analogy Imagine you're at a buffet, and there's a stack of trays available for you to use. You can take a tray from the top of the stack and place your food items on it. When you're done with your meal, you return the tray to the top of the stack. 1. Adding Items (Push): When you take a tray from the top of the stack, you're essentially "pushing" a tray onto the stack. The new tray becomes the topmost one. 2. Removing Items (Pop): When you're done with your meal and return your tray, you're "popping" the top tray off the stack. The tray you took last is the one you return first. 3. Last In First Out (LIFO): Just like in a stack data structure, the last tray you put on the stack is the first one you'll remove. It's similar to how the last plate you put on a stack of plates is the first one you'll take off. 4. Tray Count (Size): The number of trays in the stack at any given time represents the size of the stack. If there are no trays, the stack is empty; if there are too many trays to fit, you could encounter an overflow situation. 5. Empty Stack: If there are no trays left in the stack, you can't pop off any more trays. Similarly, in programming, trying to pop from an empty stack can lead to an underflow situation. LIFO, Usage and Implementation • LIFO: The Last In First Out principle means that the last element added is the first one to be removed. • Usage: Stacks are used to manage function calls and local variables in computer programs, undo/redo functionality in applications, expression evaluation (e.g., evaluating arithmetic expressions), and maintain state in various algorithms. • Implementation: Stacks can be implemented using arrays or linked lists. Arrays are simpler but might have size limitations, while linked lists can handle dynamic sizes efficiently. Some applications of stack 1.Memory Management: Stacks are used for managing memory allocation and deallocation in various programming languages, helping to keep track of memory blocks that can be used and released. 2.Compiler and Interpreter Design: Stacks play a crucial role in parsing and evaluating code in compilers and interpreters. They assist in tracking variables, scope, and execution flow. 3.Browser History: As mentioned earlier, stacks are used to manage browser history, enabling users to navigate back and forth through visited web pages. 4.Function Call Management: Stacks are used to manage function calls and their local variables in programming languages. When a function is called, its information is pushed onto the stack. When the function completes, its information is popped, and the program resumes where it left off. 5.Undo/Redo Functionality: Many software applications provide undo and redo features. Stacks can be used to store states of the application, enabling users to undo actions by popping from one stack and redo actions from another. Overflow and Underflow in stack • Stack overflow occurs when you try to add an element to a full stack, and stack underflow happens when you try to remove an element from an empty stack. • Overflow: Stack overflow occurs when you try to add an element to a stack that has reached its maximum capacity. It's like trying to add more plates to a stack than it can hold. This can lead to unpredictable behavior or errors in a program. To prevent overflow, programmers often implement checks to ensure that the stack doesn't exceed its capacity. • Underflow: Stack underflow happens when you try to remove an element from an empty stack. It's like trying to take a plate from an empty stack of plates. This can also result in errors or unexpected outcomes in your program. To avoid underflow, programmers typically check whether the stack is empty before attempting to remove an element. • In both cases, handling overflow and underflow situations is crucial to writing reliable and robust code when using stacks. Proper error-checking and handling mechanisms ensure that your programs gracefully handle these scenarios and avoid unexpected issues. Advantages and Disadvantages of stack Advantages of using stack: 1.Easy Concept: Stacks are simple to understand because you only add or remove items from the top. 2.Memory Management: They help manage memory efficiently, especially for function calls and local variables. 3.Tracking Flow: Stacks are good for keeping track of where you are in a program, like with undo actions. 4.Checking Expressions: They're useful for checking if parentheses or brackets in expressions are balanced. Disadvantages of Stack: 1.Limited Access: You can only get to the top item. Accessing other items is harder. 2.Size Limits: Some stacks have a maximum size. If you exceed it, you can have problems. 3.Not for Everything: Stacks are great for certain tasks, but not always the best choice. 4.Complex Recursion: Sometimes deep recursion can cause issues with stacks. Key Points: 1. If stack is already empty and we use pop operation, it will result in a runtime error since no element can be popped out as stack is empty. This is a condition of underflow. 2. If stack is empty, then using top operation will generate a random value since the top of stack does not contain any value. 3. If we push a lot of elements into the stack it will result in stack overflow or overutilization of memory. For insertion: For Removal: Top Top Top+1 Stack 2 [Top] value Top-1