8/3/2019 Computer Notes - Data Structures - 2
1/24
Class No.02
Data Structures
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 Computer Notes - Data Structures - 2
2/24
Implementing Lists
We have designed the interface for the List; wenow must consider how to implement thatinterface.
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 Computer Notes - Data Structures - 2
3/24
Implementing Lists
We have designed the interface for the List; wenow must consider how to implement thatinterface.
Implementing Lists using an array: for example,the list of integers (2, 6, 8, 7, 1) could berepresented as:
A
6 8 7 11 2 3 4 5
2 current3
size5
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 Computer Notes - Data Structures - 2
4/24
List Implementation
add(9); current position is 3. The new list would thusbe: (2, 6, 8, 9, 7, 1)
We will need to shifteverything to the right of 8 oneplace to the right to make place for the new element 9.
current
3
size
5step 1: A
6 8 7 1
1 2 3 4 5
2
6
current
4
size
6step 2: A 6 8 7 1
1 2 3 4 5
2
6
9
notice: current points
to new elementhttp://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 Computer Notes - Data Structures - 2
5/24
Implementing Lists
next():
current
4
size
6A
6 8 7 1
1 2 3 4 5
2
6
9
5
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 Computer Notes - Data Structures - 2
6/24
Implementing Lists
There are special cases for positioning thecurrent pointer:
a. past the last array cell
b. before the first cell
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 Computer Notes - Data Structures - 2
7/24
Implementing Lists
There are special cases for positioning thecurrent pointer:
a. past the last array cellb. before the first cell
We will have to worry about these when we
write the actual code.
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 Computer Notes - Data Structures - 2
8/24
Implementing Lists
remove(): removes the element at the currentindex
current
5
size
6
A 6 8 1
1 2 3 4 5
2
6
9
5
Step 1:
current
5
size
5A 6 8 1
1 2 3 4 5
2 9Step 2:
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 Computer Notes - Data Structures - 2
9/24
Implementing Lists
remove(): removes the element at the currentindex
We fill the blank spot left by the removal of 7 byshifting the values to the right of position 5 overto the left one space.
current
5
size
5A 6 8 1
1 2 3 4 5
2 9Step 2:
current
5
size
6
A 6 8 1
1 2 3 4 5
2
6
9
5
Step 1:
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 Computer Notes - Data Structures - 2
10/24
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 Xcurrent = j; // current points to where X found
return 1; // 1 for true}return 0; // 0 (false) indicates not found
} http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 Computer Notes - Data Structures - 2
11/24
Implementing Lists
Other operations:
get() return A[current];
update(X) A[current] = X;length() return size;
back() current--;
start()
current = 1;end() current = size;
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 Computer Notes - Data Structures - 2
12/24
Analysis of Array Lists
add
we have to move every element to the right ofcurrent to make space for the new element.
Worst-case is when we insert at the beginning; wehave to move every element right one place.
Average-case: on average we may have to movehalf of the elements
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 Computer Notes - Data Structures - 2
13/24
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.
Other operations are one-step.
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 Computer Notes - Data Structures - 2
14/24
List Using Linked Memory
Various cells of memory are not allocatedconsecutively in memory.
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 Computer Notes - Data Structures - 2
15/24
List Using Linked Memory
Various cells of memory are not allocatedconsecutively in memory.
Not enough to store the elements of the list.
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 Computer Notes - Data Structures - 2
16/24
List Using Linked Memory
Various cells of memory are not allocatedconsecutively in memory.
Not enough to store the elements of the list.
With arrays, the second element was right nextto the first element.
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 Computer Notes - Data Structures - 2
17/24
List Using Linked Memory
Various cells of memory are not allocatedconsecutively in memory.
Not enough to store the elements of the list.
With arrays, the second element was right nextto the first element.
Now the first element must explicitlytell us
where to look for the second element.
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 Computer Notes - Data Structures - 2
18/24
List Using Linked Memory
Various cells of memory are not allocatedconsecutively in memory.
Not enough to store the elements of the list.
With arrays, the second element was right nextto the first element.
Now the first element must explicitlytell us
where to look for the second element. Do this by holding the memory address of the
second element
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 Computer Notes - Data Structures - 2
19/24
Linked List
Create a structure called a Node.
object next
The objectfield will hold the actual list element.
The nextfield in the structure will hold the
starting location of the next node. Chain the nodes together to form a linked list.
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 Computer Notes - Data Structures - 2
20/24
Linked List
Picture of our list (2, 6, 7, 8, 1) stored as alinked list:
2 6 8 7 1
head
current
size=5
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 Computer Notes - Data Structures - 2
21/24
Linked List
Note some features of the list:
Need a headto point to the first node of the list.
Otherwise we wont know where the start of the
list is.
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 Computer Notes - Data Structures - 2
22/24
Linked List
Note some features of the list:
Need a headto point to the first node of the list.
Otherwise we wont know where the start of the
list is.
The currenthere is a pointer, not an index.
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 Computer Notes - Data Structures - 2
23/24
Linked List
Note some features of the list:
Need a headto point to the first node of the list.
Otherwise we wont know where the start of the
list is.
The currenthere is a pointer, not an index.
The next field in the last node points to nothing.
We will place the memory address NULL whichis guaranteed to be inaccessible.
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 Computer Notes - Data Structures - 2
24/24
Linked List
Actual picture in memory:1051
1052
1055
1059
10601061
1062
1063
1064
1056
1057
1058
1053
1054 2
6
8
7
1
1051
1063
1057
1060
0
head 1054
1063current
2 6 8 7 1
head
current
1065
http://ecomputernotes com
http://ecomputernotes.com/http://ecomputernotes.com/