Professional Documents
Culture Documents
Iftikhar Muhammad
1
Iftikhar 1
DS & Algorithms:2013
Real life:
a. shopping list,
b. groceries list,
c. list of people to invite to dinner
d. List of presents to get
Iftikhar 2
DS & Algorithms:2013
Lists
Iftikhar 3
DS & Algorithms:2013
Lists
Iftikhar 4
DS & Algorithms:2013
Lists
Iftikhar 5
DS & Algorithms:2013
Lists
Lists
List Operations
Useful operations
• createList(): create a new list
• copy(): set one list to be a copy of another
• clear(); clear a list (remove all elments)
• insert(X, ?): Insert element X at a particular position
in the list
• remove(?): Remove element at some position in
the list
• get(?): Get element at a given position
• update(X, ?): replace the element at a given position
with X
• find(X): determine if the element X is in the list
• length(): return the length of the list.
Iftikhar 8
DS & Algorithms:2013
List Operations
Iftikhar 9
DS & Algorithms:2013
List Operations
Iftikhar 10
DS & Algorithms:2013
List Operations
Iftikhar 11
DS & Algorithms:2013
Implementing Lists
Iftikhar 12
DS & Algorithms:2013
Implementing Lists
current size
A 2 6 8 7 1
3 5
1 2 3 4 5
Iftikhar 13
DS & Algorithms:2013
List Implementation
current size
step 1: A 2 6 8 7 1 3 5
1 2 3 4 5 6
current size
step 2: A 2 6 8 9 7 1
4 6
1 2 3 4 5 6
Implementing Lists
next():
current size
A 2 6 8 9 7 1
4 6
1 2 3 4 5 6 5
Iftikhar 15
DS & Algorithms:2013
Implementing Lists
Iftikhar 16
DS & Algorithms:2013
Implementing Lists
Iftikhar 17
DS & Algorithms:2013
Implementing Lists
current size
Step 2: A 2 6 8 9 1
5 5
1 2 3 4 5
Iftikhar 18
DS & Algorithms:2013
Implementing Lists
current size
Step 2: A 2 6 8 9 1
5 5
1 2 3 4 5
Implementing Lists
int find(int X)
{
int j;
for(j=1; j < size+1; j++ )
if( A[j] == X ) break;
Implementing Lists
Other operations:
Iftikhar 21
DS & Algorithms:2013
add
we have to move every element to the right of
current to make space for the new element.
Worst-case is when we insert at the beginning; we
have to move every element right one place.
Average-case: on average we may have to move
half of the elements
Iftikhar 22
DS & Algorithms:2013
remove
Worst-case: remove at the beginning, must shift all
remaining elements to the left.
Average-case: expect to move half of the elements.
find
Worst-case: may have to search the entire array
Average-case: search at most half the array.
Iftikhar 23
DS & Algorithms:2013
Iftikhar 24
DS & Algorithms:2013
Iftikhar 25
DS & Algorithms:2013
Iftikhar 26
DS & Algorithms:2013
Iftikhar 27
DS & Algorithms:2013
Iftikhar 28
DS & Algorithms:2013
Linked List
object next
Iftikhar 29
DS & Algorithms:2013
Linked List
head
2 6 8 7 1 size=5
current
Iftikhar 30
DS & Algorithms:2013
Linked List
Iftikhar 31
DS & Algorithms:2013
Linked List
Iftikhar 32
DS & Algorithms:2013
Linked List
Iftikhar 33
DS & Algorithms:2013
Linked List
Actual picture in memory:
1051 6
1052 1063
current 1053 1063
1054 2
head 1055 1051
1056
2 6 8 7 1 1057 7
1058 1060
current 1059
1060 1
1061 0
head 1062 1054
1063 8
1064 1057
1065
Iftikhar 34