Dsa 02
Dsa 02
● Arrays
● Lists
● Stacks
● Queues
● Deques
Arrays
An array is a collection of items stored at contiguous memory locations.
Characteristics:
● Fixed size
● Homogeneous elements
● Indexed access
Operations:
● Access: O(1)
● Insertion: O(n)
● Deletion: O(n)
Example in Python:
arr = [1, 2, 3, 4, 5]
arr.append(6) # Insertion
arr.pop(1) # Deletion
Lists
Lists in Python are dynamic arrays that can hold heterogeneous data.
Characteristics:
● Dynamic resizing
Example:
my_list.append('banana')
print(my_list)
Stacks
A stack is a LIFO (Last In, First Out) data structure.
Operations:
● Push: Add an item to the top
Use Cases:
Implementation:
stack = []
stack.append(10)
stack.append(20)
print(stack.pop()) # Output: 20
Queues
A queue is a FIFO (First In, First Out) data structure.
Operations:
Use Cases:
● Task scheduling
Implementation:
from collections import deque
queue.append('D')
queue.popleft()
Deques
Double-ended queues (deques) allow insertion and deletion from both ends.
Use Cases:
● Palindrome checking
Example:
dq = deque([1, 2, 3])
dq.appendleft(0)
dq.append(4)
dq.pop()
dq.popleft()
Conclusion
Linear data structures are fundamental for various applications, from simple data storage to
complex operations. In the next document, we will explore non-linear data structures like trees
and graphs.