Computer Science with Python Class 12
Data Structures
Meaning of Data Structure
It is a named group of data of same or different data types which is stored in a specific
way and can be processed as a single unit. A data structure has well defined well defined
operations , behaviour and properties.
Data Type and Data Structure
Computer Science Dept. Mount Carmel School AN
Computer Science with Python Class 12
Stack
A stack is a linear data structure whose elements are accessed according to the Last-In
First-Out (LIFO) principle. It means the data element which is inserted or added in the
end, is removed or deleted first of all from the stack. In a stack, insertion and deletion of
elements can only take place at one end, called top of the stack.
Real life example of Stack: Stack of Plates, Stack of Books , Stack of Bangles etc.
Example of Stack in a Computer System/Programming: Undo Operations, Reversing
a string , Function calls etc.
Two main operations are performed on a stack.
1. Push: Inserting or Adding a new data element at the top of the stack. An empty stack
list is initialized with an invalid subscript. When a Push operation is performed, the top
is incremented by one and then the new value is inserted on the top of the list till the
time the value of top is less than or equal to the size of the stack.
2. Pop: Removing or Deleting an existing data element from the top of the stack. It is
verified if the stack is empty or not by checking the value of top. If the value of top is -1,
then the stack is empty and such a situation is called Underflow. Otherwise Pop
operation can be performed in the stack. The top is decremented by one if an element is
deleted from the list.
Computer Science Dept. Mount Carmel School AN
Computer Science with Python Class 12
Overflow: It refers to a situation or condition when the element is tried to push in the
stack but the stack is full. It means there is no place for a new data element.
Underflow: It refers to a situation or condition when the element is tried to pop out of
the stack but the stack is empty. It means there is no element to remove or delete.
Processing of a stack
Computer Science Dept. Mount Carmel School AN