Google Play badge

array and linked lists


Arrays and Linked Lists

Welcome to our lesson on arrays and linked lists. In this lesson, we will learn two simple ways to store and organize data. Imagine you have a row of toy boxes or a line of lockers at school. Arrays and linked lists work in a similar way. They help us keep things neat and easy to find. This lesson is written in simple language with everyday examples to help you understand these ideas easily.

Introduction

Data structures help computers store and organize information. Two important data structures are arrays and linked lists. You can think of an array as a row of boxes, and a linked list as a chain of connected clues on a treasure hunt. Both of these help us keep track of many items, like toys, books, or even your favorite snacks.

We will talk about what an array is, what a linked list is, how they work, and how they are different. We will also see real-world examples that make these ideas as clear as possible.

What is an Array?

An array is just a collection of items. It is like a row of boxes where each box holds one item. For example, imagine a set of five boxes lined up in a row. You can use each box to store a favorite toy or a snack.

Each box in an array has a number called an index. The first box is usually numbered 0, the next one is 1, then 2, and so on. This numbering helps you quickly find a specific item. For instance, if you want the item in the third box, you simply look at the box with index 2.

Here is a simple formula to explain how we can find an item in an array. If the first box is at a starting point, then the address of any item can be thought of as:

\( \textrm{Address}(A(i)) = \textrm{Address}(A(0)) + i \times \textrm{(size of one item)} \)

This tells us that to move from the first box to the box we want, you count forward a certain number of spaces.

Properties of Arrays

Think of an array like the seats in a small movie theater. Each seat has a number, and you can quickly go to your seat if you know its number.

Everyday Example of an Array

Imagine your school has a row of lockers, each with a unique number. When you go to put your bag in your locker, you use the specific number on the locker. In an array, each locker is like a box, and the number tells you the exact spot where your bag—or data—is kept.

What is a Linked List?

A linked list is another way to store items. It is different from an array because it does not use a long row of fixed boxes. Instead, it uses special boxes called nodes. Each node holds an item and also has a pointer that tells you where the next node is.

Imagine you are on a treasure hunt. Each clue you find tells you where the next clue is hidden. In a linked list, each node is like one of these clues. When you start at the first clue, you follow the pointer from one node to the next until you find what you need.

You can think of each node as a small envelope. The envelope carries a card (the data) and also a note (the pointer). This note tells you which envelope comes next in the line.

How a Linked List Works

Let’s look at a simple way of writing what a node is:

Node = {data, pointer)

The "data" in a node is the information stored, and the "pointer" is like an arrow that directs you to the next node. Unlike an array, a linked list does not require all nodes to be next to each other in memory; they can be anywhere, as long as the pointers connect them.

Types of Linked Lists

There are different styles of linked lists. Here are three common kinds:

Real-World Example of a Linked List

Imagine you are following a treasure map. Each step on the map tells you where the next step is. Even if you add an extra clue or remove one, you can still follow along by reading the clue on each card. This is how a linked list works. Each node (or clue) is connected to the next one, letting you move through the list one step at a time.

Comparison Between Arrays and Linked Lists

Arrays and linked lists both help us store items, but they do it in different ways. Here are some comparisons:

Advantages and Disadvantages

Each data structure has its good parts and its challenges. Understanding these helps you choose the best one to use.

Arrays:

Advantages:

Disadvantages:

Linked Lists:

Advantages:

Disadvantages:

Working with Arrays

Let’s see how we can use an array in a simple way. Suppose you want to store your five favorite colors. You create an array with five boxes. Then you put each color into a box in order. For example:

Now, if you want to know which color is in Box 2, you simply look at that box and you will see "Green." This easy access is one of the best parts of using an array.

Working with Linked Lists

Now, let’s look at a linked list. Think of this as a treasure hunt where you start with a clue and then follow instructions to find the next one. In a linked list, we start with a node that contains some data. This node has a pointer that shows which node comes next.

For example, imagine you have three nodes in a linked list that tell a fun story:

You start at Node 1 and follow the pointer (the clue) to Node 2, then to Node 3. Even if you want to add a new clue between any of these, you only need to change a few pointers. This makes linked lists very flexible.

Visualizing Arrays and Linked Lists

It is helpful to picture these data structures in your mind. Picture an array as a long row of clear, labeled boxes on a shelf. Each box holds something and has a fixed place. Now, picture a linked list as a string of cards. Each card has a note indicating where the next card is hidden. In an array, you can jump directly to a specific box by its number. In a linked list, you need to follow the cards in order.

Everyday Applications

Arrays are used in many everyday things. For example, imagine a calendar. A calendar has a fixed number of days in each week, and those days are arranged in a row. When you look at the calendar, you know exactly which day is in which spot.

Linked lists are used when the number of items might change over time. Think of a line of people waiting at an ice cream truck. Sometimes new people join the line, and sometimes someone leaves. The line can grow or shrink without having to create a new fixed structure. This makes linked lists very useful in scenarios where things change often.

Customizing Data Storage

Choosing between arrays and linked lists depends on what you need to do with your data. If you know you will always have a fixed number of items—like the days in a week—then an array is very suitable. However, if the amount of data changes and you need a structure that can easily adapt, a linked list will be a better choice.

For instance, in a computer game, an array might be used to store the scores for each level because the number of levels is fixed. On the other hand, a linked list might be used to manage a list of player actions or moves, which can grow as the game continues.

How to Decide Which to Use

When you need fast access to items by their position, arrays are the best choice. This is because you can directly jump to any spot if you know its number. However, when you need to frequently add or remove items, linked lists are more helpful because they allow you to change the list without moving many items around.

Think about it this way: if you have a sticker album with a set number of pages, an array is like that album. But if you have a growing collection of postcards that you add to a bulletin board, a linked list is more like that because you can easily add a new postcard in between others without rearranging the whole board.

Key Points and Summary

Let us review the main points of our lesson:

Arrays:

Linked Lists:

Differences and Uses:

In summary, arrays and linked lists are two important data structures used to organize data. Arrays work like a row of fixed, numbered boxes, while linked lists work like a treasure hunt where each step tells you where to go next. Both methods have their own strengths and are used in different situations based on the needs of the task.

Understanding these two methods of data storage is very useful. Many computer programs, games, and applications use arrays and linked lists in the background. By learning how they work, you gain insight into the way computers organize and manage data.

Remember: arrays are simple and fast when the structure is fixed, while linked lists offer flexibility when the data changes. Whether you imagine a row of lockers or a treasure trail of clues, these concepts help us make sense of how information is stored and used every day.

This lesson has given you a clear idea of what arrays and linked lists are. As you continue to learn and explore computer science, these basic ideas will help you understand more complex topics. They are the building blocks of more advanced data structures and algorithms.

Summary of Key Points:

Thank you for reading this lesson on arrays and linked lists. We hope you have enjoyed learning about these methods for storing data in a clear and simple way. As you grow and learn more, remember these basic structures and how they help make computers work efficiently.

Download Primer to continue