| Download the amazing global Makindo app: Android | Apple | |
|---|---|
| MEDICAL DISCLAIMER: Educational use only. Not for diagnosis or management. See below for full disclaimer. |
Related Subjects: |Python |C |Java |C++ |C sharp |VB |Natural Language processing |How does a CPU work |Computer Networking |Computer Security |Concurrent Programming |Compilers |Cryptography |Data Structures |Database Management |Object orientated programming
Data structures are fundamental concepts in computer science that provide ways to store, organize, and manage data efficiently. They are essential for designing efficient algorithms and for solving complex computational problems. Understanding data structures is crucial for developing robust and high-performance software applications.
# Array arr = [1, 2, 3, 4, 5] # Linked List class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def append(self, data): new_node = Node(data) if not self.head: self.head = new_node return last = self.head while last.next: last = last.next last.next = new_node ll = LinkedList() ll.append(1) ll.append(2) ll.append(3)
# Binary Tree
class TreeNode:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.left = TreeNode(4)
root.left.right = TreeNode(5)
# Graph using adjacency list
class Graph:
def __init__(self):
self.graph = {}
def add_edge(self, u, v):
if u not in self.graph:
self.graph[u] = []
self.graph[u].append(v)
g = Graph()
g.add_edge(0, 1)
g.add_edge(0, 2)
g.add_edge(1, 2)
g.add_edge(2, 0)
g.add_edge(2, 3)
g.add_edge(3, 3)
arr = [1, 2, 3, 4, 5] print(arr[2]) # Output: 3
class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def append(self, data): new_node = Node(data) if not self.head: self.head = new_node return last = self.head while last.next: last = last.next last.next = new_node ll = LinkedList() ll.append(1) ll.append(2) ll.append(3)
stack = [] stack.append(1) stack.append(2) stack.append(3) print(stack.pop()) # Output: 3
from collections import deque queue = deque() queue.append(1) queue.append(2) queue.append(3) print(queue.popleft()) # Output: 1
class TreeNode: def __init__(self, data): self.data = data self.left = None self.right = None root = TreeNode(1) root.left = TreeNode(2) root.right = TreeNode(3) root.left.left = TreeNode(4) root.left.right = TreeNode(5)
class Graph:
def __init__(self):
self.graph = {}
def add_edge(self, u, v):
if u not in self.graph:
self.graph[u] = []
self.graph[u].append(v)
g = Graph()
g.add_edge(0, 1)
g.add_edge(0, 2)
g.add_edge(1, 2)
g.add_edge(2, 0)
g.add_edge(2, 3)
g.add_edge(3, 3)
hash_table = {}
hash_table["key1"] = "value1"
hash_table["key2"] = "value2"
print(hash_table["key1"]) # Output: value1
Data structures are vital components in computer science, enabling efficient data storage, organization, and manipulation. Linear data structures include arrays, linked lists, stacks, and queues, while non-linear data structures include trees and graphs. Each data structure has its own set of operations and applications, making them suitable for various computational tasks. Understanding and implementing these data structures is essential for developing efficient algorithms and solving complex problems in computer science.