Welcome to the Data Structures Examples page! Here, we'll explore common data structures through practical code snippets and visual explanations. Let's dive in!
Common Data Structures & Examples
1. Arrays 📊
Arrays store elements in contiguous memory locations.
Example in Python:
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # Output: apple
2. Linked Lists ⛓️
Linked lists use nodes connected by pointers.
Example in JavaScript:
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
3. Stacks 📂
Stacks follow LIFO (Last In, First Out) principle.
Example in Java:
Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
System.out.println(stack.pop()); // Output: 2
4. Queues 🚶♂️
Queues follow FIFO (First In, First Out) principle.
Example in C++:
#include <queue>
queue<string> q;
q.push("hello");
q.push("world");
cout << q.front(); // Output: hello
5. Trees 🌳
Trees are hierarchical structures with nodes connected by edges.
Example of a binary tree:
class TreeNode:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
6. Graphs 🗺️
Graphs consist of nodes (vertices) and edges.
Example of an adjacency list in Python:
graph = {
"A": ["B", "C"],
"B": ["A", "D"],
"C": ["A", "D"],
"D": ["B", "C"]
}
Practical Applications
- Arrays: Storing fixed-size data like inventory lists 🛒
- Linked Lists: Dynamic memory allocation in browsers 🌐
- Stacks: Undo functionality in editors 📝
- Queues: Task scheduling in operating systems ⏱️
- Trees: File system hierarchies 📁
- Graphs: Social network connections 🤝
Expand Your Knowledge
For a deeper dive into data structures, check out our comprehensive tutorial. 🚀
Want to see visual comparisons of these structures? Explore data structure diagrams for clarity! 📈