Spring 2021

Mon March 8

More on linked lists, stacks, queues, and Python to C tips.

Start recording.


This week we're going to continue to practice with what we started last week, so if the last assignment was a bit crazy, you can look at my versions, rework some of yours, and continue making sense of all this stuff.

The homework assignment for today was too long, and tried to do too much all at once, in too many languages. You all put in some good work; we'll continue to get better at this stuff and get more comfortable with both the ideas and the language syntax.

There are no classes this Thursday, so we don't meet again for a week. After I see where you all are with what we're doing, I'll post an assignment tomorrow which will give you a chance to use some of these ideas on a few specific problems :

Over the next few weeks the other classic data structures I'd like us to look at are

a singly linked list example

To start, let's take a look at this and draw some pictures to understand what's happening with what I call "link bookkeeping" during the push and pop operation.

The printed ids show that python is using references to keep track of things, even though the language doesn't emphasize that.

Let's run this in to see what's going on.

As you can see, this implementation acts as a stack. Could it be used as a queue? Why or why not?

As several of you have seen, the runestone material on this is a reasonable place to explore these ideas.

in class

We may do an in class coding stint to translate this into C.

We started coding this in C during class, the complete translation is now at linked_list_basic.c.

I've also attached the tiny "here's a class in both Python & C" and foo.c.


I'd like to remind ourselves the context of what we're doing and why.

So let's define and discuss a few terms and fill out this table :

And here are a few of the details :


We're using two different programming languages, not just one : Python and C. The the two books that I've used most often for this course in the past (Runestone's and Skienna's) use one of each. Most of you have more familiarity with Python, and in many ways its easier and more powerful. But C is closer to what's happening in the guts of the computer, and can give us a more concrete picture of how things work underneath. Python has a dictionary built-in ... but using one doesn't tell you how it works. C does not have dictionaries ... but you can learn to code one yourself. (And will soon.) The most common python implementations are written in C; not the other way around.


I'd also like to practice with writing recursive functions this week, and understanding better what it's good for. Many of the algorithms we'll be looking at this semester come in recursive flavors, and it can be a powerful tool.

So :

(1) Let's discuss recursion for a bit; what it is and why its cool.

(2) Let's talk about the homework, what parts of it made sense and which parts need more thinking and coding.

the last homework, in more detail

My (overdone) version of the homework code is in ../code/linked_lists ; I'll walk through some of the ideas :

If there's time, we'll should draw some pictures to understand specifically what happens during an insert operation.

The files in that folder are also examples of how to break up a Python or C program into multiple files, a "library" file that provides some sort of functionality, and then another "main" file that uses it.

coding practice

Depending on time and where the conversation takes us ...

Breakout class exercise - recursion vs iteration :

Write a program to calculate the n'th Fibonnaci number .

(This is a fairly classic example.)

Group class exercise :

asides /courses /spring2021 /algorithms /notes /stacks
last modified Mon March 8 2021 7:08 pm

attachments [paper clip]

  last modified size
TXT foo.c Mon Mar 08 2021 07:10 pm 939B
TXT Mon Mar 08 2021 07:10 pm 431B