Professional Documents
Culture Documents
Linked Lists
Linked Lists
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
So, 13 x 79 = 1027
Arrays:
Linked Lists
Lists
AddInOrder() = O(n)
AddToFront() = O(1)
Stack
Pop() = O(1)
Peek() = O(1)
Push() = O(1)
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