Over the past 2 weeks we have been looking at a couple of abstract data types (ADTs) designed to store other objects. While I have used stacks, queues, and linked lists in the past, actually implementing them was definitely a learning experience.
Definitions: Sacks, Stacks and Queues
A sack is an unordered collection of objects. Objects can go in and a random object can be pulled out. They are similar to sets, but they can have multiple of the same object stored in them.
Stacks and queues are ordered collections of objects. Both are can remove and object from the end of the collections, but queues can only add objects to the beginning, and stacks can only add objects to the end. This means that queues are first-in-first-out, whereas stacks are first-in-last-out.
The closest python comes is the list class for stacks, and the deque class for queues, but neither of these are exactly stacks or queues. The list class does have an append (push) and pop function, which are all you really need for a stack, but it also has all sorts of other methods and properties which are not needed. Unlike private variables and functions that have a standard to protect them, there doesn't appear to be a standard that protects a stack implemented through a list from being misused.
The case is somewhat similar with deques and queues. "Deque" stand for double-ended queue: a queue that can push/pop an element both to/from the "left" and "right" sides of the queue. In a practical situation it would usually be reasonable to use this instead of creating a new queue class.
In class we inherited from the container class to implement these ADTs, and used them to solve simple problems like parenthesis counting. Surprisingly, this wasn't a purely theoretical exercise to teach us about the structure of stacks and queues, as python doesn't actually have these as a module/class.
Linked Lists
Linked lists are more different than lists than the ADTs described above, and on a lower level. In lists each object is stored after the previous element, which makes accessing a particular index quite fast, but makes inserting an object quite slow, as all the objects behind it have to be shifted as well. To counter this a linked list can be used. A linked list is made up of nodes, each which has a pointer to a value stored somewhere in the memory allocated to the program, and a pointer to the next node. This way, getting to a specific index in the linked list takes time as each node before that node is traversed, but inserting an object is as simple as creating node that points to that object and the next node, and changing the previous node's pointer to point at the new node.
Obviously, linked lists are better than lists if the list is quite long/has large objects and a lot of inserting/removing is done. Linked lists also have issues with memory efficiency, as they by nature require nodes, which are more memory intensive than lists. In most situations I suspect that using lists or other ADTs would be better, but having experience implementing a linked list is better than not having experience implementing one.
As an aside, apparently python automatically cleans up objects that are impossible to access anymore. I guess I already knew this considering that variables are just pointers to an object, and variables change. I was still surprised however by node removal in linked lists simply requiring a change of pointers, with no code to clean up the node itself.
As an aside, apparently python automatically cleans up objects that are impossible to access anymore. I guess I already knew this considering that variables are just pointers to an object, and variables change. I was still surprised however by node removal in linked lists simply requiring a change of pointers, with no code to clean up the node itself.
Midterm
Last week we had our midterm for CSC148. Before the test, I really wasn't sure what was going to be on it. We haven't covered that much so far in terms of algorithms, and the previous midterms had different material on them so they weren't particularly useful. So I memorized the formatting we needed, syntax of properties, inheritance, etc., but still wasn't feeling too certain entering the exam room: had I missed some critical example or lecture, what on Earth was going to be on the midterm? As it turned out, none of the questions were particularly hard: anyone who had attended lecture and tutorials as well as made sure they understood them should have been able to answer them, given enough time. Unfortunately, the 50 minutes given to write the test were - for the majority of people I have talked to - not enough time.
Given the great importance of marks in this course for anyone who wants to get into the computer science POSt, this test assumedly reflects what is needed. Therefore it's interesting and useful to see that speed, exact knowledge, and decisiveness are the traits that ensure success in later years.
This week is reading week, so stand by for a post about an interesting cs problem. Have a nice reading week, whether you're relaxing or trying to stay afloat.
No comments:
Post a Comment