Data Structure – Stack
Q. Answer the following questions:
1. Define a Stack.
2. Explain the LIFO (Last-In, First-Out) principle of a stack.
3. What are the primary operations on a stack? (PUSH, POP, PEEK/TOP, isEmpty/isFull)
4. What is the significance of the "TOP" pointer in a stack?
5. What is "Stack Overflow"? When does it occur?
6. What is "Stack Underflow"? When does it occur?
7. Differentiate between a Stack and a List (in Python context).
8. Differentiate between a Stack and a Queue.
Q. Following questions involve writing Python functions to implement stack operations.
1. Write a Python function PUSH(STK, VAL) to add an element VAL to a stack STK.
2. Write a Python function POP(STK) to remove and return an element from a stack STK. Handle
the "Stack Underflow" condition.
3. Write a Python function PEEK(STK) or TOP(STK) to display the topmost element of the stack
without removing it.
4. Write a Python function IsEmpty(STK) to check if the stack is empty.
5. Write a Python function DisplayStack(STK) to display all elements of the stack.
6. Write a function to push only even numbers from a given list into a stack. Then pop and display
the contents.
7. Write a function to push elements greater than a certain value into a stack.
8. A school has a dictionary STUDENTS = {'Rohan': 40, 'Rihaan': 55, 'Tejas': 80, 'Ajay': 90} (name:
marks). Write a program with functions push_eligible_students(stack, students_dict) and
pop_and_display(stack). push_eligible_students should push the names of students whose
marks are greater than 70 into the stack. Then pop_and_display should pop and display the
names. Write a function to traverse a list and push only odd numbers onto a stack. Then pop
and display.
9. Write a function to reverse a string or a list using stack operations.
10. You have a stack named BooksStack that contains records of books. Each book record is
represented as a list containing book_title, author_name, and publication_year. Write the
following user-defined functions in Python to perform the specified operations on the stack
BooksStack:
(I) push_book(BooksStack, new_book): This function takes the stack
BooksStack and a new book record new_book as arguments and pushes the
new book record onto the stack.
(II) pop_book(BooksStack): This function pops the topmost book record from
the stack and returns it. If the stack is already empty, the function should
display "Underflow".
(III) peep(BookStack): This function displays the topmost element of the stack
without deleting it. If the stack is empty, the function should display 'None'.
11. Write the definition of a user-defined function `push_even(N)` which accepts a list of integers
in a parameter `N` and pushes all those integers which are even from the list `N` into a Stack
named `EvenNumbers`. Write function pop_even() to pop the topmost number from the stack
and returns it. If the stack is already empty, the function should display "Empty". Write function
Disp_even() to display all element of the stack without deleting them. If the stack is empty, the
function should display 'None'.
For example: If the integers input into the list `VALUES` are: [10, 5, 8, 3, 12] Then the
stack `EvenNumbers` should store: [10, 8, 12]
12. 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. (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=[["New York", "U.S.A.", 11734],
["Naypyidaw", "Myanmar", 3219],
["Dubai", "UAE", 2194],
["London", "England", 6693],
["Gangtok", "India", 1580],
["Columbo", "Sri Lanka", 3405]]
The stack should contain: ['Naypyidaw', 'Myanmar'],
['Dubai', 'UAE'],
['Columbo', 'Sri Lanka']
The output should be: ['Columbo', 'Sri Lanka']
['Dubai', 'UAE']
['Naypyidaw', 'Myanmar']
Stack Empty