CSCE 3110 Data Structures & Algorithm Analysis: Rada Mihalcea
CSCE 3110 Data Structures & Algorithm Analysis: Rada Mihalcea
Arrays
Array: a set of pairs (index and value)
data structure
For each index, there is a value associated with
that index.
representation (possible)
implemented by using consecutive memory.
Arrays in C
int list[5], *plist[5];
list[5]: five integers
list[0], list[1], list[2], list[3], list[4]
*plist[5]: five pointers to integers
plist[0], plist[1], plist[2], plist[3],
plist[4]
implementation of 1-D array
list[0]
base address =
list[1]
+ sizeof(int)
list[2]
+ 2*sizeof(int)
list[3]
+ 3*sizeof(int)
Arrays in C (contd)
Compare int *list1 and int list2[5] in C.
Same: list1 and list2 are pointers.
Difference: list2 reserves five locations.
Notations:
list2 - a pointer to list2[0]
(list2 + i) - a pointer to list2[i] (&list2[i])
*(list2 + i) - list2[i]
Example
Example:
Address
Contents
1228
1230
1232
1234
1236
printf(\n);
}
Polynomial ADT
Objects:
Methods:
for all poly, poly1, poly2 Polynomial, coef Coefficients, expon
Exponents
Polynomial Zero( )
::= return the polynomial p(x) = 0
Boolean IsZero(poly)
::= if (poly) return FALSE
else return TRUE
Coefficient Coef(poly, expon)
::= if (expon poly) return its
coefficient else return Zero
Exponent Lead_Exp(poly)
::= return the largest exponent in
poly
Polynomial Attach(poly,coef, expon) ::= if (expon poly) return error
else return the polynomial poly
with the term <coef, expon>
Running time?
}
advantage: easy implementation
disadvantage: waste space when sparse
coef
exp
finishb avail
10
1000
Running time?
Addition(int starta, int enda, int startb, int endb, int startc, int endc)
{
}
advantage: less space
disadvantage: longer code
Sparse Matrices
col1 col2 col3 col4 col5 col6
15 0
0 11
row0
row1
22 0 15
0 0
0 6 0
0 0
row4
0
91
0 0
row5
0 28
0 0
row2
row3
5*3
15/15
8/36
sparse matrix
data structure?
0
0
0
0
0
6*6
transpose
[5]
b[0]
[1]
[2]
[3]
[4]
2 5
[6]
[7]
[8]