Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Dynamic Array

• You would like to use an array data structure but you do not know the
size of array at compile time.

• You find out when the program executes that you need an integer
array of size n=20.

• Allocate an array using the new operator:


int* y=new int[20] or int *y=new iny[n]
y[0]=10
y[1]=15
Dynamic Arrays
• Here ‘y’ is an lvalue; it is a pointer that holds the address of 20
consecutive cells in memory.

• It can be assigned a value. The new operator returns an address that


is stored in y.

• Now we can also write as:


y = &x[0] //y gets the address of the first cell of x array
y =x; //y gets the address of the first cell of x array
Dynamic Array
• We must free the memory we got using new operator once we are
done with this y array.

Delete[] y

• We can not do this to x Array because we did not use new to create it.
Static vs. Dynamic Memory Allocation
Static Memory Allocation Dynamic Memory Allocation
• At compile time • At run time
• Amount of memory needed is known in • No need to know in advance
advance • Only required amount of memory allocated
• Finite amount of memory allocated • No memory wastage
• Memory wastage incase allocated space not
utilized fully • It will increase run time
• As memory allotted before run time, saves
run time • Slow allocation
• Fast allocation • Memory can be freed once used
• Allocated space can never be freed • Examples: Dynamic array, pointers
• Examples : Arrays, structure etc
The List Data Structure
• It is among the most generic data structures

• Real Life:

a. Shopping list
b. Groceries list
c. List of people to invite dinner
d. List of presents to get
List
• A list is collection of items that are all of the same type.

• The items, or elements of the list, are stored in some particular order.

• It is possible to insert new elements into various positions in the list


and remove any element in the list.

• List has ordered collection.


List Operations
• createList(): create new list (presumably empty)
• copy(): copy the data items from one to another list
• clear(): clear a list (remove all elements)
• Insert(x,?): insert element x at a particular position in the list
• remove(?): remove an element from particular position in the list
• update(x,?): replace the element at a given position with x
• find(x): determine if the element x is in the list
• length(): return the length of the list
List Operations
• We need to decide what is meant by “particular position”; we have
used “?” for this.

• There are two possibilities

1. Use the actual index of the element: insert after element 3, get element
number 6. This approach is taken by arrays.
2. Use a “current” marker or pointer to refer to a particular position in the list.
List Operations
• If we use the “current” marker, the following four methods would be
useful:

• start(): moves the “current” pointer to the very first element.

• tail(): moves the “current” pointer to the very last element.

• next(): moves the current position forward one element.

• back(): moves the current position backward one element.


Implementing list using array
• The list of integers (2,6,8,7,1) could be represented as by using an
array of arbitrary size:
2 6 8 7 1

0 1 2 3 4

current size

3 5
List implementation
• add(9): current position is 3. The new list would now be (2,6,8,9,7,1)
• Thus we need to shift every thing to the one position right of the
current position, so that we can make space for new element 9 at
current position.
2 6 8 7 1

0 1 2 3 4 5

current size

3 6
List implementation
• next():

2 6 8 9 7 1

0 1 2 3 4 5

current size
4 6
List implementation
• There are special cases for positioning the current pointer:

a. past the last array cell


b. before the first cell

• We need to keep this in mind while writing the code


List implementation
• remove(): removes the element at the current index

2 6 8 9 1 current size
0 1 2 3 4 5 4 6

2 6 8 9 1
current size
4 5
0 1 2 3 4
List implementation
• find(x): traverse the array until x is located
int find(int x)
{
Int j;
for(j=1;j<size;j++)
{
if(A[j]==x)
break;
}
if(j<size)
{
current=j;
return 1;
}
return 0;
}
List implementation
• get() return A[current]
• update(x) A[current]=x
• length() return size
• back() curren--
• start() current=1
• end() current=size
Analysis of Array Lists
• add

We have to move every element to the right of current to make space for the
new element.

Worst case: when we have to insert at the start

Average case: Half the elements are moved

best case: insert at the end


Analysis of Array Lists
• remove
Worst case: remove from the beginning
Average case: Half the elements are moved
Best case: at the end
• find
Worst case: may have to search the entire array
Average case: search at most half the array
Best case: element found at the first location to be searched
• All the other operations are one-step

You might also like