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

Chapter 8

Uploaded by

ashim05birbhum
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)
2 views5 pages

Chapter 8

Uploaded by

ashim05birbhum
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

Chapter 8: Data Structures in Python

(Stacks & Queues)


8.1 Introduction to Data Structures
A data structure is a way of organizing and storing data efficiently.

8.1.1 Types of Data Structures

1. Linear Data Structures → Elements are arranged sequentially.


o Examples: Stack, Queue, Linked List
2. Non-Linear Data Structures → Elements are connected in a hierarchical manner.
o Examples: Tree, Graph

This chapter focuses on Stacks & Queues, which are linear data structures.

8.2 Stack in Python


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

8.2.1 Basic Operations in a Stack

Operation Description
Push Adds an element to the top of the stack.
Pop Removes and returns the top element.
Peek (Top) Returns the top element without removing it.
isEmpty Checks if the stack is empty.

8.3 Implementing Stack in Python


A stack can be implemented using a list or the queue module.

8.3.1 Stack Using a List


python
Copy
stack = [] # Creating an empty stack

# Push operation
stack.append(10)
stack.append(20)
stack.append(30)

# Pop operation
print(stack.pop()) # Output: 30 (LIFO)

# Peek operation
print(stack[-1]) # Output: 20

# Checking if stack is empty


print(len(stack) == 0) # Output: False

8.3.2 Stack Using a Class


python
Copy
class Stack:
def __init__(self):
self.stack = []

def push(self, item):


self.stack.append(item)

def pop(self):
if not self.is_empty():
return self.stack.pop()
return "Stack is empty"

def peek(self):
if not self.is_empty():
return self.stack[-1]
return "Stack is empty"

def is_empty(self):
return len(self.stack) == 0

# Using the Stack class


s = Stack()
s.push(10)
s.push(20)
print(s.pop()) # Output: 20
print(s.peek()) # Output: 10

8.4 Applications of Stack


 Expression Evaluation (Postfix, Prefix, Infix conversions).
 Backtracking (Maze solving, Undo/Redo functionality).
 Function Calls & Recursion (Maintains function call history).

8.5 Queue in Python


A Queue is a data structure that follows the FIFO (First In, First Out) principle.

8.5.1 Basic Operations in a Queue


Operation Description
Enqueue Adds an element to the rear of the queue.
Dequeue Removes an element from the front.
Front (Peek) Returns the front element without removing it.
isEmpty Checks if the queue is empty.

8.6 Implementing Queue in Python


A queue can be implemented using list, collections.deque, or queue.Queue.

8.6.1 Queue Using a List


python
Copy
queue = [] # Creating an empty queue

# Enqueue operation
queue.append(10)
queue.append(20)
queue.append(30)

# Dequeue operation
print(queue.pop(0)) # Output: 10 (FIFO)

# Peek operation
print(queue[0]) # Output: 20

# Checking if queue is empty


print(len(queue) == 0) # Output: False

8.6.2 Queue Using collections.deque


python
Copy
from collections import deque

queue = deque() # Creating a queue

# Enqueue operation
queue.append(10)
queue.append(20)
queue.append(30)

# Dequeue operation
print(queue.popleft()) # Output: 10

# Peek operation
print(queue[0]) # Output: 20

8.6.3 Queue Using a Class


python
Copy
class Queue:
def __init__(self):
self.queue = []

def enqueue(self, item):


self.queue.append(item)

def dequeue(self):
if not self.is_empty():
return self.queue.pop(0)
return "Queue is empty"

def peek(self):
if not self.is_empty():
return self.queue[0]
return "Queue is empty"

def is_empty(self):
return len(self.queue) == 0

# Using the Queue class


q = Queue()
q.enqueue(10)
q.enqueue(20)
print(q.dequeue()) # Output: 10
print(q.peek()) # Output: 20

8.7 Applications of Queue


 Task Scheduling (CPU scheduling, Print Spooler).
 Data Buffering (Handling multiple requests in a network).
 Breadth-First Search (BFS) in Graphs (Exploring nodes level-wise).

8.8 Types of Queues


Type Description
Follows FIFO. Elements are inserted at the rear and removed
Simple Queue
from the front.
The last position is connected to the first, improving space
Circular Queue
utilization.
Priority Queue Elements are dequeued based on priority instead of order.
Double-Ended Queue
Allows insertion and deletion from both ends.
(Deque)

8.8.1 Circular Queue Implementation


python
Copy
class CircularQueue:
def __init__(self, size):
self.queue = [None] * size
self.size = size
self.front = self.rear = -1

def enqueue(self, item):


if (self.rear + 1) % self.size == self.front:
return "Queue is full"
elif self.front == -1:
self.front = self.rear = 0
else:
self.rear = (self.rear + 1) % self.size
self.queue[self.rear] = item

def dequeue(self):
if self.front == -1:
return "Queue is empty"
elif self.front == self.rear:
item = self.queue[self.front]
self.front = self.rear = -1
else:
item = self.queue[self.front]
self.front = (self.front + 1) % self.size
return item

# Using Circular Queue


cq = CircularQueue(3)
cq.enqueue(10)
cq.enqueue(20)
print(cq.dequeue()) # Output: 10

Summary of Chapter 8
 Stack follows LIFO (Last In, First Out).
 Queue follows FIFO (First In, First Out).
 Operations: Push, Pop (Stack) | Enqueue, Dequeue (Queue).
 Stack Applications: Expression evaluation, Backtracking, Function calls.
 Queue Applications: CPU Scheduling, BFS, Data Buffering.
 Types of Queues: Simple, Circular, Priority, Deque.

You might also like