Sunday, 15 March 2015

How to Draw Your Code

One of the main themes from week 8 was drawing pictures... But only to understand linked lists, of course! CSC 148 is branching (and I'm not only talking about Trees here) away from the easy data structures, and getting into some of the more complex ones. Linked lists can give you a bit of a headache, but once you understand how everything is connected, they are not too bad.

Linked lists are, as the name would suggest, a new form of list. They represent a sequence of a collection of nodes, all connected to one another. It's a bit abstract to explain, but they make a lot of sense when you draw out the connections.

A lot of our lab excercises this week covered inserting new nodes into previously created lists. I'm stealing a picture from Wikipedia to help explain myself, as you can see below.


All you have to do is create a new node, and relabel the values of the node before the place you want to put the new node and the one that' coming up next. Each node keeps track of what the next node is, but when you write your code you have to create a new variable to keep track of what the old node was, in order to properly organize your data structure. It makes a lot of sense, and helps keep your data structure neat and organized.

Linked lists are good for memory, and they keep the program run time down. Stacks and queues are especially useful for linked lists. However, linked lists do have their downfalls -- they don't have a lot of built in functions for indexing, meaning you might have to write your own helper functions. Traversing a linked list is not as easy as traversing a tree. Most crucially, it is really easy to get confused by linked lists -- if you don't keep careful track of what's doing what, you might find your code completely misses the point, as do the nodes in your linked lists! All in all, linked lists are one of the many data structures available in Python, though maybe not always the most convenient.

No comments:

Post a Comment