You are on page 1of 31

LISTS

The List ADT


 General list of the form
𝐴0 , 𝐴1 , 𝐴2 , … , 𝐴𝑁−1
 List size : N
 Empty list : a list of size 0
 First element is 𝐴0
 Last element is 𝐴𝑁−1
List Operations
 makeEmpty
 Empties the list
 printList
 Prints the elements in the list
 find
 Returns the position of the first occurrence of an item
List Operations
 insert
 Insert some element at some position in the list
 remove
 Remove some element from some position in the list
 findKth
 Return the element in some position in the list
List Operations
 Example:
34 12 52 16 12

1. find(52) // return 2
2. insert(70, 2) // 34 12 70 52 16 12
3. remove(2) // 34 12 70 16 12
List Implementations
 Using arrays
 Using linked list
Array Implementation of Lists

0 1 2 3 4 5 6 7 8 9

A 52 48 16 25 31 5 33 11 13 40

 printList - linear time


 find - linear time
 find(31) // 4
Array Implementation of Lists

0 1 2 3 4 5 6 7 8 9

A 52 48 16 25 31 5 33 11 13 40

 findKth - constant time


 findKth(4) // 31
Array Implementation of Lists
 insert(item, index) - O(n)

0 1 2 3 4 5 6 7 8 9

A 52 48 16 25 31 5

 insert(70, 2)

0 1 2 3 4 5 6 7 8 9

A 52 48 70 16 25 31 5
Array Implementation of Lists
 insert(item, index) - O(n)

0 1 2 3 4 5 6 7 8 9

A 52 48 16 25 31 5 33 11 13 40

 insert(70, 2)
Array Implementation of Lists
 insert(item, index) - O(n)

0 1 2 3 4 5 6 7 8 9

A 52 48 16 25 31 5 33 11 13 40

 insert(70, 2)
 Display error message then terminate
 Create bigger array and copy contents of smaller
array to new array
Array Implementation of Lists
 insert(item, index) -

0 1 2 3 4 5 6 7 8 9

A 52 48 16 25 31 5

 insert(70, n) //n is the # of elements in the array


 Insertion at the end of the list
 O(1)
Array Implementation of Lists
 remove(index) - O(n)

0 1 2 3 4 5 6 7 8 9

A 52 48 16 25 31 5

 remove(0)

0 1 2 3 4 5 6 7 8 9

A 48 16 25 31 5
Array Implementation of Lists
 remove(index) - O(n)

0 1 2 3 4 5 6 7 8 9

A 52 48 16 25 31 5

 remove(7)
Array Implementation of Lists
 remove(index) - O(n)

0 1 2 3 4 5 6 7 8 9

A 52 48 16 25 31 5

 remove(7)
 Display error message then terminate
Array Implementation of Lists
 remove(index)

0 1 2 3 4 5 6 7 8 9

A 52 48 16 25 31 5

 remove(n-1) //n is the # of elements in the array


 Remove the last element in the list
 O(1)
Linked list Implementation of List
 A linked list

 Consists of nodes
Linked list Implementation of List
 Nodes

 Nodes not necessarily adjacent in memory


 Node contains an element and link to next element
Linked list Implementation of List
 A linked list

 beginMarker
 endMarker
Linked list Implementation of List
 A linked list

 printList - linear time


 find - linear time

start at first node and traverse the list following the


next link
Linked list Implementation of List
 A linked list

 findKth - linear
 findKth(4)

start at first node and traverse the list following until the
fourth element
Linked list Implementation of List
 Insertion into a linked list
Linked list Implementation of List
 Deletion from a linked list
Linked list Implementation of List
 To avoid the linear time operations for adding and
removing elements, we will think of lists as formed
by putting together Nodes that can be connected

next

previous
Linked list Implementation of List
 To avoid the linear time operations for adding and
removing elements, we will think of lists as formed
by putting together Nodes that can be connected
Linked list Implementation of List
 Doubly linked list with header and tail nodes
Linked list Implementation of List
 Doubly linked list with header and tail nodes
Linked list Implementation of List
 Insertion in a doubly linked list by getting new node
and then changing the pointers in the order
indicated
Linked list Implementation of List
 Removing node specified by p from a doubly linked
list
Summary

Operation ArrayList LinkedList


find(x)
findKth(i)
remove(x)
remove(x)
-last element
insert(item, index)
insert(item, index)
-at the end of the list

You might also like