05 Data Structures
05 Data Structures
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:
1. Push: It adds an element to the TOP of the Stack.
2. Pop: It removes an element from the TOP of the Stack.
3. Peek: It is used to know/display the value of TOP without removing it.
4. isEmpty: It is used to check whether Stack is empty.
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
Assignment
1 Sanya wants to remove an element from empty stack. Which of the following term is related
to this?
(a) Empty Stack (b) Overflow (c) Underflow (d) Clear Stack
109 | P a g e
ASSERTION (A): A data structure is a named group of data types.
REASON(R): A data structure has a well-defined operations, behaviour and
properties.
● ASSERTION (A): A Stack is a Linear Data Structure, that stores the elements in FIFO
order.
REASON(R): New element is added at one end and element is removed from that end
only.
● ASSERTION (A): An error occurs when one tries to delete an element from an empty
stack.
REASON(R): This situation is called an Inspection.
8 Write a function in Python POPSTACK (L) where L is a stack implemented by a list of numbers.
The function returns the value deleted from the stack.
110 | P a g e
10 Write a function in Python, Push(SItem) where , SItem is a dictionary containing the details
of stationery 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 the dictionary contains the following data:
Ditem={"Pen":106,"Pencil":59,"Notebook":80,"Eraser":25}
The stack should contain:
Notebook
Pen
The output should be:
11 Create a stack that stores dictionaries as elements. Each dictionary represents a person's
information
( name, age, city).
- Implement a `push_dict` method to push a dictionary onto the stack of person above age
20
- Implement a `pop_dict` method to pop the top dictionary from the stack.
- Implement a `push_student` method to push student records onto the stack that GPA
above 60.
- Implement a `pop_student` method to pop the top student record from the stack.
13 Assume a nested dictionary .Each dictionary can contain other dictionaries as values.the
format of the dictionary is as follows:
{1:{‘a’:’one,’b’:’two},2:{‘x’:10},3:{‘y’:100,’z’:200}....}
- Implement a `pop_nested_dict` method to pop the top element from the stack.
for example :
after implementing push_nested_dict() , the stack becomes:
{‘y’:100,’z’:200}
{‘x’:10}
{‘a’:’one,’b’:’two}
111 | P a g e
14 Write the definition of a function POP_PUSH (LPop, LPush, N) in Python. The function should
Pop out the last N elements of the list LPop and Push them into the list LPush. For example:
If the contents of the list LPop are [10, 15, 20, 30]
And value of N passed is 2, then the function should create the list LPush as [30, 20] And the
list LPop should now contain [10, 15]
NOTE: If the value of N is more than the number of elements present in LPop, then display
the message "Pop not possible".
16 Write a function in Python, 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. 3
For example:
If the dictionary contains the following data: Vehicle=("Santro": "Hyundai", "Nexon": "TATA",
"Safari": "Tata"}
Safari
Nexon
112 | P a g e