Professional Documents
Culture Documents
Data Structures Lec 2
Data Structures Lec 2
02
Data Structures
AL 1
Implementing Lists
▪ We have designed the interface for the List; we
now must consider how to implement that
interface.
AL 2
Implementing Lists
▪ We have designed the interface for the List; we
now must consider how to implement that
interface.
▪ Implementing Lists using an array: for example,
the list of integers (2, 6, 8, 7, 1) could be
represented as:
current size
A 2 6 8 7 1
3 5
1 2 3 4 5
AL 3
List Implementation
▪ add(9); current position is 3. The new list would thus be:
(2, 6, 8, 9, 7, 1)
▪ We will need to shift everything to the right of 8 one
place to the right to make place for the new element ‘9’.
current size
step 1: A 2 6 8 7 1
3 5
1 2 3 4 5 6
AL 5
Implementing Lists
AL 6
Implementing Lists
AL 7
Implementing Lists
▪ remove(): removes the element at the current
index
Step 1: current size
A 2 6 8 9 1
5 6
1 2 3 4 5 6
5
current size
Step 2: A 2 6 8 9 1
5 5
1 2 3 4 5
AL 8
Implementing Lists
▪ remove(): removes the element at the current
index
Step 1: current size
A 2 6 8 9 1
5 6
1 2 3 4 5 6
5
current size
Step 2: A 2 6 8 9 1
5 5
1 2 3 4 5
int find(int X)
{
int j;
for(j=1; j < size+1; j++ )
if( A[j] == X ) break;
AL 11
Analysis of Array Lists
▪ 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
AL 12
Analysis of Array Lists
▪ 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.
AL 13
List Using Linked Memory
▪ Various cells of memory are not allocated
consecutively in memory.
AL 14
List Using Linked Memory
▪ Various cells of memory are not allocated
consecutively in memory.
▪ Not enough to store the elements of the list.
AL 15
List Using Linked Memory
▪ Various cells of memory are not allocated
consecutively in memory.
▪ Not enough to store the elements of the list.
▪ With arrays, the second element was right next
to the first element.
AL 16
List Using Linked Memory
▪ Various cells of memory are not allocated
consecutively in memory.
▪ Not enough to store the elements of the list.
▪ With arrays, the second element was right next
to the first element.
▪ Now the first element must explicitly tell us
where to look for the second element.
AL 17
List Using Linked Memory
▪ Various cells of memory are not allocated
consecutively in memory.
▪ Not enough to store the elements of the list.
▪ With arrays, the second element was right next
to the first element.
▪ Now the first element must explicitly tell us
where to look for the second element.
▪ Do this by holding the memory address of the
second element
AL 18
Linked List
▪ Create a structure called a Node.
object nex
t
AL 19
Linked List
▪ Picture of our list (2, 6, 7, 8, 1) stored as a linked
list:
head
2 6 8 7 1 size=5
current
AL 20
Linked List
Note some features of the list:
▪ Need a head to point to the first node of the list.
Otherwise we won’t know where the start of the
list is.
AL 21
Linked List
Note some features of the list:
▪ Need a head to point to the first node of the list.
Otherwise we won’t know where the start of the
list is.
▪ The current here is a pointer, not an index.
AL 22
Linked List
Note some features of the list:
▪ Need a head to point to the first node of the list.
Otherwise we won’t know where the start of the
list is.
▪ The current here is a pointer, not an index.
▪ The next field in the last node points to nothing.
We will place the memory address NULL which
is guaranteed to be inaccessible.
AL 23
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
AL 24