Chapter 2
Chapter 2
This chapter focuses on Stacks and Queues, two important linear data structures.
def pop(self):
if not self.is_empty():
return self.stack.pop()
else:
return "Stack is empty"
def peek(self):
if not self.is_empty():
return self.stack[-1]
else:
return "Stack is empty"
def is_empty(self):
return len(self.stack) == 0
# Example Usage
s = Stack()
s.push(10)
s.push(20)
print(s.pop()) # Output: 20
print(s.peek()) # Output: 10
def dequeue(self):
if not self.is_empty():
return self.queue.pop(0)
else:
return "Queue is empty"
def front(self):
if not self.is_empty():
return self.queue[0]
else:
return "Queue is empty"
def is_empty(self):
return len(self.queue) == 0
# Example Usage
q = Queue()
q.enqueue(10)
q.enqueue(20)
print(q.dequeue()) # Output: 10
print(q.front()) # Output: 20
Summary of Chapter 2
Stacks → LIFO, Push, Pop, Peek, Applications.
Queues → FIFO, Enqueue, Dequeue, Applications.
Different types of queues → Simple, Circular, Deque, Priority Queue.