Most any data structure uses one of two fundamental approaches in its underlying data representation:
We've previously covered the contiguous memory approach. In this lecture, we'll begin to explore linked structures. As an initial example, we'll implement a linked list.
|
1: Sequential Containers and Data Structures
First, let's briefly review the kinds of sequential containers we'd like to build and the applications they're used for. |
|
2: Arrays vs. Linked Lists
2.1
Data structures generally fall into one of two fundamental for these containers generally use one of two fundamental approaches: Option 1 - Contiguous Memory: We've covered this throughout the last several lectures on array-based data structures and growable containers. The use of a contiguous block of memory allows for efficient indexing, but insert/erase operations in the middle of the array incur a linear complexity due to the need to shift elements (assuming we're dealing with an ordered container where we must preserve the relative ordering of elements). Option 2 - Linked Structures: The video below introduces the general idea of a linked-list as the alternative approach, gives a preliminary look at its data representation, and compares/contrasts the efficiency of various operations on linked-lists vs. arrays.
2.1 Exercise: Arrays vs. Linked Structures
Describe one of the operations that can be performed more efficiently on an array than on a linked list. Why is this the case and what are the relevant time complexities? Describe one of the operations that can be performed more efficiently on a linked list than on an array. Why is this the case and what are the relevant time complexities? There is no walkthrough video for this question, but you can refer back to the video above for examples. |
|
3: Pros/Cons Recap
This section provides a brief summary of the pros/cons of arrays vs. linked lists, which are discussed at length in the previous section and its video. Array-Based Ordered Container:
*The complexity for operations that push or insert an element technically have worst-case complexity - this occurs when the array is currently full and data must be copied to a new array with 2x capacit. However, this amounts to an amortized if considered over the next N operations that don't require growing the array. Linked List:
|
|
4: Intro to Linked Lists
4.1
Fall 2025In some of the videos below, I might refer to implementing a linked list on project 4. That's project 5 this term. Here we'll consider building an ADT for a linked Linked List, which is the simplest linked data structure. The key idea is that we implement a sequential container by storing several nodes (each individually allocated in dynamic memory) that contain element values and a pointer to the next node in the sequence. There's no requirement that the nodes are contiguous in memory. Specifically, we'll start with a "singly-linked, single-ended" list, which we'll call
4.1 Exercise: Linked List Representation Invariants
Here again is the basic data representation for a linked list:
Brainstorm three different representation invariants for the linked list's data representation. Think about what things must be true for the node structure to make sense, or for functions working with the structure to be able to do their job correctly. Sample solution…
|
|
5: Linked List Implementation
5.1
Let's keep working on the
5.1 Exercise:
ForwardList::pop_front()
Implement the
Theme:
// This exercise is not automatically graded.
// However, getting a correct answer is a bit tricky.
// I highly suggest you check the walkthrough video.
You're welcome to check your solution with this walkthrough video: |
|
6: Traversing a Linked List
6.1
With a data structure based on contiguous memory, walking through increasing indices or addresses is a natural approach. With a linked structure, however, this doesn't work (we can't rely on the contiguous memory assumption anymore). Instead, we have to follow
6.1 Exercise:
ForwardList::print()
Implement the
Theme:
// This exercise is not automatically graded.
// You can check your solution against the walkthrough video.
You're welcome to check your solution with this walkthrough video: |
|
7: Doubly-Linked, Double-Ended Lists
7.1
Let's take a look at three upgrades to our data representation and where they make a difference in terms of efficiency:
7.1
Which of the following are true? |
|
8: The Big Three
One more thing - since our class manages dynamically allocated nodes, we'll need custom implementations of "the big three". (Don't be tempted to skip this video - it includes implementations that are directly relevant to the linked list you implement in the project, including a few places with subtle pitfalls!) |