1 / 24

CS222

Week 9 - Wednesday. CS222. Last time. What did we talk about last time? structs. Questions?. Project 4. Quotes. C combines the power and performance of assembly language with the flexibility and ease-of-use of assembly language. Anonymous. Example. struct s tudent {

trent
Télécharger la présentation

CS222

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Week 9 - Wednesday CS222

  2. Last time • What did we talk about last time? • structs

  3. Questions?

  4. Project 4

  5. Quotes C combines the power and performance of assembly language with the flexibility and ease-of-use of assembly language. Anonymous

  6. Example structstudent { char name[100]; double GPA; intID; }; • Read in 100 student names, GPAs, and ID numbers • Sort them by ID numbers • Print out the values

  7. Naming types • You might have noticed that there are all these odd types floating around • time_t • size_t • On some systems, you will even see aliases for your basic types • FLOAT • INT32 • How do people create new names for existing types?

  8. typedef • The typedef command allows you to make an alias for an existing type • You type typedef, the type you want to alias, and then the new name • Don't overuse typedef • It is useful for types like time_t which can have different meanings in different systems typedefintSUPER_INT; SUPER_INT value = 3; //has type int

  9. typedef with structs • The typedef command is commonly used with structs • Often it is built into the struct declaration process • It allows the programmer to leave off the stupid struct keyword when declaring variables • The type defined is actually struct _wombat • We can refer to that type as wombat typedefstruct_wombat { char name[100]; double weight; } wombat; wombat martin;

  10. Even more confusing! • You can actually typedef the name of the struct to be the same without the struct part • Or, if you don't need the name of the struct inside itself, you can typedef an anonymous struct typedefstructwombat { char name[100]; double weight; } wombat; typedefstruct { char name[100]; double weight; } wombat;

  11. Linked lists

  12. Linked lists • Since you have all taken CS122 (and many have taken CS221), you all know the power of the linked list • A linked list is a dynamic data structure with the following features: • Insertion, add, and delete can be O(1) time • Search is O(n) time • They are ideally suited for a merge sort • They are a pain to program

  13. Singly linked list • Node consists of data and a single next pointer • Advantages: fast and easy to implement • Disadvantages: forward movement only head 23 47 58 X

  14. Linked lists in C • Since C doesn't have classes, we can't make a self-contained linked list • But we can create nodes and a set of operations to use on them • Clearly, we will need a struct to make the node • It will contain data • It will contain a pointer to the next node in the list • Doubly-linked lists are possible too

  15. An example node struct • We'll use this definition for our node for singly linked lists • Somewhere, we will have the following variable to hold the beginning of the list typedefstruct_node { intdata; struct_node* next; } node; node* head = NULL;

  16. Add to front • Let's define a function that takes a pointer to a (possibly empty) linked list and adds a value to the front • There are two possible ways to do it • Return the new head of the list • Take a pointer to a pointer and change it directly node* add(node* head, int value); void add(node** headPointer, int value);

  17. Find • Let's define a function that takes a pointer to a (possibly empty) linked list and a value and returns the node containing the value • Or NULL if there is no such node node* find(node* head, int value);

  18. Sum values • Let's define a function that takes a pointer to a (possibly empty) linked list and returns the sum of the values inside • An empty list has a sum of 0 int sum(node* head);

  19. Remove • Let's define a function that takes a pointer to a (possibly empty) linked list and deletes the first occurrence of a given value • List is unchanged if the value isn't found • There are two possible ways to do it • Return the new head of the list • Take a pointer to a pointer and change it directly node* remove(node* head, int value); void remove(node** headPointer, int value);

  20. Insert in sorted order • Let's define a function that takes a pointer to a (possibly empty) linked list and adds a value in sorted order (assuming that the list is already sorted) • There are two possible ways to do it • Return the new head of the list • Take a pointer to a pointer and change it directly node* add(node* head, int value); void add(node** headPointer, int value);

  21. Quiz

  22. Upcoming

  23. Next time… • The enumtype • Lab 9

  24. Reminders • Keep working on Project 4 • Keep reading K&R chapter 6

More Related