Академический Документы
Профессиональный Документы
Культура Документы
02
Data Structures
Dr. Sohail Aslam
Implementing Lists
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
3
size
5
List Implementation
step 2:
A
A
current
3
size
5
current
4
size
6
Implementing Lists
next():
A
current
4
5
size
6
Implementing Lists
Implementing Lists
Implementing Lists
Step 2:
1
5
current
5
size
6
current
5
size
5
Implementing Lists
Step 2:
1
5
current
5
size
6
current
5
size
5
Implementing Lists
find(X): traverse the array until X is located.
int find(int X)
{
int j;
for(j=1; j < size+1; j++ )
if( A[j] == X ) break;
if( j < size+1 ) { // found X
current = j;
// current points to where X found
return 1; // 1 for true
}
return 0; // 0 (false) indicates not found
}
Implementing Lists
Other operations:
get()
update(X)
length()
back()
start()
end()
return A[current];
A[current] = X;
return size;
current--;
current = 1;
current = size;
add
remove
find
Linked List
next
Linked List
6
current
size=5
Linked List
Note some features of the list:
Need a head to point to the first node of the list.
Otherwise we wont know where the start of the
list is.
Linked List
Note some features of the list:
Need a head to point to the first node of the list.
Otherwise we wont know where the start of the
list is.
The current here is a pointer, not an index.
Linked List
Note some features of the list:
Need a head to point to the first node of the list.
Otherwise we wont know where the start of the
list is.
The current here is a pointer, not an index.
Linked List
head
2
1051
1052
1063
1053
1063
1054
1055
1051
1056
current
1057
1058
1060
1059
head
1060
1061
1062
1054
1063
1064
1057
1065