0% found this document useful (0 votes)
6 views

Abstract Data Types (ADT's) (Stack) - Anqa#11Green

Uploaded by

Anqa Rehman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Abstract Data Types (ADT's) (Stack) - Anqa#11Green

Uploaded by

Anqa Rehman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

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:-
The two primary operations on a stack are "push,"
1.Pop() which adds an element to the top of the stack,
and "pop," which removes the top element.
2.Push()
The top of the stack is the most
3.Top() recently added element.
4.Empty() 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.
5.Size() 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
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: Disadvantages of Stack:
1.Easy Concept: Stacks are simple 1.Limited Access: You can only
to understand because you only add get to the top item. Accessing
or remove items from the top. other items is harder.
2.Memory Management: They help 2.Size Limits: Some stacks have
manage memory efficiently,
a maximum size. If you exceed it,
especially for function calls and local
variables. you can have problems.
3.Tracking Flow: Stacks are good 3.Not for Everything: Stacks
for keeping track of where you are in are great for certain tasks, but
a program, like with undo actions. not always the best choice.
4.Checking Expressions: They're 4.Complex Recursion:
useful for checking if parentheses or Sometimes deep recursion can
brackets in expressions are balanced. 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+1 Top Top-1
Stack 2 [Top] value

You might also like