Sitemap
Siripinyo

Siripinyo

A lecturer and researcher in AI and Data Science

Data Structure-2: Basic Data Structures

7 min readJan 13, 2024

--

Table of Contents

  1. What Are Data Structures?
  2. Overview of Basic Data Structures: Arrays, Linked Lists, Stacks, Queues, Trees, and Graph
  3. Real-world Applications

What Are Data Structures?

Data structures are systematic ways of organizing, managing, and storing data that enable efficient access and modification. Like a library that organizes books in a specific order to facilitate finding them quickly, data structures arrange data in a computer to be accessed rapidly, updated, and managed optimally. The choice of data structure can significantly affect the performance of a program, determining how quickly it can process the data it holds.

Data structures are commonly categorized into two types: primitive and non-primitive.

  • Primitive data structures are the basic data types built into the programming language. They include integers, floats, booleans, and characters. These types serve as the building blocks for more complex structures and are handled directly by the computer’s architecture.
  • Non-primitive data structures, or composite or compound data structures, are more complex than primitive types. They can be linear or non-linear and static or dynamic. Linear structures like Arrays, Lists, Stacks, and Queues arrange data sequentially, while non-linear structures like Trees and Graphs organize data hierarchically or interconnectedly. Static data structures have a fixed size, whereas dynamic structures can grow and shrink during runtime.
Press enter or click to view image in full size
Classification of Data Structures (source: Data structures and Algorithm with C)

The efficiency of a program often hinges on using the appropriate data structure for a given task. Selecting the correct data structure can lead to faster execution times, reduced memory usage, and more readable and maintainable code. For example, a hash table might be excellent for quick data retrieval based on key-value pairs, while a binary search tree might be more suitable for data constantly being inserted or deleted in a sorted order. Understanding the strengths and limitations of each data structure allows programmers to make informed decisions that ultimately affect the performance and scalability of their applications.

Overview of Basic Data Structures: Arrays, Linked Lists, Stacks, Queues, Trees, and Graph

These data structures are among computer science’s most fundamental and widely used. They each offer unique ways to organize and manage data and are chosen based on the specific needs of the application or algorithm.

Arrays: An array is a collection of elements, each identified by one or more indices or keys. The elements in an array are usually of the same data type and are stored in contiguous memory locations. This allows quick access to any element, as its memory address can be easily calculated. However, the size of an array is static and determined at the time of creation, which can be a limitation if the amount of data is not known upfront.

Example: A scoreboard in a game, where the scores of different players are stored in an array.

Press enter or click to view image in full size
1D array (source: https://www.geeksforgeeks.org/introduction-to-arrays-data-structure-and-algorithm-tutorials/)
2D array (source: https://www.geeksforgeeks.org/introduction-to-arrays-data-structure-and-algorithm-tutorials/)

Linked Lists: A linked list is a linear collection of data elements called nodes, each pointing to the next node utilizing a pointer. It is a dynamic data structure, meaning that it can grow or shrink at runtime and does not require a continuous block of memory. The downside is that access time can be slower, as one must traverse the list from the beginning to reach a particular element.

Example: A music playlist application where each song is a node with a pointer to the next song.

Press enter or click to view image in full size
Single linked list (source: https://www.geeksforgeeks.org/what-is-linked-list/)

Stacks: A stack is a collection of elements that follows the Last-In-First-Out (LIFO) principle. The most recently added element is the first one to be removed. Stacks are often used in situations where recursion is involved or for undo mechanisms in applications.

Get Siripinyo’s stories in your inbox

Join Medium for free to get updates from this writer.

Example: The back button in a web browser allows you to visit the last page you were on.

Queues: A queue is similar to a stack but operates on a First-In-First-Out (FIFO) basis. Elements are added at the rear and removed from the front. This structure is useful in scenarios where order needs to be preserved.

Example: A printer queue, where print jobs are handled in the order they were submitted.

Press enter or click to view image in full size
Queue (source: https://www.geeksforgeeks.org/queue-data-structure/?ref=lbp)

Trees: A tree is a non-linear data structure composed of nodes that mimic a hierarchical tree structure with a root value and subtrees of children, represented as a set of linked nodes. A special kind of tree, the binary tree, allows each node to have at most two children. Trees are used in many applications, including organizing hierarchical data, such as file systems, and in algorithms, such as those used in databases.

Example: The folder structure on your computer, where each folder is a node with potentially many child folders.

Binary tree (source: https://www.geeksforgeeks.org/introduction-to-binary-tree-data-structure-and-algorithm-tutorials/)

Graphs: A graph is a versatile data structure in computer science and mathematics, consisting of a set of vertices (or nodes) connected by edges. These vertices represent entities, and the edges denote their relationships or interactions. Graphs can be directed or undirected, indicating whether the relationships have a specific direction, and can be weighted if the connections have associated costs or values. This structure is incredibly useful for modelling complex networks such as social networks, transportation systems, and communication networks. Its diverse applications range from pathfinding in GPS navigation systems to analyzing social network connections, making it a fundamental concept in both theoretical and applied computer science.

Example: In a transportation network graph, cities are vertices, and roads are edges connecting them. In a social network graph, individuals are vertices, and friendships or connections are the edges linking these individuals.

Press enter or click to view image in full size
Graph (source: https://www.geeksforgeeks.org/introduction-to-graphs-data-structure-and-algorithm-tutorials/)

How different data structures are suited to different tasks

The choice of a data structure is driven by the operations it needs to support and the efficiency with which it can carry out these operations. For instance, arrays are excellent for quick, random access of elements. In contrast, linked lists are better for scenarios where the data is constantly inserted or deleted, as these operations do not require shifting elements around. Stacks and queues are ideal for tasks requiring elements to be processed in a specific order, and trees are well-suited for organizing data hierarchically, allowing quick searches, insertions, and deletions.

Understanding these properties allows developers to choose the most appropriate data structure, optimizing the performance and effectiveness of their programs.

Real-world Applications

Managing User Data in Social Media
Social media applications use various data structures to efficiently manage the vast amount of user data. For instance, they may use hash tables to store user profiles, allowing quick access when a user logs in or searches for friends. Graph data structures represent and manage the complex relationships and interactions between users, such as friendships and followers. Dynamic arrays or linked lists can store user posts and feeds, where new content is continuously added, and scalability is key.

Organizing Files in Operating Systems
Operating systems use tree data structures to organize files and directories. This hierarchical system allows users to navigate through folders (nodes) and access files (leaves). For example, a directory might represent a tree’s root, with folders as branches and files as leaves. Additionally, a variation of trees, called B-Trees or B+Trees, is often used in filesystems to efficiently manage data storage on disk, enabling fast file retrieval and space allocation.

E-commerce Product Inventory
E-commerce platforms utilize data structures to manage their inventory and product listings. An array or a linked list might be used to hold individual product records. In contrast, a binary search or balanced tree like an AVL tree could be used to keep the products sorted for quick search and retrieval, crucial for a seamless user shopping experience.

Search Engines Indexing
Search engines use a combination of sophisticated data structures to index web pages. Inverted indices, a type of hash table, allow search engines to retrieve all pages containing a specific word quickly. Tries are used for auto-completion features, predicting what a user tries to type based on popular queries and indexed data.

Routing and Navigation Systems
GPS navigation systems use graph data structures to represent maps and find the shortest path between two points. Each intersection is a node, and each road segment is an edge. Algorithms like Dijkstra’s or A* are applied over these graphs to calculate the quickest or shortest route for users.

These examples illustrate how data structures are used behind the scenes in everyday applications to manage and manipulate data effectively, ensuring that our digital services are fast, reliable, and user-friendly.

In the upcoming article of this series, you’ll be taking a closer look at individual data structures. It will discuss their explanation, their complexities, and practical coding examples. Stay tuned to dive deeper into arrays, linked lists, stacks, queues, trees, and graphs.

--

--

Siripinyo

Written by Siripinyo

A lecturer and researcher in AI and Data Science