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

Week 4

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

Week 4

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

STACK

S BY
AMBER MURTAZA
Introduction
Stacks in Data Structures is a linear type of data
structure that follows the LIFO (Last-In-First-Out)
principle and allows insertion and deletion
operations from one end of the stack data
structure, that is top. Implementation of the stack
can be done by contiguous memory which is an
array, and non-contiguous memory which is a linked
list.
Cont.
• It contains only one pointer top pointer pointing to
the topmost element of the stack.
• Whenever an element is added in the stack, it is
added on the top of the stack, and the element
can be deleted only from the stack.
• In other words, a stack can be defined as a container
in which insertion and deletion can be done from
the one end known as the top of the stack.
Real-life
Examples
• It is named stack as it behaves like a real-world
stack, for example – a deck of cards, a pile of
plates, piles of books, piles of money, and many
more.
Cont.
These examples allows you to perform operations
from one end only, like when you insert and remove
new books from the top of the stack. It means
insertion and deletion in the stack data structure
can be done only from the top of the stack. You can
access only the top of the stack at any given point
in time.
• Inserting a new element in the stack is termed a
push operation.
• Removing or deleting elements from the stack is
termed pop operation.
Why we use
Stack?
In general, stacks are useful for processing nested
structures or for functions which call other
functions (or themselves). A nested structure is one
that can contain instances of itself embedded
within itself. For example, algebraic expressions
can be nested because a sub expression of an
algebraic expression can be another algebraic
expression. Stacks are used to implement
functions, parsers, expression evaluation, and
backtracking algorithms.
Some key points related to
•stack
It is called as stack because it behaves like a real-
world stack, piles of books, etc.
• A Stack is an abstract data type with a pre-defined
capacity, which means that it can store the
elements of a limited size.
• It is a data structure that follows some order to
insert and
delete the elements, and that order can be LIFO or
FILO.
Working of
Stack
As we can observe in the below figure there are five
memory blocks in the stack; therefore, the size of
the stack is 5. Suppose we want to store the
elements in a stack and let's assume that stack is
empty. We have taken the stack of size 5 as shown
below in which we are pushing the elements one by
one until the stack becomes full.
Cont.
• Since our stack is full as the size of the stack is 5.
• In the above cases, we can observe that it goes
from the top to the bottom when we were entering
the new element in the stack.
• The stack gets filled up from the bottom to the top.
Cont.
• When we perform the delete operation on the
stack, there is only one way for entry and exit as
the other end is closed.
• It follows the LIFO pattern, which means that the
value entered first will be removed last.
• In the above case, the value 1 at position 5 is
entered first, so it will be removed only after the
deletion of all the other elements.
Basic Operations on
Stackfollowing are some operations
There that are
implemented on the stack.
• push(): When we insert an element in a stack then
the operation is known as a push. If the stack is
full then the overflow condition occurs.
• pop(): When we delete an element from the stack,
the operation is known as a pop. If the stack is
empty means that no element exists in the stack,
this state is known as an underflow state.
• isEmpty(): It determines whether the stack is empty
or not.
Cont.
• isFull(): It determines whether the stack is full or
not.
• peek(): is used to retrieve or fetch the first element
of the Stack or the element present at the top of
the Stack. The element retrieved does not get
deleted or removed from the Stack.
• 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
The steps involved in the PUSH operation is
given below:
• Before inserting an element in a stack, we check
whether the stack is full.
• If we try to insert the element in a stack, and the
stack is full, then the overflow condition occurs.
• When we initialize a stack, we set the value of top
as -1 to check that the stack is empty.
Cont
• When the new element is pushed in a stack, first,
. the value of the top gets incremented, i.e.,
top=top+1, and the element will be placed at the
new position of the top.
• The elements will be inserted until we reach the
max size
of the stack.
How you implement a push
operation? void push(int item)
{
if (!isFull())
{
top = top + 1;
stack[top] = item;
}
else {
cout << "stack is full"
<<endl; }
POP
Operation
The steps involved in the POP operation is
given below:
• Before deletingthe elementfrom the stack, we
check whether the stack is empty.
• If we try to delete the element from the empty
stack, then the underflow condition occurs.
• If the stack is not empty, we first access
the element which is pointed by the top.
Cont.
• Once the pop operation is the top
performed, decremented by 1, i.e., is
top=top-1.
Implementing a POP operation is
as follows:
int pop(int data) {

if(!isempty()) {

data =

stack[top]; top

= top - 1;

return data;

else {

cout<<"Could not retrieve


data, Stack is empty<<endl;
Implementation of the peek operation
is:
int peek()

return
stack[top];

}
The implementation the
of function is as isFull()
follows: Bool isFull()

if(top ==

maxsize)

return true;
else

return false;

}
The implementation of the
isEmpty() function is:
Bool
isEmpty()

if(top = =

-1) return

true; else

return
false;

}
Stack in data structures using an
array
Stack in data structures using
linked list

You might also like