Lists

A list is an abstract data structure similar to an array. It can hold multiple values or objects of the same datatype.

The key difference between a list and an array is that a list should be a dynamic data structure – that is that it expands as more items are added, and is not fixed when it is declared.

Along with stacks and queues, lists are a commonly used and useful data structure.

Items in a list may be added to and removed from any point of a list (unlike a queue or stack).

You might use a list for:

  • Shopping basket on a website

  • An inventory in an adventure game

  • Students in a class

There are a number of operations you would expect to perform on a list:

  • Add – Adds an item to the list

  • Contains – Checks to see if the specified item is in the list (returns True/False)

  • IndexOf – returns the index of the first occurrence of a specified item

  • ElementAt – returns the item at a specified index

  • Clear – remove all items from the list

  • Count – returns the number of items in the list

  • InsertAt – adds an item to the list and the specified index

  • Remove – removes the first occurrence of a specified item

  • RemoveAt – removes an item at a specified index

Most languages have a way of creating lists as a predefined data structure, but this is not always the case.
We will look at 2 methods of implementing our own lists: