Data Structures and Algorithms Fundamentals
Introduction to Data Structures
Organizing Information
Imagine your closet. If you just throw all your clothes in a pile, finding a specific sock is a nightmare. But if you organize them—socks in one drawer, shirts hung up—finding what you need becomes fast and easy. In programming, we have a similar need to organize information. A data structure is simply a way of organizing and storing data so it can be accessed and used efficiently.
Choosing the right data structure is like choosing the right tool for a job. You wouldn't use a hammer to cut a piece of wood. Similarly, the way you structure your data can dramatically affect how well your program works. Let's look at some of the most common and fundamental data structures.
Arrays
The simplest data structure is the array. Think of it as a row of numbered mailboxes. Each mailbox can hold one item, and if you know the box number (its index), you can go directly to it. In programming, arrays store a collection of items in a specific order and in a contiguous block of memory.
An important detail: in most programming languages, array indices start at 0, not 1. So, the first item is at index 0, the second at index 1, and so on.
Arrays are great when you need to quickly access any element. If you want to see the third item in a list of a million items, an array can jump right to index 2 instantly. However, they have a drawback. Most arrays have a fixed size, meaning you have to decide how many mailboxes you need from the start. Adding or removing an item in the middle can also be slow, as you might have to shift all the other items to make room or close a gap.
Use an array when you have a fixed number of items and need to access them quickly by their position.
Linked Lists
What if you don't know how many items you'll have? Or what if you need to add and remove items constantly? This is where a linked list shines. Imagine a scavenger hunt. Each clue not only has information but also tells you where to find the next clue. A linked list works the same way.
Each item in a linked list, called a node, contains two things: the data itself and a pointer (or a link) to the next node in the sequence. The list can easily grow or shrink by simply adding new nodes and updating the pointers. You don't need a contiguous block of memory; the nodes can be scattered all over.
The trade-off is in access time. To find the 50th item in a linked list, you can't just jump to it. You have to start at the very first node and follow the links 49 times to get there. But inserting a new item is fast. You just create a new node and adjust the pointers of its neighbors, like adding a new stop on a train line.
Use a linked list when you need a dynamic list that changes size often, especially with frequent insertions and deletions.
Stacks and Queues
Sometimes, you don't need full access to every item in a list. You might only need to interact with items at the beginning or the end. Stacks and queues are data structures that enforce specific rules for adding and removing elements. They can be built using either arrays or linked lists under the hood.
Stack
noun
A data structure that follows the Last-In, First-Out (LIFO) principle. The last element added is the first one to be removed.
Think of a stack of plates. You add a new plate to the top, and you take a plate from the top. You don't pull one from the bottom of the stack. This is the LIFO principle. The main operations are push (adding an item to the top) and pop (removing the item from the top).
Now, let's consider a line at a ticket counter.
Queue
noun
A data structure that follows the First-In, First-Out (FIFO) principle. The first element added is the first one to be removed.
The first person to get in line is the first person to get a ticket. This is the FIFO principle. A queue works the same way. The main operations are enqueue (adding an item to the back of the line) and dequeue (removing the item from the front).
Stacks are useful for things like the "undo" function in a text editor or managing function calls in a program. Queues are perfect for managing tasks in the order they were received, like a printer handling print jobs or a web server handling requests.
These four structures are the foundational building blocks for organizing data in computer science. Understanding how they work and when to use each one is a key step in writing clean, efficient, and effective code.
