Arrays
Arrays
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)
Example
#include <iostream>
void print1(int *ptr, int rows)
{
int i;
cout << "Address Contents" << endl;
for (i=0; i < rows; i++)
cout << ptr+i << " " << *(ptr+i) << endl;
}
void main(){
int one[] = {0, 1, 2, 3, 4}; //Goal: print out address and value
print1(one, 5);
}
Address Contents
0xbffffbb4 0
0xbffffbb8 1
0xbffffbbc 2
0xbffffbc0 3
0xbffffbc4 4
Questions
What is the complexity of retrieve in an
array?
What is the complexity of store in an array?
What about insertion and deletion for ordered
elements in an arrary?
Polynomial ADT
Objects:
Methods:
for all poly, poly1, poly2 Polynomial, coef Coefficients, expon
Exponents
Polynomial Zero( )
::= return the polynomial p(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?
finishb avail
coef
exp
0
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]
(0, 0, 15)
(3, 0, 22)
(5, 0, -15)
(1, 1, 11)
Linked Lists
Avoid the drawbacks of fixed size arrays with
Growable arrays
Linked lists
Growable arrays
Avoid the problem of fixed-size arrays
Increase the size of the array when needed
(I.e. when capacity is exceeded)
Two strategies:
tight strategy (add a constant): f(N) = N + c
growth strategy (double up): f(N) = 2N
Tight Strategy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Tight Strategy
void insertLast(int rear, element o) {
if ( size == rear) {
capacity += k;
element* B = new element[capacity];
for(int i=0; i<size; i++) {
B[i] = A[i];
}
A = B;
}
A[rear] = o;
rear++;
size++; }
Growth Strategy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Linked Lists
Avoid the drawbacks of fixed size arrays with
Growable arrays
Linked lists
Using Dynamically
Allocated Memory (review)
int *pi = new int;
float *pf=new float;
*pi =1024;
*pf =3.14;
request memory
return memory
Linked Lists
bat
cat
sat
vat
NULL
NODE
Data item
Link to the next node
HEAD
A
CURRENT
TAIL
Insertion
A
X
A
B
3
X
A
Deletion
A
B
C
Questions
Can we do insertion BEFORE a node in singly
linked list?
Can we do deletion BEFORE a node in singly
linked list?
List ADT
The List ADT models a
sequence of positions storing
arbitrary objects
It establishes a before/after
relation between positions
Generic methods:
size(), isEmpty()
Query methods:
isFirst(p), isLast(p)
Accessor methods:
first(), last()
before(p), after(p)
Update methods:
replaceElement(p, o),
swapElements(p, q)
insertBefore(p, o),
insertAfter(p, o),
insertFirst(o),
insertLast(o)
remove(p)
header
prev
next
elem
node
nodes/positions trailer
elements
Insertion
We visualize operation insertAfter(p, X), which returns position q
p
B
p
B
p
B
q
X
q
X
Deletion
We visualize remove(p), where p = last()
p
D
p
D
A
O(?)
O(?)
O(?)
O(?)
O(?)
O(?)