What are some commonly used Data Structures in Programming ?

Author: Praweg Koirala

🗓️ April 18, 2024

⌛️ 9 mins

🪴 Growing

🏷️    Programing | data | javascript | python

The Core of most of our modern application lies in the data and their ability to make meaningful connections in our everyday Life.

We can look at some of the popular applications that we use everyday as an example. Uber: a ride hailing app, tinder: a dating app, instacart: a grocery delivery app, fitbit: a fitness tracking app and bunch of other similar apps , they all have one thing in common. They all cater their services via data. In the core of all these applications lie ability to handle and manipulate data which in turn becomes a service that is worthy of monthly subscription, a value that we all have become accustomed to.

What are these different data structures that allow to build these great algorithms, which we can be conveniently accessed through the palm sized portals in our pocket.

Lets thoroughly go through these different structures that are commonly found in most programming languages

  • Arrays
  • Linked Lists.
  • Stacks
  • Queues
  • Hash Table (Hash Maps).
  • Trees
  • Graphs
  • In Java:

    Java Provides a rich set of data structures through its collections Framework, which includes both built-in classes and interfaces. Here are the available structure types:

  • Arrays: Java arrays are similar to those in other languages. They allow you to store multiple elements of the same type in a contiguous memory block. Arrays have a fixed size and are indexed by integers.
  • ArrayList: An implementation of a dynamic array that can grow or shrink as needed. It’s part of the Java Collections Framework and provides methods for adding, removing, and accessing elements.
  • LinkedList: A doubly-linked list where each element (node) contains a reference to the next and previous nodes. It’s useful for scenarios where frequent insertions and deletions are required.
  • Stack: Java doesn’t have a built-in stack class, but you can use the java.util.Stack class or implement your own custom stack using an array or linked list.
  • Queue: Java provides the java.util.Queue interface, which has implementations like LinkedList and PriorityQueue. Queues follow the FIFO (First-In-First-Out) order.
  • HashMap: A hash table-based implementation of the Map interface. It allows efficient key-value pair lookups. Keys are hashed to determine their storage location.
  • HashSet: An implementation of the Set interface using a hash table. It stores unique elements and doesn’t allow duplicates.
  • TreeSet: A sorted set implemented as a self-balancing binary search tree (usually a red-black tree). Elements are ordered based on their natural ordering or a custom comparator.
  • TreeMap: A sorted map implemented as a self-balancing binary search tree. It maintains key-value pairs in sorted order.
  • Binary Trees: Java doesn’t provide a direct binary tree class, but you can create custom binary trees using nodes and references.
  • AVL Trees: Similarly, AVL trees (self-balancing binary search trees) are not part of the standard library, but you can implement them manually.
  • Heaps: Java provides a PriorityQueue class, which is a min-heap by default. You can also create max-heaps using custom comparators.
  • Tries (Prefix Trees): Tries are not directly available in Java, but you can build custom trie structures using nodes and character-based links.
  • Graphs: Java doesn’t have a built-in graph class, but you can represent graphs using adjacency lists or matrices. Graph algorithms can be implemented manually.
  • Undirected Graphs: Similar to directed graphs, undirected graphs can be represented using adjacency lists or matrices. They lack direction in their edges
  • In Python:

    Python is highly favored by data scientists and widely used in Machine Learning & AI. This clearly indicates that it is capable of handling data and allows to process variety of data structures. Python is also popular for its shorter and clean syntax. You write less but still get a lot of task done, which to me is the definition of efficiency. Hence we can see python as being a language that allows to handle data structures and algorithms efficiently. Here are the data structures commonly used in python:

  • Lists: These are like Arrays and called arrays in some languages but lists in other. Lists are versatile and widely used. They allow you to store an ordered collection of elements. Lists can hold different data types, and you can perform operations like appending, slicing, and iterating.
  • Dictionaries (Dicts):These are similar to Hash-Table/Hash-map in other programming languages Dictionaries are key-value pairs. They provide fast lookups based on keys. Use dictionaries when you need to associate values with unique identifiers (keys).
  • Sets: Sets are unordered collections of unique elements. They are useful for eliminating duplicates and performing set operations like union, intersection, and difference.
  • Tuples: Tuples are similar to lists but are immutable (cannot be modified after creation). They are handy for representing fixed data structures or returning multiple values from a function.
  • Stacks: Although Python doesn’t have a built-in stack class, you can use lists to simulate stacks. The last-in, first-out (LIFO) behavior is useful for algorithms like depth-first search.
  • Queues: Similarly, Python doesn’t provide a native queue class, but you can use lists or the collections module’s deque to create queues. Queues follow the first-in, first-out (FIFO) principle.
  • Heaps: Python’s heapq module allows you to create min-heaps and max-heaps. Heaps are essential for priority queues and efficient algorithms like Dijkstra’s shortest path.
  • Graphs: While Python doesn’t have a built-in graph class, you can represent graphs using dictionaries (adjacency lists) or custom classes. Graph algorithms (BFS, DFS, etc.) rely on these structures.
  • Trees: Trees (including binary trees, AVL trees, and others) are fundamental for hierarchical data representation. You can create custom tree structures using classes.
  • Hash Tables: Although Python doesn’t expose hash tables directly, dictionaries (dicts) serve as efficient hash maps. They allow constant-time lookups based on keys.
  • Linked Lists: Python doesn’t have a native linked list class, but you can implement custom linked lists using classes. Linked lists are useful for certain algorithms and data organization.
  • In C++:

    C++ is one of the older, modern programming language, that has been around for long enough and used widely enough to gain trust. It is a high level programming language that can be seen as lower level, compared to other modern high level languages. A language closer to the machine code (binary system) than other modern languages . It has much better grasp at devices memory allocation/management . Because of these reason c++ can provide the most flexibility in terms of efficient algorithm. It offers a rich set of data structures .

  • Arrays: Arrays are fixed-size collections of elements of the same type. They allow direct access by index and are useful for storing homogeneous data.
  • Vectors (std::vector): Dynamic arrays that can grow or shrink as needed. Vectors are part of the C++ Standard Library and provide methods for adding, removing, and accessing elements.
  • Linked Lists (std::list): Doubly-linked lists where each element (node) contains references to the next and previous nodes. Useful for frequent insertions and deletions.
  • Maps (std::map): Ordered search trees that store key-value pairs. Efficient for lookups based on keys.
  • Hash Tables (std::unordered_map): Hash maps that provide fast key-value pair lookups. Keys are hashed to determine their storage location.
  • Sets (std::set and std::unordered_set): Collections of unique elements. Sets are useful for eliminating duplicates and performing set operations.
  • Stacks (std::stack): Follow the Last-In-First-Out (LIFO) principle. You can use the std::stack class or create custom stacks using arrays or linked lists.
  • Queues (std::queue): Follow the First-In-First-Out (FIFO) principle. The std::queue class or custom queues can be used for algorithms like breadth-first search.
  • Heaps (std::priority_queue): Priority queues implemented as heaps. Useful for algorithms like Dijkstra’s shortest path.
  • Graphs: Represented using adjacency lists or matrices. Graph algorithms (BFS, DFS, etc.) rely on these structures.
  • Trees (including Binary Trees): Hierarchical data structures. Custom binary trees can be created using nodes and references.
  • Tries (Prefix Trees): Useful for efficient string matching and autocomplete features. Custom trie structures can be built.
  • In JavaScript:

    If the goal is to use Javascript as a language to practice different data structure types and building algorithms with them. It does not come with all the commonly known data structure types built in like in c++ or Java but they can be custom created as needed.

  • Arrays: Arrays are versatile and widely used. They allow you to store an ordered collection of elements. Each item can be accessed through its index (position) number. For example:

    const fruits = ['apple', 'banana', 'cherry', 'mango']; console.log(fruits[2]); // Output: 'cherry'
  • Objects (Hash Tables): Objects in JavaScript act as hash tables. They store key-value pairs, allowing efficient lookups based on keys. For example:JavaScript

    const person = { name: 'Alex', age: 30 }; console.log(person.name); // Output: 'Alex'
  • Stacks: Although JavaScript doesn’t have a native stack class, you can simulate stacks using arrays. The last-in, first-out (LIFO) behavior is useful for algorithms like depth-first search.

  • Queues: Similarly, JavaScript doesn’t have a built-in queue class, but you can implement queues using arrays or the deque from the collections module. Queues follow the first-in, first-out (FIFO) principle.

  • Linked Lists: While JavaScript doesn’t have a native linked list class, you can create custom linked lists using objects or classes. Linked lists are useful for scenarios with frequent insertions and deletions.

  • Trees: Binary trees, AVL trees, and other tree structures can be implemented manually using objects or classes. Trees are essential for hierarchical data representation.

  • Heaps: JavaScript doesn’t provide a direct heap class, but you can create min-heaps or max-heaps using arrays or custom classes. Heaps are useful for priority queues and algorithms like Dijkstra’s shortest path.

  • Graphs: Although JavaScript doesn’t have a built-in graph class, you can represent graphs using objects (adjacency lists) or other custom structures. Graph algorithms (BFS, DFS, etc.) rely on these structures.

  • In Dart:

    In the List of Programming languages mentioned here Dart is the newest. In fact it is probably one of the newer programming language in general that has been used to build different applications. Dart is the programming language used in Framework/SDK called Flutter that allows to build Cross Platform applications (Mobile, Web, Desktop). Dart is an Object Oriented, statically typed & null safe modern programming language that adopts a lot of features from other matured programing languages like, Java, c++ & JavaScript. It is equipped to build the modern applications . However, it is still evolving and has yet to mature. In terms of Data structure types it does not come with all the commonly known data structures built in like in other languages above but can be custom created as needed.

  • Lists: Lists are ordered collections of data that you can store and reference as a single entity. Each element in the list is accessed by its index, starting from 0. Lists are useful for scenarios where data grows dynamically, such as storing browsing history, songs in a music player, or navigating webpages in a browser. You can create either growable or fixed-length lists in Dart.
  • Maps (Hash Tables): Maps allow you to store key-value pairs. They are efficient for lookups based on keys. You can nest maps, pass them to functions, and store them in other data structures like lists and sets.
  • Sets: Sets are unordered collections of unique elements. They are useful for eliminating duplicates and performing set operations like union, intersection, and difference.
  • Records: Records are real values in Dart. You can store them in variables, nest them, pass them to and from functions, and store them in data structures like lists, maps, and sets.
  • Trees: Although Dart doesn’t provide built-in tree classes, you can create custom tree structures using objects or classes. Trees are essential for hierarchical data representation.
  • Heaps: Dart doesn’t have a direct heap class, but you can create min-heaps or max-heaps using lists or custom classes. Heaps are useful for priority queues and algorithms like Dijkstra’s shortest path.
  • Graphs: While Dart doesn’t have a built-in graph class, you can represent graphs using custom structures like adjacency lists or matrices. Graph algorithms (BFS, DFS, etc.) rely on these data structures.
  • This post was last updated on: April 18, 2024

    Back