You are on page 1of 5

To calculate how much data an array stores against how much data a

linked list store

Example;

How much more memory (in bytes) would a Singly Linked List require
compared to an Array if we wished to store a collection of 79 customer IDs
that are each 13 bytes in size. Assume that pointers occupy 4 bytes, and that
we need a separate head pointer to gain access to the List)

1. Each ID is 13 Bytes
2. Each pointer is 4 bytes
3. We need one additional head pointer

So for each node, we have the data (13 bytes) and the pointer (4 bytes), so
this is 17 bytes per node.

Total memory for linked list= (Data size per node + Pointer size per node) ×
Number of nodes + Head pointer size

So, 17 x 79 + 4 = 1347

Now, we need to work out the array. This doesn’t have any pointers, as the
data is stored adjacent in memory.

1. Each ID Is 13 Bytes
2. No pointers

Total memory for array=Data size per element × Number of elements

So, 13 x 79 = 1027

1347 – 1027 = 320


Pros and Cons of Arrays and Linked lists

Arrays:

- Fixed in size, expensive to resize


- Random access, fast efficient indexing
- Items are adjacent in memory
- Insert and delete operations are inefficient, and means that you must
shift all elements
- Memory is reserved for maximum number of elements, so wasted if
fewer items are held

Linked Lists

- Dynamic memory allocation, no memory is wasted


- No random access, can be slow to access data
- Slow access to data as items are not contiguous in memory
- Insert and delete operations are quick, no shifting needed
- Dynamic size, can shrink and grow during run time
Time complexities

Lists

AddInOrder() = O(n)

AddToBack() = O(n) (O(1) if there is a tail pointer)

AddToFront() = O(1)

Stack

Pop() = O(1)

Peek() = O(1)

Push() = O(1)

Queues WITH HEAD AND TAIL POINTER

Enqueue() = O(1)

Peek() = O(1)

Dequeue() = O(1)
Best, Worst and Average case scenarios

NOTE: For a stack with a head BUT no tail pointer, each case is 1 hop.

NOTE: For a queue with both head and tail pointers, each case is 1 hop.

QUEUE = FIFO

STACK = LIFO
Cumulative hops for linked list

Cumulatively (i.e., in total), how much work, measured in units of hops, would
be required to insert 49 new nodes into an initially empty Singly Linked List
using addToFront()? [note: we will count hopping off the head pointer as 1
hop; hopping through any existing node also counts as 1 hop].

49

Cumulatively (i.e., in total), how much work, measured in units of hops, would
be required to insert 63 new nodes into an initially empty Singly Linked List
using addToBack()? [note: we will count hopping off the head pointer as 1
hop; hopping through any existing node also counts as 1 hop].

2,016

Cumulatively (i.e., in total), how much work, measured in units of hops, would
be required to insert 71 new nodes into an initially empty Singly Linked List
using addInOrder()? Enter your answer to 1 d.p. [note: we will count hopping
off the head pointer as 1 hop; hopping through any existing node also counts
as 1 hop].

1313.5

You might also like