Data Structure
Data Structure
05
C[0] 24
C[1] 26
C[2] 28
C[3] ...
C[4] ...
C[5] ...
C[6] ...
C[7] ...
C[8] ...
C[9] ...
1-D array representation in memory
Consider array name is Auto established in 1960:
loc (Auto[k]) = base(Auto) + w * ( k - lb)
base (Auto) = 200 //base address of array
w = words per memory cell = 02 //(int having 2 bytes)
K = kth element
Lb = lower bound
60 61 62 63 64 65 66 67 68
10 20 30 11 22 33 55 66 77
1,1 1,2 1,3 2,1 2,2 2,3 3,1 3,2 3,3
60 61 62 63 64 65 66 67 68
10 11 55 20 22 66 30 33 77
1,1 2,1 3,1 1,2 2,2 3,2 1,3 2,3 3,3
Real life:
a. shopping list,
b. groceries list,
c. list of people to invite to dinner
d. List of presents to get
Array - Based Lists
A list is collection of items that are all of the same type
(grocery items, integers, names)
current size
A 2 6 8 7 1
3 5
1 2 3 4 5
List Implementation
current size
step 1: A 2 6 8 7 1
3 5
1 2 3 4 5 6
next():
current size
A 2 6 8 9 7 1
4 6
1 2 3 4 5 6 5
Lists Implementation
current size
Step 2: A 2 6 8 9 1
5 5
1 2 3 4 5
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.
Lists Implementation
Other operations:
find
Worst-case: may have to search the entire array
Average-case: search at most half the array.
Algorithm steps
•Visit the first element of array and compare its value with the
required value.
•If matches, search is completed.
•If does’nt match, move to the next element of array and repeat the
same process.
Note: You are required to write Java code for above algorithm
Exercise:
Write a Java code in which one can insert an element
at the location specified by the user.
10 20 30 40 50
Enter location. 2
The element to be inserted is: 99
Output: 10 20 99 30 40 50