Spring 2021
-->

# Mon March 8

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

Start recording.

Questions?

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 :

• parsing strings made up of grouping symbols (parens, brackets, and braces)
• reversing a linked list : how to re-arrange and keep track of the pointer
• searching through a linked list

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

• binary search trees and heaps (tree structures can get you O(log n) for search & modify)
• hash tables (i.e. dictionaries) (which gets you O(1) lookup)

## a singly linked list example

To start, let's take a look at this linked_list_basic.py 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 pythontutor.com 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" foo.py and foo.c.

## context

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 :

• An indexed array is ....
• A linked list is ...
• What operations do we want to do with them?
• What is the O() behavior of these operations, and why?
• Why do we care about these things?
• What are the pros and cons of each of these things?

And here are a few of the details :

• a "dynamic array" is ...

## languages

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.

## recursion

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 :

• link management and special cases (insert, push, pop)
• testing ordinary and special cases
• keeping track of the length as you go along

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.)

• With an explicit loop
• Using recursion.
• Which is faster and why?
• Aside : discuss memoization

Group class exercise :

# asides

https://cs.bennington.college /courses /spring2021 /algorithms /notes /stacks