Linked List

Table of Contents

For this first part of the interpreter project, you’ll create a linked list that you’ll then use throughout the rest of the project.

This is an interpreter project team assignment, which means that if you are working on a team with someone else, you and your partner should do your best to even the workload between you. Strict pair programming is not required, but you should make sure that both team members are contributing to the project. I do recommend that you try to do pair programming if it is convenient to do so.

1 Get started

1.1 Use GitHub classroom to create a repository for your assignment

The first thing you’ll need to do is to create a repository for your project using GitHub classroom. If you are working on a team, this repository will be shared, so only one of you should do this. To help reduce a round of communication, I’m going to designate that if you are working in a pair, the member of your team whose Carleton official email address comes second alphabetically should do this next step of setting up the GitHub repository. This is opposite from the previous pair assignment; I’m trying to give everyone an equal chance to participate. If you are working alone, then you should do this step.

If you are the designated person above, visit this GitHub classroom link (which I’ve placed in Moodle). Log into GitHub if your browser prompts you to after you get to GitHub. You should be taken to a screen that invites you to either “Join an existing team” or to “Create a new team”. You should create a new team. Please name the team with both of your GitHub usernames. For example, if users FSchiller and StevieP are working together, please name the team FSchiller-StevieP. The order doesn’t matter. This will make it easier for the graders to know who the repositories belong to. GitHub should then hopefully respond with a screen that says “You are ready to go!”

On this “You are ready to go!” screen, you should see a “Work in Repl.it” button right there. Click that button, and it will make a repl for you in Repl.it.

Next, visit the GitHub repository that you created. You can likely hit the back button in your browser from the repl in Repl.it that was just created, or you can open another tab and find your repository in GitHub. You need to do is make sure you enable GitHub Actions, so that the automated tests can run. To do this, click on the find the “Actions” tab. (Is that a tab? I guess so. For me, it’s the fourth option from the left, near the top of the screen, which starts with “Code” on the left side.) Once you’ve clicked “Actions”, then click the green button that says “Enable Actions on this repository.”

If you’ve gotten this far, you have successfully created the repository, and you the user who is logged in have access to it. Contact your partner if you have one and tell them that this step worked. (If you are working alone on the project, you can skip the next step.)

The other one of you, after the above is complete, should visit the same GitHub classroom link in the first sentence in the previous paragraph. You’ll see the screen asking you to “Join an existing team” or to “Create a new team.” You should be able to see the team that your partner created above; click the “Join” button. This should then take you to the “You are ready to go!” screen, and you can hopefully find a link to the repository. Click on it. You now both have access to the same GitHub repository.

2 Values and lists

One of the first questions to arise will be “What is this a linked list of? Integers? Strings? More lists?” The catch is that you’ll be storing lists created in Scheme, which can contain values of a whole variety of types. You might think: “Ah! I’ll have a Value class, with subclasses for IntValue, StringValue, etc.” And that would be totally awesome, if you were implementing this list using an OOP (object-oriented programming) language. Oops.

In C, one way to handle this type of typing issue is to use union types. (See our online C reference that I assigned or other online tutorials.) These types allow you to have a single datatype that sometimes includes an int, sometimes a char *, etc.) Every such value should also include a tag telling you what kind of data you’re storing.

typedef enum {INT_TYPE, DOUBLE_TYPE, STR_TYPE,...} valueType;

struct Value {
    valueType type;
    union {
        int i;
        double d;
        char *s;
        ...
    };
};

typedef struct Value Value;

The names INT_TYPE, DOUBLE_TYPE, etc., represent types of Values. The Value struct includes a field (type) containing one of these tags, as well as a union of various possible fields. For example, you could create a value containing the integer 5 as follows:

Value myInt;
myInt.type = INT_TYPE;
myInt.i = 5;

If you have a Value and want to determine its type, you might choose to use a switch statement:

switch (val.type) {
case INT_TYPE:
    // do something with val.i
    break;
case DOUBLE_TYPE:
    // do something with val.d
    break;
...
}

You will thus want to make some sort of linked list implementation where the nodes contain Values. There are many different ways that one can construct a linked list. The most common approach you have likely seen is one that consists of nodes, where each node is a struct containing two items: a pointer to a value, and a pointer to another node.

Don’t do it this way for the assignment. There’s a better way that will save you much pain later.

Because you will eventually be using your linked list to represent Scheme S-expressions, you will have a much easier time if your linked list actually resembles a Scheme linked list. Specifically, each node should be a “cons cell” with two pointers within, and it should not be strictly typed.

Here is an abbreviation of the technique that you will use:

struct Value {
    valueType type; // type will also have a CONS_TYPE as an option
    union {
        int i;
        double d;
        /* .... */
        struct ConsCell {
            struct Value *car;
            struct Value *cdr;
        } c;       
    };
};


typedef struct Value Value;

The “head” pointer for your linked list, whatever you choose to call it, should be of type Value*. It should be NULL_TYPE if the list is empty, or point to a Value. That Value should be one that holds a cons cell. The car of that cell should point to the first item in your linked list; the cdr should point to another Value. And so on. At the end of the linked list, the cdr of that Value should point to a NULL_TYPE Value.

And finally: if you insert tokens at the beginning of the list, which is the simplest way, your tokens will be represented in backwards order from what you typically want. One could handle this efficiently by writing code to add at the tail of the linked list instead of the head. Instead, we’ll make things simpler by writing an additional function to reverse a linked list. Is this less efficient? Yeah. This project is large enough; we won’t focus very much on efficiency, though you might think about tracking places to make it more efficient if you want to improve it at the end.

You can feel free to leverage any linked list code that we may or may not have written in class, though bear in mind that it might not fit this framework.

3 Some specifics

After you clone your repository, you should be able to see that you get the following starting files:

  • value.h: this defines the Value structure, described above
  • linkedlist.h: this defines all of the functions that you will need to write
  • main.c: this is a tester function that makes some nodes, puts them into a linked list, displays them, and cleans up memory afterwards
  • Makefile: contains instructions for the command make, which will compile and test your code
  • test-e and test-m: usual
  • test_utilities.py: helper utilities used by test-e and test-m

The missing file here is linkedlist.c, which you’ll need to create yourself in order to implement everything in linkedlist.h. To compile your code, issue the command make at the command prompt. This will follow the instructions in the Makefile for building your project in order to produce an executable called linkedlist. At first, it won’t build at all because your linkedlist.c file isn’t there. Create the file and for now, create every function that you need with no code inside it so that you can get everything to build. Once you have done that, you can begin implementing your functions, and testing appropriately.

The tester code creates an executable that you can run by typing ./linkedlist, which takes parameters depending on whether or not it should run the exemplary tests. The easiest way to run the tests is to use ./test-m and ./test-e, as usual, which will automatically compile all of your code and run the ./linkedlist executable for you.

Your code should have no memory errors when running on any input (correct or incorrect) using valgrind. The testing scripts will automatically run valgrind on your code, and show you if there are memory errors.

4 Capstone work

Work in this section is 100% optional, and doesn’t contribute towards your grade. Nonetheless, if you’re looking for an extra challenge, these are fun additional exercises to try.

You’ve already implemented some Scheme functions: car, cdr, and cons. In the same spirit, add new functions titled append, and list that are analogs to the functions of the same name in Scheme. You’ve got more flexibility in how you go about this; we won’t stress test these in quite the same way as the rest, so make sure to indicate in your comment and in your tests what you’ve done.

5 What to submit

Make sure that you have added linkedlist.c correctly, then push to GitHub. (You can leave out your compiled executables as well as the .o object files if you wish.) All of your work should be in linkedlist.c, so all you should really need to add is linkedlist.c.

The graders and I need to know which commit is the one that we should use for grading. As usual, label it as SUBMITTED with a commit message, and prepend with M: or E: to run tests. Remember to only push with an M: or an E: after you have verified that your tests run correctly, so we can save minutes of time on GitHub.

Good luck, and have fun!


This assignment was originally created by David Liben-Nowell and has since been updated by Dave Musicant and Laura Effinger-Dean.