Computer Science (XII) Notes by Sumit Garg Sir
DATA STRUCTURE
Stack: A stack is a data structure whose elements are accessed according to the Last-In First-
Out (LIFO) principle. This is because in a stack, insertion and deletion of elements can only
take place at one end, called top of the stack.
Consider the following examples of stacks:
1. Ten glass plates placed one above another. (The plate that is kept last must be taken out first).
2. The tennis balls in a container. (You cannot remove more than one ball at a time)
3. A pile of books.
4. Stack of coins
The Significance of Stack Top : If we want to remove any coin from the stack, the coin on the
top of the stack has to be removed first. That means, the coin that In the above picture coins are
kept one above the other and if any additional coin is to be added, it can be added only on was
kept last in the stack has to be taken out first.
Note: Stack Work on LIFO (Last In First Out) principle.
Operations on Stack :
We can perform two operations on stack are: Push and Pop
Push (Add/Insert): Adding an element in stack is called Push operation.
When the stack is empty, the value of top is Basically, an empty stack is initialized with an invalid
subscript. Whenever 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.
The algorithm for Push operation on a stack:
1. Start 2. Initialize top with -1 3. Input the new element
4. Increment top by one. 5. Stack[top]=new element. 6. Print “Item inserted” 7. Stop
Pop (Removing (deleting) an element from the stack.
Removing existing elements from the stack list is called pop operation. Here we must check if the
stack is empty 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. The algorithm for pop operation is
as follows:
• Push_element(NList): It takes the item as an argument and pushes it in list using append( ).
• Pop_element(): It pops the objects from the stack and displays them using pop( ). Also, it
displays “Stack Empty” when there are no elements in the stack.
Example Question :
A list, NList contains following record as list elements:
[City, Country, distance from Delhi]
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.
• 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.
• 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.
Computer Science (XII) Notes by Sumit Garg Sir
ANSWER:
travel=[]
def Push_element(NList):
for L in NList:
if(L[1] != 'India' and L[2]<3500):
[Link]([L[0],L[1]])
def Pop_element():
while len(travel):
print([Link]())
else:
print('Stack Empty')