No history yet

Introduction to Lists

What is a List?

At its core, a list in computer science is just what it sounds like: a collection of items in a specific order. Think of a grocery list or a to-do list. The first item is the first item, the second is the second, and so on. This sequence is what makes lists so useful. They provide a straightforward way to store and manage data that has a natural order.

Whether it's a list of users for a website, scores in a game, or steps in a recipe, the data is stored as a sequence. This ordered nature allows us to access items by their position, like finding the 5th person in a line.

A list is an ordered collection of data. The position of each item matters.

Common List Operations

To work with lists, we use a few fundamental operations. These are the basic actions that let us manage the data inside.

  • Insertion: Adding a new item to the list. You can add it at the beginning, at the end, or somewhere in the middle.
  • Deletion: Removing an item from the list.
  • Traversal: Going through the list and visiting each item, one by one, from start to finish.
  • Search: Looking for a specific item in the list to see if it's there and find its location.

How Lists Are Built

While the idea of a list is simple, there are different ways to construct one in a computer's memory. The two most common approaches are array-based lists and linked lists. They both store an ordered collection of data, but they do it in fundamentally different ways.

Think of it like this: an array-based list is like a pill organizer, with a fixed number of compartments all in a row. A linked list is like a scavenger hunt, where each clue tells you where to find the next one.

Lesson image

In an array-based list, all the items are stored right next to each other in memory. This makes it very fast to jump to any item if you know its position. However, adding or removing items from the middle can be slow, because you might have to shift many other items to make space or close a gap.

In a linked list, each item also stores a reference, or a "link," to the next item in the sequence. The items themselves can be scattered all over memory. This structure makes it much easier to insert or delete items, as you only need to change a couple of links. But, finding an item at a specific position can be slow, because you have to follow the links from the very beginning.

Let's test your understanding of these basic concepts.

Quiz Questions 1/5

What is the most defining characteristic of a list in computer science?

Quiz Questions 2/5

If you go through a list, examining each item one by one from the first to the last, which fundamental operation are you performing?

Understanding these core ideas about lists sets the foundation for exploring how different data structures work. The choice between an array-based list and a linked list depends entirely on what you need to do with your data.