Saturday, July 24, 2010

Linked list knitting

Bit of an odd project to talk about today (also, if you know me on facebook you may have seen this already.
The idea is to implement a system of knitting using linked lists. There have been quite a few recent experiments into knitting with unusual media, whether it's carrier bags, wire, human hair, or even fibre-optic cable -- you can knit with just about anything which can be made into threads. You could think of this as a really extreme version of this -- a linked list is essentially a thread of data, so why not knit with this? (Or you could see it as just a terrible pun being taken too far, which is probably more accurate.)
So here I've designed a system of knitting using these lists, and implemented it in C.

So, linked list is a data structure in which each data point contains the address for the next piece of data, so to read it all you start at one point and move along the chain following the addresses. I'll actually be using doubly-linked lists -- for these each data point contains two references, which means the chain can self-intersect.

To come up with a system of knitting using these lists, we need to be able to form loops, and to pass loops through each other. This can be done as follows:

This is really all you need to start knitting -- forming a loop corresponds to casting on, we know how to knit into a stitch, and we can cast off by simply threading the 'yarn' through the stitch without forming a loop.
It would also be possible to give analogues for many other knitting concepts -- you can purl by reversing the orientation when you knit, you could Kfb by passing two loops through the stitch, Make by simply forming a new loop in between stitches, K2tog by passing the new loop through two previous stitches, and so on.

So I thought it would be fun to actually implement this, and you can see the results here:
The first file contains the details of the implementation, the second is a set of instructions for knitting a small stocking stitch swatch, and the the third gives a 2*1 ribbed cuff knit in the round -- hopefully you will be able to see how these correspond to a real knitting pattern. (The final file is the header) If you're using linux you can compile these with the commands:
gcc tensionsq.c llknitting.c
gcc ribbedcuff.c llknitting.c
I'm not too sure what the equivalent command would be in Windows.
The output from each program gives the positions of the start of each stitch in the array, which isn't terribly illuminating, but hopefully convinces you that it's actually doing something. (I decided not to worry too much about outputting data since it's really the program which is the interesting part.)

So there you go -- that's how to knit with linked lists. If, for some reason, you wanted to do that. So, linked list crochet?

Happy knitting, and/or data structuring!

1 comment:

peahen said...

I don't think it's a pun gone too far - it's beautiful! Bravo.