Data structures are code structures for storing and organizing data that make it easier to modify, navigate, and access information. Data structures determine how data is collected, the functionality we can implement, and the relationships between data. Data structures can be categorized as in built and user defined data structures.
- Manage large datasets
- Build clear hierarchical or relational connections between data points
- Speed up data processing
List
Lists are ordered collection of items.
`# creating a list.
salad = ['mango', 'apple', 'avocado']
editing list items.
salad[0] = 'banana'
adding items to a list.
salad.append('strawberries')
removing an items
salad.remove('avocado')`
Dictionary
A dictionary is a collection which is ordered and does not allow duplicates. Dictionary is changeable as compared to a tuple
Laptop= {
"brand": "HP",
"model": "Probook",
"year": 2012
}
Tuple
Tuple is an ordered collection of item like lists but cannot be changed.
`# Creating a tuple
Apartment= ("Bukani", "Gateway", "Riverine")
Accessing items in a tuple
print(Apartment[0:2])
Deleting a tuple
del Apartment`
Set
A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries.
salad= {"mango", "apple", "avocado"}
print(salad)
Queues
Queue is a linear data structure that stores items in First In First Out (FIFO) manner. With a queue the least recently added item is removed first.
Operations associated with queue are:
Enqueue: Adds an item to the queue. If the queue is full, then it is said to be an Overflow condition.
Dequeue: Removes an item from the queue. The items are popped in the same order in which they are pushed.
Front: Get the front item from queue
Rear: Get the last item from queue
`# Python program to demonstrate queue implementation
# Initializing a queue
queue = []
# Adding elements to the queue
queue.append('a')
queue.append('b')
queue.append('c')
print("Initial queue")
print(queue)
** Removing elements from the queue**
print("\nElements dequeued from queue")
print(queue.pop(0))
print(queue.pop(0))
print(queue.pop(0))`
Stack
A stack is a linear data structure that stores items in a Last-In/First-Out (LIFO manner. In stack, a new element is added at one end and an element is removed from that end only. The insert and delete operations are often called push and pop.
The functions associated with stack are:
empty() – Returns whether the stack is empty
size() – Returns the size of the stack
top() – Returns a reference to the topmost element of the stack
push(a) – Inserts the element ‘a’ at the top of the stack
pop() – Deletes the topmost element of the stack
`stack = []
append() function to push
element in the stack
stack.append('a')
stack.append('b')
stack.append('c')
print('Initial stack')
print(stack)
pop() function to pop
element from stack in
LIFO order
print('\nElements popped from stack:')
print(stack.pop())
print(stack.pop())
print(stack.pop())
print('\nStack after elements are popped:')
print(stack)`
Linked List
Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at a contiguous location
`class Node:
# Function to initialize the node object
def __init__(self, data):
self.data = data # Assign data
self.next = None # Initialize next as null
Linked List class
class LinkedList:
# Function to initialize the Linked List object
def __init__(self):
self.head = None`
Top comments (0)