Lists and Linked Lists


Contents

What is a list?

A list is an abstract data type that contains a number of items of the same datatype. This can be a list of arrays or a list of characters.

Examples

Example using integers

[1,6,3,4,2]

Example using characters

['a','b','c','d']

Operations on a list

In this example I will use the list [A,B,C,D]. This list will be called demo

OperationExplanationDemoList after operationReturn
isEmptyTest to see if the list is emptydemo.isEmpty()[A,B,C,D]False
appendAppends onto the listdemo.append(E)[A,B,C,D,E]Null
removeRemove a item from the listdemo.remove(A)[B,C,D]Null
searchSearches for an item and returns the index of that itemdemo.search(C)[A,B,C,D]2
lengthGet the length of the listdemo.length()[A,B,C,D]4
insertInsert an item into the list with a given positon.demo.insert(X,1)[A,X,B,C,D]Null
popRemove the last item off the list and return that item.demo.pop()[A,B,C]D
pop with positionSimilar to remove but returns the removed item.demo.pop(1)[A,C,D]B

***


Linked lists

A linked list is a dynamic data structure that holds a ordered data structure, where each element points to the next element.