















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
















AL 1
We have designed the interface for the List; we now must consider how to implement that interface.
AL 2
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:
A (^6 8 7 ) 1 2 3 4 5
2 current 3 size 5
AL 4
next():
current 4
size 6
A (^6 8 7 ) 1 2 3 4 5
2 6
9 5
AL 5
next():
current 4
size 6
A (^6 8 7 ) 1 2 3 4 5
2 6
9 5
There are special cases for positioning the current pointer: a. past the last array cell b. before the first cell
AL 7
remove(): removes the element at the current index
current 5
size 6
A (^6 8 ) 1 2 3 4 5
2 6
9 5
Step 1:
current 5
size 5
A (^6 8 ) 1 2 3 4 5
Step 2: 2 9
AL 8
remove(): removes the element at the current index
We fill the blank spot left by the removal of 7 by shifting the values to the right of position 5 over to the left one space.
current 5
size 5
A (^6 8 ) 1 2 3 4 5
Step 2: 2 9
current 5
size 6
A (^6 8 ) 1 2 3 4 5
2 6
9 5
Step 1:
AL 10
Other operations:
get() return A[current]; update(X) A[current] = X; length() return size; back() current--; start() current = 1; end() current = size;
AL 11
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 13
Various cells of memory are not allocated consecutively in memory.
AL 14
Various cells of memory are not allocated consecutively in memory.
Not enough to store the elements of the list.
AL 16
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
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 19
Picture of our list (2, 6, 7, 8, 1) stored as a linked list:
2 6 8 7 1
head
current
size=
AL 20
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.