12 Stack
12 Stack
STACK
CHAP 9
DATA STRUCTURE
Data structure refers to a data
collection with well defined
operations and behavior or
properties.
Stack
• A stack is a linear structure
implemented in LIFO (Last In
First Out) manner where
insertions and deletions are
restricted to occur only at one
end – stack’s top.
Rules to be
followed:
• Data can only be removed
from the top ( pop), i.e., the
element at the top of the
stack. the removal of element
from a stack is technically
called as POP operation.
Rules to be
followed:
• A new element can only
be added to the top of the
stack ( push). the insertion
of element in a stack is
technically called PUSH
operation.
PUSH & POP
Peek
Stack
• Underflow refers to situation (ERROR) when
one tries to pop/delete an item from an
Underflow empty stack.
• That is stack is currently having no item and
still one tries to pop an item
Implementing stack in Python
• Peak
<stack>[top]
• Push
<stack>.append(<item>)
• Pop
<stack>.pop()