Week 5 -Data Structure

Welcome to CS50! This is my review for Week 5's content.

Lecture

  1. Nothing much to note down.

Section

  1. There are two important parts in this line of code: int *p = malloc(sizeof(int) * 4)

    • int *p: This creates an int pointer on the stack

    • malloc(sizeof(int) * 4): This allocates 4 integers on the heap and returns the address of the first integer. This address is then stored in the pointer p.

  2. Using free(p), you just free the memory on the heap, which means the pointer p is still on available on the stack and now it points to NULL.

Shorts

Singly Linked List

  1. Define a linked list node using struct

typedef struct node
{
    int number;
    struct node *next;
}
node;

The struct node appearing twice here is important for the self-referencing.

  1. Destroy the whole list using the idea of recursion:

    1. If you've reached a NULL pointer, return. (Base case)

    2. Delete the rest of the list

    3. Free the current node

Tries

  1. Key-value pairs:

    • In Array: The key is the element index, the value is the data at that location.

    • In Hash Tables: The key is the hash code of the data, the value is a linked list of data hashing to that hash code (supposed using chaining)

  2. In Tries, the data to be searched for is now a roadmap:

    • If you can follow the map from beginning to end, the data exists in the trie.

    • If you can't, it doesn't exist.

    • For example, using the idea of key-value pair, the keys are four-digit years(YYYY) and the values are names of universities founded during those years. In this trie, the paths from a central root node to a leaf node (where the school names would be), would be labeled with digits of the year. Each node on the path from root to leaf could have 10 pointers emanating from it, one for each digit. Its structure can be defined as below:

    • To search for an element in the trie, use successive digits to navigate from the root, and if you can make it to the end without hitting a dead end (a NULL pointer), you've found it. (This is similar to using the key to find the unique value)

Problem Set 5

Divide and Conquer

  1. create_family()

  1. free_family()

Things to notice in the problem statement

  1. The implementation of check must be case-insensitive.

Divide and Conquer

  1. check(): Check whehter the word is in the dictionary

  1. hash(): Hash the word to a number.

Design a good hash function makes this problem interesting and here is my design, which will use the first two characters to determine the hash code of a word. To use the first two characters, we need to know the idea of base-26.

  1. load(): Load the dictionary into memory.

  1. size(): Return the number of words in the dictionary

  1. unload(): Unload the dictionary from memory

Take-aways

  1. To read the word from the dictionary, we need to use fscanf(), which is very useful to read from file besides fread().

Last updated