week 11 monday what did we talk about last time enum
play

Week 11 - Monday What did we talk about last time? enum Doubly - PowerPoint PPT Presentation

Week 11 - Monday What did we talk about last time? enum Doubly linked lists There are two ways of constructing a software design. One way is to make it so simple that there are obviously no deficiencies. And the other way is to make


  1. Week 11 - Monday

  2.  What did we talk about last time?  enum  Doubly linked lists

  3. There are two ways of constructing a software design. One way is to make it so simple that there are obviously no deficiencies. And the other way is to make it so complicated that there are no obvious deficiencies. Charles Antony Richard Hoare Inventor of Quicksort

  4.  Node consists of data, a next pointer, and a previous pointer  Advantages: bi-directional movement  Disadvantages: slower, 4 pointers must change for every insert/delete X head 23 47 58 X tail

  5.  We'll use this definition for our node for doubly linked lists typedef struct _node { int data; struct _node* next; struct _node* previous; } node;  Somewhere, we will have the following variables to hold the beginning and ending of the list node* head = NULL; node* tail = NULL;

  6.  Let's define a function that adds a value to the back of a (possibly empty) doubly linked list  Since the head and the tail might both get updated, the only reasonable way to write the function is to take a pointer to each void addToBack(node** headPointer, node** tailPointer, int value);

  7.  A tree is a data structure built out of nodes with children  Every child has exactly one parent node  There are no loops in a tree  A tree expresses a hierarchy or a similar relationship  The root is the top of the tree, the node which has no parents  A leaf of a tree is a node that has no children  An inner node is a node that does have children  An edge or a link connects a node to its children  A subtree is a node in a tree and all of its children

  8.  A binary tree is a tree such that each node has two or fewer children  The two children of a node are generally called the left child and the right child , respectively

  9.  A binary search tree is binary tree with three properties: The left subtree of the root only contains nodes with keys less than 1. the root’s key 2. The right subtree of the root only contains nodes with keys greater than the root’s key Both the left and the right subtrees are also binary search trees 3.

  10. 4 2 5 1 3 6

  11. typedef struct _Tree { int data; struct _Tree* left; struct _Tree* right; } Tree;

  12.  Write a function that will find an element in a BST  Use recursion  Hints:  If the value is smaller than the current root, look to the left  If the value is larger than the current root, look to the right Tree* find(Tree* root, int value);

  13.  Write a function that will add an element to a BST  Use recursion  Hint: Look for the location where you would add the element, then add when you reach a NULL Tree* add(Tree* root, int value);

  14.  Think of a file as a stream of bytes  It is possible to read from the stream  It is possible to write to the stream  It is even possible to do both  Central to the idea of a stream is also a file stream pointer, which keeps track of where in the stream you are  We have been redirecting stdin from and stdout to files, but we can access them directly as well

  15.  To open a file, call the fopen() function  It returns a pointer to a FILE object  Its first argument is the path to the file as a null-terminated string  Its second argument is another string that says how it is being opened (for reading, writing, etc.) FILE* file = fopen("data.txt", "r");

  16.  The following are legal arguments for the second string Argument Meaning "r" Open for reading. The file must exist. "w" Open for writing. If the file exists, all its contents will be erased. Open for appending. Write all data to the end of the file, preserving anything that is "a" already there. "r+" Open a file for reading and writing, but it must exist. "w+" Open a file for reading and writing, but if it exists, its contents will be erased. "a+" Open a file for reading and writing, but all writing is done to the end of the file.

  17.  Once you've got a file open, write to it using fprintf() the same way you write to the screen with printf()  The first argument is the file pointer  The second is the format string  The third and subsequent arguments are the values FILE* file = fopen("output.dat", "w"); fprintf(file, "Yo! I got %d on it!\n", 5);

  18.  Review  Lab tomorrow

  19.  Keep working on Project 4  Read K&R Chapter 7  Exam 2 is Friday

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend