Linked Lists

A linked list is a data structure that is similar to an array in that both can be used for data that is a sequential list of elements.
A linked list is a collection of elements, or nodes. Each node contains a pointer to the next node and thus the nodes need not be adjacent in memory.
image.png

Adding a Node

Empty List
  1. ; initiate
  2. LD R2, NEW_ITEM
  3. LD R1, LISTHEAD
  4. ; insert
  5. STR R2, R0, #0
  6. STR R1, R2, #0

Not Empty List
  1. ; R2 is the node to insert
  2. ; R1 is the current pointer, R0 is the previous pointer
  3. ; initiate
  4. LD R2, NEW_ITEM
  5. LD R1, LISTHEAD
  6. ; compare, to sort by NAMEs
  7. LOOP ...
  8. BRp INSERT
  9. BRn MOVE_PTR
  10. BRz LOOP
  11. ; move pointer
  12. MOVE_PTR ADD R0, R1, #0
  13. LDR R1, R1, #0
  14. ; insert
  15. INSERT STR R2, R0, #0
  16. STR R1, R2, #0

image.pngimage.png

Delete a Node