Arrays LinkedList CheatSheet
Arrays LinkedList CheatSheet
Arrays LinkedList CheatSheet
Before we understand what, each data structure is, we should understand how they are used
and what operations they support. (what are we really trying to solve here)
Specifications Representation
What the operations do/support and mean Algorithms (how to support those operations)
You should be able to differentiate/separate what you want to do, and how you want to do it,
before selecting which tool (data structure) to you want to solve it with.
2. Data can be stored in either a fixed size data structure, or a dynamic size (not fixed).
Where, a data structure ‘array’ has been initialized to support the items, with a fixed size
of ‘n’
Ex:
3 6 1 13 7
3. len() : return n
Ans: 5
4. iter_seq(): output x0, x1, x2 … xn-1 in a sequential order
3 6 1 13 7
ex: array[3] = 13
6. set_at(i,x): set x at i
ex: array[1] = 2
3 2 1 13 7
Dynamic Sequences
So that means, Shifting xi -> xi+1 -> xi+2….. -> xn-1 ->
xn
Therefore, the get and set of the first/last indices are special cases.
Sometimes by analyzing the special case for these, it may be more efficient than the
plain insert.
Linked Lists
items next
2 class variables
➢ These are assembled into a structure where
Item -> value
Next -> provides us an order
& we keep track of head, length
➢ Linked Lists are linear data structures, which can be traversed one by one.
➢ Linked Lists have the advantages over stacks and queues where elements can be
added
6 15 8 3
Front
Ex:
1) Create node
a)
b)
➢ that means, the ith index of the array, can be found in the RAM
➢ In Linked Lists, pointers can be stored in a single word, which means we can de-
reference them (we can see what is on the other side of the pointer) in constant – time in
our word – RAM model.
➢ In reality, each of these nodes is stored somewhere in the array of the computer (RAM),
in an arbitrary order.
➢ Using this, we can allocate an array of size n (in linear time), where each data structure
in 2 words long.
Conclusion:
• Arrays are great at random access
• Linked Lists = bad at random access, but great if you are working on the ends
Note: It may be asked in the interview, how to access the linked list’s last element in constant
time.
Ans: Start a point at the end of the list – tail pointer. This is also known as D.S.
augmentation.