0% found this document useful (0 votes)
13 views35 pages

Stack

Uploaded by

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

Stack

Uploaded by

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

a

OBJECTIVES

Describe the Implement a basic Recognize the


fundamental stack using an importance of the
concepts of a stack array or linked list stack data structure in
solving problems.
data structure in code
DEFINITION
What is a stack?
DEFINITION OF STACK
is a linear data structure in which insertions and
deletions are allowed only at the end, called the
top of the stack.
Stack of
coins

Pile of books
Pile of Stack of
books coins
Both of these examples have two things in common
1. Any object (either book or coin) can be accessed only from the top
2. Any object can be added only at the top
DEFINITION OF STACK
❑is a linear data ❑To implement the stack,
structure based on it is required to
LIFO(Last In First Out) maintain the pointer to
principle in which the the top of the stack ,
insertion of a new which is the last element
element and removal of
an existing element to be inserted because
takes place at the same we can access the
end represented as the elements only on the top
top of the stack. of the stack.
REPRESENTATION OF STACK
❑Stack follows LIFO
(Last In First Out)
Principle so the
element which is
pushed last is popped
first.
ADVANTAGES AND DISADVANTAGES OF STACK
ADVANTAGES DISADVANTAGES
❑ Simple ❑ Manipulation is
Implementation restricted to the top
❑ Efficient Memory of the stack
Usage: ❑ Not much flexible
❑ Useful for
Algorithmic Problems
APPLICATIONS OF STACK DATA STRUCTURE:
❑ Redo-undo features at many places like editors,
photoshop.
❑ Forward and backward features in web browsers
❑ In Memory management, any modern computer
uses a stack as the primary management for a
running purpose. Each program that is running in a
computer system has its own memory allocations.
TYPES OF STACK
Fixed Size Stack Dynamic Size Stack
❑ If the stack is full and an ❑ When the stack is full, it
attempt is made to add automatically increases its
an element to it, an size to accommodate the new
overflow error occurs.
element, and when the stack is
❑ If the stack is empty and empty, it decreases its size.
an attempt is made to
remove an element from ❑ This type of stack is
it, an underflow error implemented using a linked
occurs.
list, as it allows for easy
resizing of the stack.
BASIC OPERATIONS ON STACK
❑ push() to insert an element into the stack
❑ pop() to remove an element from the stack
❑ top() Returns the top element of the stack.
❑ isEmpty() returns true if stack is empty else false.
❑ isFull() returns true if the stack is full else false.
BASIC OPERATIONS ON STACK
❑ peek(): It returns the element at the given position.
❑ count(): It returns the total number of elements
available in a stack.
❑ change(): It changes the element at the given position.
❑ display(): It prints all the elements available in the stack.
PUSH OPERATION
Adds an item to the
stack. If the stack is
full, then it is said to
be an overflow
condition.
ALGORITHM FOR
PUSH OPERATION
❑ Before pushing the element to
the stack, we check if the stack
is full .
❑ If the stack is full (top ==
capacity-1) , then Stack
overflows and we cannot insert
the element to the stack.
❑ Otherwise, we increment the
value of top by 1 (top = top +
1) and the new value is
inserted at top position .
❑ The elements can be pushed
into the stack till we reach the
capacity of the stack.
POP OPERATION
Removes an item from
the stack. The items
are popped in the
reversed order in
which they are pushed.
If the stack is empty,
then it is said to be an
underflow condition.
ALGORITHM FOR
POP OPERATION
❑ Before popping the element
from the stack, we check if
the stack is empty.
❑ If the stack is empty (top
== -1), then Stack
underflows and we cannot
remove any element from
the stack.
❑ Otherwise, we store the
value at top, decrement the
value of top by 1 (top =
top – 1) and return the
stored top value.
TOP OR PEEK
OPERATION
Returns the top element of the
stack.
Algorithm for Top Operation:
Top Element
❑ Before returning the top element = 40
from the stack, we check if the
stack is empty.
❑ If the stack is empty (top == -1),
we simply print “Stack is empty”.
❑ Otherwise, we return the element
stored at index = top .
isEmpty OPERATION
Returns true if the stack is
empty, else false.
Algorithm for isEmpty Operation:
❑ Check for the value of top in
stack.
❑ If (top == -1) , then the stack is
empty so return true .
❑ Otherwise, the stack is not
empty so return false . isEmpty= isEmpty=
false true
isFull OPERATION
Returns true if the stack is
full, else false.
Algorithm for isFull Operation:
❑ Check for the value of top in
stack.
❑ If (top == capacity-1), then the
stack is full so return true.
❑ Otherwise, the stack is not full
so return false. isFull = isFull=
true false
IMPLEMENTATION OF STACK DATA STRUCTURE:
There are two ways to implement a stack –
❑Using Array
❑Using Linked List
ARRAY-BASED IMPLEMENTATION
❑ The push operation is implemented by
incrementing the index of the top element
and storing the new element at that index.
❑ The pop operation is implemented by
returning the value stored at the top index
and then decrementing the index of the top
element.
IMPLEMENTATION OF STACK USING ARRAY:
IMPLEMENTATION OF STACK USING ARRAY:
ADVANTAGES OF ARRAY IMPLEMENTATION:
❑ Easy to implement.
❑ Memory is saved as pointers are not involved
DISADVANTAGES OF ARRAY IMPLEMENTATION:
❑ It is not dynamic i.e., it doesn’t grow and
shrink depending on needs at runtime.
❑ The total size of the stack must be defined
beforehand.
LINKED LIST-BASED IMPLEMENTATION
❑ The push operation is implemented by
creating a new node with the new element
and setting the next pointer of the current top
node to the new node.
❑ The pop operation is implemented by setting
the next pointer of the current top node to
the next node and returning the value of the
current top node.
IMPLEMENTATION OF STACK USING LINKED LIST:
IMPLEMENTATION OF STACK USING LINKED LIST:
ADVANTAGES OF LINKED LIST IMPLEMENTATION:
❑ The linked list implementation of a stack can
grow and shrink according to the needs at
runtime.
❑ It is used in many virtual machines like Java
Virtual Machine (JVM).
DISADVANTAGES OF LINKED LIST IMPLEMENTATION:
❑ Requires extra memory due to the
involvement of pointers.
❑ Random accessing is not possible in stack.
OUTPUT #2: PALINDROME CHECKER
Implement or create a simple program
that uses a stack to check if a given
word is a palindrome. This involves
pushing characters onto a stack and
then popping them to compare with the
original sequence.
PALINDROME
is a word, phrase, number, or sequence of characters that
reads the same backward as forward, ignoring spaces,
punctuation, and capitalization. The symmetry in a
palindrome means that if you reverse the order of
characters, it still matches the original sequence.
Examples of Palindromes:
Words: "madam", "racecar", "level“
Numbers: "121", "12321", "4554"
OUTPUT #2: PALINDROME CHECKER

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy