Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
22 views5 pages

Stack Py

dor the sql

Uploaded by

harrydanaj03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views5 pages

Stack Py

dor the sql

Uploaded by

harrydanaj03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Juzoi Mwahhh - You Can Do It!

- Stack with List and Class

Stack Using List and Class

A Stack is a linear data structure that follows the LIFO (Last In, First Out) principle.

Stack Using List:


- Python lists can be used to implement stacks easily with append() and pop().

Example:
stack = []
stack.append(10) # Push
stack.append(20)
stack.pop() # Pop

Stack Using Class (OOP):


class Stack:
def __init__(self):
self.items = []

def push(self, item):


self.items.append(item)

def pop(self):
if not self.isEmpty():
return self.items.pop()
else:
return "Stack Underflow"

def peek(self):
if not self.isEmpty():
return self.items[-1]
else:
Juzoi Mwahhh - You Can Do It! - Stack with List and Class

return "Stack is Empty"

def isEmpty(self):
return self.items == []

Stack Operations

1. push(item):
- Adds an item to the stack.
2. pop():
- Removes and returns the top item.
3. peek():
- Returns the top item without removing it.
4. isEmpty():
- Checks if the stack is empty.

10 MCQs with Answers and Explanations

1. Which data structure uses LIFO?


a) Queue b) Stack c) Array d) Linked List
Answer: b) Stack

2. What is the time complexity of push() in stack (list-based)?


a) O(1) b) O(n) c) O(log n) d) O(n^2)
Answer: a) O(1)

3. Which method is used to remove element from stack?


a) pop() b) push() c) peek() d) delete()
Answer: a) pop()

4. What happens if you pop from an empty stack?


Juzoi Mwahhh - You Can Do It! - Stack with List and Class

a) Underflow b) Overflow c) Crash d) Nothing


Answer: a) Underflow

5. Which is used to add element in stack?


a) append() b) insert() c) push() d) add()
Answer: c) push()

6. What does peek() do?


a) Removes top b) Returns top c) Adds top d) Checks if empty
Answer: b) Returns top

7. Stack is used in?


a) Function calls b) Recursion c) Undo operations d) All of the above
Answer: d) All of the above

8. Stack overflow occurs when?


a) Stack is full and push is called.
Answer: a)

9. In Python, list.pop() removes from?


a) Front b) End c) Random d) None
Answer: b) End

10. The default return of list.pop() is?


a) First element b) Last element c) None d) Error
Answer: b) Last element

5 Most Important Class-based PYQs

1. Define a class Stack and implement push(), pop(), peek(), isEmpty().


Juzoi Mwahhh - You Can Do It! - Stack with List and Class

2. Write a class to reverse a string using stack.

3. Implement a stack class to check balanced parentheses.

4. Write a Stack class to convert decimal to binary.

5. Create a stack class to evaluate postfix expressions.

Output Prediction Questions

1.
s = []
s.append(5)
s.append(10)
print(s.pop())
# Output: 10

2.
class Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()

s = Stack()
s.push(1)
s.push(2)
print(s.pop())
print(s.pop())
Juzoi Mwahhh - You Can Do It! - Stack with List and Class

# Output: 2
# 1

3.
s = []
print(len(s)==0)
# Output: True

You might also like