Algorithms
and
Data
Structures

Spring 2022
course
site
-->

Mon March 7

In our last class I dumped a bunch of new ideas on you - both some new data structures (linked lists, stacks) and some new C syntax and memory concepts (pointers, structs, malloc).

Today let's unpack that and dig in more slowly.

I've uploaded an even shorter linked list example in both python and C to code/linked_lists, and would like to look at both at pythontutor.com. (Yes, that site lets you visualize C code too.)


To do in class :

OK, now let's look a bit more closely at this "pointer" concept, and how python is actually (sometimes) doing the same thing.

The point here is that python sometimes uses pointers (for objects and indexed arrays) but sometimes doesn't (for integer values), deciding what it thinks is best. C on the other hand is always completely explicit.

For another example, here's the program that I mentioned Friday :

// You can do this in C but not in Python.  ;)
#include <stdio.h>

void swap(int* i_ptr, int* j_ptr){
  int temp = *i_ptr;
  *i_ptr = *j_ptr;
  *j_ptr = temp;
}

int main(){
  int a = 10;
  int b = 20;
  printf(" a=%i , b=%i  \n", a, b);
  swap(&a, &b);
  printf(" a=%i , b=%i  \n", a, b);
  return 0;
}

Can you do something similar in Python instead? (Hint: think indexed arrays.)


Another class exercise :

Can you implement a linked list in Python ... without using "class" ?

https://cs.bennington.college /courses /spring2022 /algorithms /notes /march7
last modified Mon March 7 2022 3:34 pm

attachments [paper clip]

  last modified size
TXT links_with_extras.py Mon Mar 07 2022 03:34 pm 1.3K