Professional Documents
Culture Documents
CS-102
Ashok K Turuk
1
Data Structure
vs
Storage Structure
• Data Structure
– Linear
– Non-Linear
3
Representation in Memory
4
Operation on Linear Structure
5
Array
6
Linear Arrays
7
Linear Arrays
9
Representation of Linear Array in
Memory
1000
1001
1002
1003
1004
1005
:
Computer Memory
10
Representation of Linear Array in
Memory
• Let LA be a linear array in the memory of the
computer
• LOC(LA[K]) = address of the element LA[K] of
the array LA
• The element of LA are stored in the successive
memory cells
• Computer does not need to keep track of the
address of every element of LA, but need to track
only the address of the first element of the array
denoted by Base(LA) called the base address of
LA
11
Representation of Linear Array in
Memory
• LOC(LA[K]) = Base(LA) + w(K – lower
bound) where w is the number of words
per memory cell of the array LA [w is
aka size of the data type]
12
Example 1
200 LA[1]
Find the address for LA[6] LA[2]
Each element of the array 201
occupy 1 byte 202 LA[3]
203 LA[4]
LA[5]
204
LA[6]
205
LA[7]
206
LA[8]
207
LOC(LA[K]) = Base(LA) + w(K – lower bound) :
LOC(LA[6]) = 200 + 1(6 – 1) = 205
13
Example 2
200
Find the address for LA[16] LA[1]
Each element of the array 201
occupy 2 byte 202 LA[2]
203
204 LA[3]
205
206 LA[4]
207
LOC(LA[K]) = Base(LA) + w(K – lower bound) :
LOC(LA[16]) = 200 + 2(16 – 1) = 230
14
Representation of Linear Array in
Memory
• Given any value of K, time to calculate
LOC(LA[K]) is same
• Given any subscript K one can access and
locate the content of LA[K] without
scanning any other element of LA
• A collection A of data element is said to
be index if any element of A called Ak can
be located and processed in time that is
independent of K
15
Traversing Linear Arrays
Linear Array
•••
16
Traversing Linear Arrays
Linear Array
•••
17
Traversing Linear Arrays
Linear Array
•••
18
Traversing Linear Arrays
Linear Array
•••
19
Traversing Linear Arrays
Linear Array
•••
20
Traversing Linear Arrays
Linear Array
•••
21
Traversing Linear Arrays
Linear Array
•••
1. Repeat for K = LB to UB
Apply PROCESS to LA[K]
[End of Loop]
2. Exit 22
Inserting and Deleting
• Insertion: Adding an element
– Beginning
– Middle
– End
23
Insertion
1 Brown 1 Brown
2 Davis 2 Davis
3 Johnson 3 Johnson
4 Smith 4 Smith
5 Wagner 5 Wagner
6 6 Ford
7 7
8 8
24
Insertion
1 Brown 1 Brown
2 Davis 2 Davis
3 Ford 3 Ford
4 Johnson 4 Johnson
5 Smith 5 Smith
6 Taylor 6 Taylor
7 Wagner 7
8 8
26
Deletion
27
Deletion
1 Brown
2 Ford
3 Johnson
4 Smith
5 Taylor
6 Wagner
7
8
29
Deletion Algorithm
• DELETE (LA, N , K , ITEM) [LA is a
linear array with N elements and K is a positive
integers such that K ≤ N. This algorithm deletes Kth
element from LA ]
1. Set ITEM := LA[K]
2. Repeat for J = K to N -1:
[Move the J + 1st element upward] Set LA[J] :=
LA[J + 1]
3. [Reset the number N of elements] Set N := N - 1;
4. Exit
30
Multidimensional Array
• One-Dimensional Array
• Two-Dimensional Array
• Three-Dimensional Array
• Some programming Language allows as
many as 7 dimension
31
Two-Dimensional Array
• A Two-Dimensional m x n array A is a
collection of m . n data elements such
that each element is specified by a pair
of integer (such as J, K) called subscript
with property that
1 ≤ J ≤ m and 1 ≤ K ≤ n
A Subscript A Subscript
(1,1) (1,1)
(2,1) Column 1 (1,2) Row 1
(3,1) (1,3)
(1,2) (1,4)
Column 2 (2,1)
(2,2)
(2,2)
(3,2) Row 2
(2,3)
(1,3) Column 3
(2,4)
(2,3)
(3,1)
(3,3) (3,2) Row 3
Column 4
(1,4) (3,3)
(2,4) (3,4)
(3,4)
Column-Major Order Row-Major Order 37
2D Array
• LOC(A[J,K]) of A[J,K]
Column-Major Order
LOC(A[J,K]) = Base(A) + w[m(K-1) + (J-1)]
Row-Major Order
LOC(A[J,K]) = Base(A) + w[n(J-1) + (K-1)]
38
2D Array Example
• An n-dimensional m1 x m2 x …. X mn array B
is a collection of m1.m2…mn data elements in
which each element is specified by a list of
n integers – such as K1, K2, …., Kn – called
subscript with the property that
1≤K1≤m1, 1≤K2≤m2, …. 1≤Kn≤mn
The Element B with subscript K1, K2, …,Kn will
be denoted by
BK1,K2, …,Kn or B[K1,K2,….,Kn]
40
Multidimensional Array
42
Example
43
Example Contd ..
45
Group 1 Group 2 Group 3 Group 4
Evan Conrad Davis Baker Two Dimensional
Harris Felt Segal Cooper
4x9 or 9x4 array
Lewis Glass Ford
Shaw Hill Gray
Penn Jones
Silver Reed
Troy
Wagner
King
46
1 Evan
2 Harris
3 Lewis Group 1
4 Shaw
5 Conrad
: : Group 2
13 Wagner
14 Davis Group 3
15 Segal
16 Baker Group 4
: :
21 Reed
47
1 Evan
: : Group 1
4 Shaw
5 $$$
Group are not index
6 Conrad Group 2 in this
: : representation
14 Wagner
15 $$$
Group 3
16 Davis
17 Segal
18 $$$
19 Baker Group 4
: : 48
1 Evan
2 Harris Group 1
Group
3 Lewis
1 1
4 Shaw
2 5
3 14 5 Conrad
Group 2
4 16 : :
5 22
13 Wagner
14 Davis Group 3
15 Segal
16 Baker Group 4
: :
21 Reed
49