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

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

The items which form the sequence are net necessarily held in contiguous data locations, cr in the order in which they occur in the sequence.

Each item in the list is called a node and contains a data field and a next address field called a link or pointer field (the data field may consist of several subfields.) • ’ he data field holds the actual data associated with the list item, and the pointer field contains the address of the next item in the sequence • ’ he link Held in the last item indicates that there are nc further items by the use of a null pointer • Associated with the list is a pointer variable which points to (i.e. contains the address of) the first node in the list