Data structures
The way you organise information decides how easy (and fast) your program can be. Meet the toolbox every programmer uses.
Which structure fits?
Real situations, matched with the right tool.
Browser history โ StackLast page in, first page out when you press Back.
Printer queue โ QueueFirst document in is the first one printed.
Contacts โ MapLook up a phone number instantly by name.
Organisation chart โ TreeEach manager has a team below them.
Road network โ GraphCities are nodes; roads are edges between them.
Task priorities โ Priority queueThe most urgent task always comes out first.
๐ Stack in action
Undo, like in your favourite editor
A stack is a pile of plates: you add on top and take from the top. Thatโs exactly how โUndoโ remembers what you did.
undo-stack.pseudo
1BEGIN2 SET HISTORY = []3 ADD "type Hello" TO HISTORY4 ADD "make bold" TO HISTORY5 ADD "change colour" TO HISTORY6 SET LAST = POP(HISTORY)7 DISPLAY "Undo:", LAST8 DISPLAY "Still in history:", HISTORY9END