Lecture 10 --------------- Announce ----------------- - hw6 due today! - midterm next monday - review on friday - project2 due fri - hw7 posted - hw4/5 grades avail This Week ------------------ - dynamic data structures - recursive functions - UNIX system calls - file descritors - file IO - process management - signals Today ------------- - linked lists - stacks/queues - recursion - binary trees Proj 2 course of action ----------------------- - understand the game - code a linked list - code the game using the linked list Linked Lists ---------------------- - simple structure usually used to store dynamic lists of data - can be ordered - can be unordered - elements are 'nodes' ________________ | data | |---------------| | ptr to next |----> |_______________| - starts with a 'head' pointer, pointing to NULL - ends with a pointer to NULL - traverse function uses node pointer move, returns the pointer - remove using a curr ptr and prev ptr. - insert at the front or end or middle (can sort 'while you build') - often times like to have some state about the linked list - an enumeration of size, head, tail, etc Stacks/Queues ----------------- - stack is FILO - queue is FIFO - can use linked list with only head pointer to do stack - should use tail pointer to do tail (add to the end when building) Recursion ------------------------- - why don't programs run out of the text segment? - one reason is because you can call a function from itself! - recursive function calls - usually have a stop condition, followed by a recursive call ex: factorial float factorial(float num) { if (num == 0) { return(1); } return(num * factorial(num-1)); } Binary Trees ------------------------- - simple structure used to store ordered data - elements are nodes, first node is 'root', end nodes are 'leaves'. data lchild-> rchild-> - also, nodes have a comparison 'rule' imposed on them - new data is inserted, rule is used to determine whether the new data goes to the left or right of the current data. ex: 7, 3, 10, 1, 9, 12 - traverse 7 3 10 1 9 12 - recursive call, uses 'left, self, right' idea 1, 3, 7, 9, 10, 12 - searching is fast, log(n) time! - use recursion to construct and search through the tree. node *search(node *root, int searchnum) { if (searchnum < root->num) { if (root->lchild != NULL) { return(search(root->lchild, searchnum)); } else { // not found return(NULL); } } else if (searchnum > root->num) { if (root->rchild != NULL) { return(search(root->rchild, searchnum)); } else { // not found return(NULL); } } else { return(root); } } - removing is tricky! ** FIGURE OUT - Problems with these data structures? - searching LL takes O(n) - searching BT takes O(log(n)) - do better? Hash Tables --------------------- - based on idea of a 'hashing function' - one-to-one function that takes data as input and returns uniq number as output. - ex: create a hashing function that takes a string as input and returns an integer idx - we know range of integers hashing func can return - allocate a static array of char *s of that many values - new string is to be 'inserted', simply hash the string to get an index, and set the array pointer at that idx to string - how much time to insert? O(1) - how much time to search? O(1) - Hash Table Problems - how much time to sort? EEK! - how much memory? EEK! - Current P2P systems use hashing to avoid distributed syncing of data structure