Data Structures and Algorithms Foundations
Introduction to Data Structures
Organizing Information
Think about how you organize things in real life. You might arrange books on a shelf alphabetically, sort your clothes by color, or make a to-do list with the most important task at the top. You do this to find and use things more easily. Computers need to do the same thing with data.
A data structure is simply a way of organizing and storing data in a computer so that it can be accessed and modified efficiently. Different structures are good for different tasks. We'll look at four fundamental types: arrays, linked lists, stacks, and queues.
The Array
An array is one of the simplest data structures. Imagine a long row of numbered boxes, all lined up next to each other in memory. Each box can hold one piece of data, and because they're numbered (starting from 0), you can instantly jump to any box you want just by knowing its number, or index.
This direct access is the array's superpower. If you have an array of 100 items and you need the 75th one, you can go straight to it without looking at any others. The main trade-off is that arrays usually have a fixed size. If you create an array with 10 boxes, you can't easily add an 11th one later.
// A simple array to hold high scores
// Indices are 0, 1, and 2
int[] highScores = { 5200, 4850, 4700 };
// To get the top score, we access index 0
int topScore = highScores[0]; // topScore is now 5200
Use an array when you know how many items you'll have and need to access them quickly by their position.
The Linked List
What if you don't know how many items you'll need, and you'll be adding or removing them often? That's where a linked list shines. Unlike an array's neat row of boxes, a linked list is more like a scavenger hunt. Each item, called a node, holds a piece of data and a pointer to the location of the next node in the sequence. They don't have to be next to each other in memory.
This structure makes it very easy to insert or delete items. To add a new item between the first and second, you just change a few pointers. You don't have to shift all the other items over like you would in an array. The downside? If you want to get to the 75th item, you have to start at the beginning and follow the chain through 74 links. There's no jumping straight to it.
A music player's playlist is a great example. You can easily add, remove, and reorder songs (nodes) without rebuilding the entire list.
Stacks and Queues
Sometimes, the rules for how you add and remove data are more important than the underlying structure. Stacks and queues are two such data structures defined by their access rules.
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 put a clean plate on top (push), and when you need one, you take the one from the top (pop). You don't grab one from the bottom. This is the LIFO principle: Last-In, First-Out. The "undo" function in a word processor works like this. Each action you take is pushed onto a stack, and when you hit "undo," the last action is popped off.
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.
A queue is the opposite. It's like a line at a grocery store. The first person to get in line is the first person to check out. This is FIFO: First-In, First-Out. When you send multiple documents to a printer, they go into a print queue. The printer handles the first document it received, then the second, and so on. The operation to add to the back of the queue is called enqueue, and removing from the front is called dequeue.
Choosing the right data structure is a fundamental part of writing effective and efficient software. By understanding how each one works, you can select the perfect tool for the job, whether you're building a simple to-do list or a complex application.
Ready to check your understanding? Let's see what you've learned.
Which data structure is characterized by the Last-In, First-Out (LIFO) principle?
What is the main advantage of using an array over a linked list?
These four structures are the building blocks for many complex programs. Getting comfortable with them is the first step toward mastering how software handles information.
