This week we learned about Stacks and Queues and Abstract Data Types. At first, Stacks seemed rather useless as they operate in the Last-In-First-Out (LIFO) order. Then Danny Heap brought up in class that stacks are used for coding programs itself, as they are used to add and resolve function calls. This was probably quite obvious but I didn't realize this. In hindsight, the python visualizer shown in CSC108 was definitely showing Stacks.
The Queue is similar but instead it operates in a First-In-First-Out (FIFO) order and is the one that is very prevalent in the real world. With Tim Hortens lines (or any retail line) being a prime example. Both of these are called Abstract Data Types (ADT) which are when we represent real world phenomena in the computer world.
Some other common ADTs include Lists, Sacks, and Dictionaries. Lists and Dictionaries are inbuilt classes. Sack is not inbuilt and represents a sack that you throw things into such that you take things out randomly.
The implementation of most of the new ADTs can be done with various inbuilt classes such as List, Tuple, and Dictionary. In class, Stack was implemented as a List for simplicity and for the already inbuilt tools. Sack was implemented as a dictionary to take advantage of the non-ordered nature. Personally, I prefer using List due to familiarity and the useful tools .append(), and .pop().
To sum up, the list of ADTs this week are:
- List: Represents a set of objects in which any object in the List can be accessed and new objects can be added anywhere. Real world example: A list!
- Dictionary: Represents a set of objects in which a key is needed to access an object.
Real world example: A dictionary.
- Stack: Represents a set of objects in which only the most recent object can be accessed.
Real world example: A stack of paperwork.
- Queue: Represents a set of objects in which only the least recent object can be accessed.
Real world example: A checkout line.
- Sack: Represents a set of objects in which produces a random object in the set when removing. Real world example: A lottery draw.
I'll probably add LinkedList next week, once we're done exploring it.
No comments:
Post a Comment