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

What Is A Stack

The document discusses stacks and their basic operations. It explains what a stack is, the core push, pop, peek, is empty and is full operations, and provides examples of push, pop and peek operations. It also covers applications of stacks such as expression evaluation, undo/redo operations, function call management, and backtracking algorithms.

Uploaded by

IT LAb 2
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

What Is A Stack

The document discusses stacks and their basic operations. It explains what a stack is, the core push, pop, peek, is empty and is full operations, and provides examples of push, pop and peek operations. It also covers applications of stacks such as expression evaluation, undo/redo operations, function call management, and backtracking algorithms.

Uploaded by

IT LAb 2
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Stack and its basic

operations
Presented By : Ali Hamza
(Mis Id:39369)
Department Of Computer Science
(Date 21/05/2024)

Presented To : Mr. Muhammad Adil


Federal Urdu University of Arts, Science & Technology, Islamabad-Pakistan
What is Stack?
A stack is a fundamental data structure in computer
science that follows the Last-In-First-Out (LIFO) principle. It
is a linear data structure where elements are added and
removed from the top of the stack.
Basic Operations on a Stack

Push Pop
Adding a new element to the top of Removing the top element from the
the stack, increasing the stack size by stack, decreasing the stack size by
one. one.

Peek Is Full
Accessing the top element of the Checking if the stack has reached its
stack without removing it. maximum capacity.
Push Operation

Add Element
To push an element onto the stack, add the new element to the top of the
stack, increasing the stack size by one.

Update Top
The new element becomes the new top of the stack, and all other elements
remain in their original order below it.

Increment Size

The size of the stack is incremented by one to reflect the addition of the new element.
Pop Operation

1 Remove Top Element


To pop an element from the stack, remove the top-most element.

Update Top
2
The element below the removed one now becomes the
new top of the stack.

Decrement Size

3 The size of the stack is decremented by


one to reflect the removal of the
element.

The pop operation in a stack removes the top-most element, making the element below it
the new top of the stack. This reduces the size of the stack by one, maintaining the Last-In-
First-Out (LIFO) principle.
Peek Operation

1 Access Top Element


The peek operation allows you to view the top element of the stack
without removing it.

2 No Changes to Stack
Peeking does not modify the stack - the size and order of elements remain
unchanged.

3 Useful for Inspection


Peeking is often used to check the value at the top of the stack before
performing other operations.
Is Full Operation

Check Capacity
1 Determine if the stack has reached its maximum size.

Return True/False

2 The operation returns a boolean value


indicating if the stack is full.

Prevent Overflow
3 Helps avoid adding more elements than
the stack can hold.

The is full operation is a crucial check in stack data structures. It allows you to determine if
the stack has reached its maximum capacity, preventing the addition of new elements that
would exceed the allotted memory. By returning a boolean value, this operation informs the
program whether the stack is currently full or has available space to accommodate new push
operations.
Is Empty Operation

Check Size
1
Determine if the stack is currently empty.

Return True/False
2 The operation returns a boolean value indicating if the
stack is empty.

Avoid Underflow
3
Helps prevent attempting to pop from
an empty stack.

The is empty operation is an essential check in stack data structures. It allows you to quickly
determine if the stack currently contains any elements or if it is in an empty state. By
returning a boolean value, this operation informs the program whether the stack can safely
perform pop or other operations without causing an underflow error.
Stack ADT (Abstract Data Type)

Definition Core Operations Encapsulation Language


Independent
A Stack ADT The key The Stack ADT
defines the operations in a encapsulates the The Stack ADT
standard set of Stack ADT are internal is language
operations and push, pop, peek, implementation independent
behaviors for a is empty, and is details, allowing it to be
stack data full. These allow presenting a implemented in
structure, control over high-level API various
providing a adding, that hides the programming
consistent and removing, and complexity of languages while
reusable inspecting the underlying maintaining
interface. elements. data structure. consistent
behavior.
Applications of Stacks

1 Expression Evaluation 2 Undo/Redo Operations


Stacks are used to evaluate Stacks store the history of user
mathematical expressions, ensuring actions, allowing for efficient
the correct order of operations is implementation of undo and redo
followed. functionality.

3 Function Call Management 4 Backtracking Algorithms


Stacks keep track of function calls Stacks facilitate backtracking in
and their return addresses, enabling algorithms, allowing for efficient
the call stack mechanism in exploration of decision trees and
programming. problem-solving.
Conclusion
In conclusion, the stack data structure is a fundamental concept in
computer science that provides a powerful and efficient way to manage
data. By understanding the core operations of push, pop, and peek, as well
as the is full and is empty checks, developers can harness the flexibility and
simplicity of the stack to solve a variety of problems.

You might also like