Data Structure (Stack)
Data Structure (Stack)
1. Stack
2. Operations on stack (push, pop, peek, display)
3. Implementation of stack using list
4.Applications of stack
102 | P a g e
DATA STRUCTURE
Data structure can be defined as a set of rules and operations to organize and store data in an
efficient manner. We can also say that it is a way to store data in a structured way. We can
apply different operations like reversal, slicing, counting etc. of different data structures.
Hence, Data Structure is a way to organize multiple elements so that certain operations can
be performed easily on whole data as a single unit as well as individually on each element
also.
In Python, Users are allowed to create their own Data Structures which enable them to define
the functionality of created data structures. Examples of User Defined data structures in
Python are Stack, Queue, Tree, Linked List etc. There are some built-in data structures also
available in Python like List, Tuple, Dictionary and Set.
STACK:
A Stack is a Linear data structure which works in LIFO (Last In First Out) manner (or we can say
FILO i.e. First In Last Out manner). It means that Insertion and Deletion of elements will be
done only from one end generally known as TOP only. In Python, we can use List data
structure to implement Stack.
103 | P a g e
Application of Stack:
1. Expression Evaluation
2. String Reversal
3. Function Call
4. Browser History
5. Undo/Redo Operations
Operations of Stack:
The Stack supports following operations:
OVERFLOW: It refers to the condition in which we try to PUSH an item in a Stack which is
already FULL.
UNDERFLOW: It refers to the condition in which we are trying to POP an item from an
empty Stack.
104 | P a g e
Implementation of Stack:
In Python, List data structure is used to implement Stack. For PUSH operation we use append()
method of List while for POP operation we use pop() method of List.
PROGRAM: To illustrate the basic operations of Stack:
105 | P a g e
named “Hotel”:
i. Push_Cust() - To Push customer’s names of those customers who are staying in ‘Delux’ Room
Type.
ii. Pop_Cust() - To Pop the names of Customers from the stack and display them. Also, display
“Underflow” when there are no customers in the stack.
For example: If the list with customer details are as follows:
[“Siddarth”, “Delux”]
[“Rahul”, ”Standard”]
[“Jerry”, “Delux”]
The stack should contain:
Jerry
Siddharth
The output should be:
Jerry
Siiddharth
Underflow
3. Write a function, Push (Vehicle) where, Vehicle is a dictionary containing details of vehicles
– {Car_Name: Maker}. The function should push the name of car manufactured by ‘TATA’
(including all the possible cases like Tata, TaTa, etc.) to the stack. For example:
Safari
106 | P a g e
QUES: A list, NList, contains following record as list elements:
Each of these records are nested together to form a nested list. Write the following user
defined functions in Python to perform the specified operations on the stack named travel.
i. Push_element(NList): It takes the nested list as an argument and pushes a list object
containing name of the city and country, which are not in India and distance is less
than 3500 km from Delhi.
ii. Pop_element(): It pops the objects from the stack and displays them. Also, the function
should display “Stack Empty” when there are no elements in the stack.
For example: If the nested list contains the following data:
NList=[[“Newyork”,”USA”,11734], [“Naypyidaw”, “Myanmar”,3219],
[“Dubai”,”UAE”,2194], [“London”,”England”,6693], [“Gangtok”, “India”,1580], [“Columbo”,
”Sri Lanka”, 3405]]
The stack should contain:
[“Naypyidaw”, “Myanmar”,3219], [“Dubai”,”UAE”,2194], [“Columbo”, ”Sri Lanka”, 3405]
The output should be:
[“Columbo”, ”Sri Lanka”, 3405]
[“Dubai”,”UAE”,2194]
[“Naypyidaw”, “Myanmar”,3219]
Stack Empty [CBSE SQP 2023]
107 | P a g e
Ques: Write a function in Python, Push(SItem) where, SItem is a dictionary containing the
details of stationary items—{Sname:price}. The function should Push the names of those
items in the stack who have price greater than 75. Also display the count of elements
pushed into the stack.
For example:
If th dictionary contains the following data:
SItem: {‘Pen’: 106, ‘Pencil’: 59, ‘Notebook’: 80, ‘Eraser’:25}
The stack should contain:
Notebook
Pen
The output should be : The count of elements in the stack is 2. [CBSE SQP 2022]
108 | P a g e