Deletion in Data Structures and Algorithms šŸŽÆ

beginner
20 min

Deletion in Data Structures and Algorithms šŸŽÆ

Welcome to the Deletion lesson! Today, we'll dive into various ways to delete elements from data structures like arrays, linked lists, and more. By the end of this lesson, you'll be able to delete elements at the beginning, end, by position, and by value. šŸ’”

Deletion in Arrays šŸ“

Arrays are a simple data structure where elements are stored in contiguous memory locations. Let's see how to delete an element by position and by value in an array.

Deletion by Position

To delete an element by position, you'll need to shift all elements after the specified index to the left. Here's an example using a Python array:

python
# Example array arr = [1, 2, 3, 4, 5, 6] # Delete element at index 2 del arr[2] # Print the modified array print(arr) # Output: [1, 2, 4, 5, 6]

Deletion by Value

Deleting an element by value requires finding the element first and then deleting it by position. Here's an example in Python:

python
# Example array arr = [1, 2, 3, 4, 5, 6] # Find the index of the value to delete (we'll use list.index()) index = arr.index(3) # Delete the element at the found index del arr[index] # Print the modified array print(arr) # Output: [1, 2, 4, 5, 6]

šŸ“ Note: If the value to delete is not found in the array, the list.index() function will raise an error.

Deletion in Linked Lists šŸ“

Linked lists are a more complex data structure where elements are stored in nodes, and each node points to the next one. Here's how to delete a node by position and by value in a linked list using Python.

Deletion by Position

To delete a node by position, you'll need to traverse the linked list until you find the correct node and then update its pointer to skip the deleted node.

python
class Node: def __init__(self, data): self.data = data self.next = None # Example linked list head = Node(1) head.next = Node(2) head.next.next = Node(3) head.next.next.next = Node(4) head.next.next.next.next = Node(5) # Function to delete a node by position def delete_node_by_position(node, position): if position == 0: node.data = node.next.data node.next = node.next.next return current = node for _ in range(position - 1): if not current.next: return "Element not found." current = current.next if not current.next.next: current.next = None else: current.next = current.next.next return "Element deleted." # Delete the node at position 2 print(delete_node_by_position(head, 2)) # Output: Element deleted. # Print the modified linked list current = head while current: print(current.data, end=" -> ") current = current.next

Deletion by Value

To delete a node by value, you'll need to traverse the linked list until you find the correct node and then delete it.

python
def delete_node_by_value(node, value): if not node: return "List is empty." if node.data == value: return delete_node(node) current = node while current.next: if current.next.data == value: return delete_node(current.next) current = current.next return "Element not found." def delete_node(node): current.next = node.next del node return "Element deleted." # Delete the node with value 3 print(delete_node_by_value(head, 3)) # Output: Element deleted. # Print the modified linked list current = head while current: print(current.data, end=" -> ") current = current.next

Quiz šŸ“

Quick Quiz
Question 1 of 1

Which method in Python can help you find the index of a value in a list?

That's it for today! Now you're equipped with the knowledge to delete elements from arrays and linked lists. In the next lesson, we'll dive deeper into advanced deletion techniques and data structures. Stay tuned! šŸ’”

šŸ“ Note: CodeYourCraft offers various resources to help you master data structures and algorithms. Explore our site to find more tutorials, practice problems, and quizzes!