You are on page 1of 20

Data Representation Methods

array --- Chapter 5


linked --- Chapter 6
Linear List Array Representation
use a one-dimensional array element[]

a b c d e

0 1 2 3 4 5 6

L = (a, b, c, d, e)
Store element i of list in element[i].
Right To Left Mapping

e d c b a
Mapping That Skips Every Other
Position

a b c d e
Wrap Around Mapping

d e a b c
Representation Used In Text

a b c d e

0 1 2 3 4 5 6 listSize = 5

put element i of list in element[i]

use a variable listSize to record current


number of elements
Insert/Erase An Element
listSize = 5
a b c d e

insert(1,g)

listSize = 6
a g b c d e
Data Type Of Array element[]
Data type of list elements is unknown.

Define element[] to be of template type T.


Use template type T
Length of Array element[]
Don’t know how many elements will be in list.

Must pick an initial length and dynamically increase


as needed.

Use variable arrayLength to store current length of


array element[].
Increasing Array Length

Length of array element[] is 6.


a b c d e f

First create a new and larger array

T* newArray = new T[15];


Increasing Array Length
Now copy elements from old array to new one.

a b c d e f

a b c d e f

#include <algorithm>

copy(element, element + 6, newArray);


Increasing Array Length

Finally, delete old array and rename new array.


delete [] element;
element = newArray;
arrayLength = 15;
element[0]

a b c d e f
template<class T>
void changeLength1D(T*& a, int oldLength,
int newLength)
{
1 if (newLength < 0)
throw illegalParameterValue();

2 T* temp = new T[newLength];


// new array
3 int number = min(oldLength, newLength);
// number to copy
4 copy(a, a + number, temp);
5 delete [] a;
// deallocate old memory
6 a = temp;
}
How Big Should The New Array Be?

At least 1 more than current array length.

Cost of increasing array length when array is


full is Q(old length).

Cost of n insert operations done on an


initially empty linear list increases by Q(n2).
Space Complexity

element[6]
a b c d e f

newArray = new char[7];

space needed = 2 * newLength – 1


= 2 * maxListSize – 1
Array Doubling

Double the array length.


a b c d e f

newArray = new char[12];


a b c d e f

Time for n inserts goes up by Q(n).


Space needed = 1.5*newLength. (e.g. 1.5 * 12)

Space needed <= 3*maxListSize – 3


How Big Should The New Array Be?

Resizing by any constant factor


new length = c * old length
increases the cost of n inserts by Q(n).

Resizing by an additive constant increases


the cost of n add operations by Q(n2).
How Big Should The New Array Be?

Resizing by any constant factor


new length = c * old length
requires at most (1+c) * (maxListSize -1) space.

Resizing by an additive constant c requires


at most (maxListSize – 1) + (maxListSize – 1 + c)
= 2 * (maxListSize – 1) + c space.
What Does C++ Do?

STL class vector … c = 1.5

arrayList of text … c = 2
To-do
Practice:
– https://www.hackerrank.com/
challenges/cpp-input-and-
output
More on insertion sort:
– https://en.wikipedia.org/wiki/I
nsertion_sort

You might also like