reading-notes

Linked Lists

Reference

What is a Linked List?

A linked list is a linear data structure in which each element is a separate object that is connected to the next element in the list using a pointer. Linked lists are dynamic data structures, meaning they can grow or shrink as needed during the execution of a program. This is in contrast to arrays, which have a fixed size and must be created with that size in mind.

Advantages of Linked Lists

One advantage of linked lists is that they can be easily inserted or removed from the middle of the list, because there is no need to shift elements around to make room for a new element or to fill the gap left by a deleted element. This makes linked lists well-suited for situations where elements will be added or removed frequently.

Disadvantages of Linked Lists

However, there are also some trade-offs to consider when using linked lists. One disadvantage is that they are generally slower than arrays when it comes to accessing individual elements, because each element must be accessed by following the pointers from one element to the next. This can make linked lists less efficient for situations where elements will be accessed randomly or frequently.

Another disadvantage of linked lists is that they require more memory than arrays, because each element in a linked list requires an additional pointer to store the address of the next element in the list. This can make linked lists less memory-efficient than arrays, especially for large lists.

Summary

Overall, the choice between using a linked list or an array will depend on the specific needs of the application. If you need to frequently insert or delete elements and memory usage is not a concern, a linked list may be a good choice. If you need fast access to individual elements and memory usage is a concern, an array may be a better choice.

Terminology

Linked List Example

Linked List

Traversing a Linked List

Adding a Node