What is a List?
A list is a linear data structure, meaning its elements are arranged in a sequence. Each element in the list can be accessed using an index value.
- Example: Storing average temperatures for each day of a week:
- Index 0: Temperature for Sunday
- Index 1: Temperature for Monday
- ...and so on
Common List Operations
Operations performed on lists include:
- Retrieve: Access an element by its index
- Update: Change the value of an element at a specific index
- Insert: Add a new element at a specific index, shifting subsequent elements
- Delete (Remove): Remove an element at a specific index, shifting subsequent elements up
- Append: Add a new element to the end of the list
Example Operations:
- Retrieve: Get the value at index 4
- Update: Replace the value at index 4 with 55
- Insert: Add 25 at index 2
- Remove: Delete the element at index 6
- Append: Add 80 to the end of the list
List Traversal
List traversal involves accessing each element of a list one-by-one. This can be done from the first element to the last or vice versa.
- Example: Summing all elements in a list:
- Access each element sequentially and add them up.