Linked List (II) CGS 3460, Lecture 37 Apr 12, 2006 Hen-I Yang
Previously… Union and Enum Linked list
Agenda Homework 6 (Finally) Linked List Introduction to preprocessor
Homework 6 2 problems Problem 1: Iterative and Recursive Functions Convert Decimal Numbers to Binary Representations Problem 2: Linked List Contact List Homework 6 description Needed.h and.c files.h.c Make sure you check back later today.
Quiz 5 and the Lecture on April 14 Quiz 5 has 3 questions, less questions in each set The material is much harder than any of the previous ones, make sure you spend enough time studying for this Instead of the normal 15 – 20 minutes, I will allot 40 minutes for this quiz and you are allowed to turn in early For the remaining 10 minutes, I will hold a tutorial type Q&A session for any questions related to the materials we’ve covered so far.
Linked List Operations Start a list Create a node Insert Search Query Print
Build a Linked List (I) Build a structure representing a node struct athlete { float speed; struct athlete * next; }; Initialize the node struct athelete *first = NULL;
Build a linked list: Create a node allocate memory for the node storing data into the node; insert the node into the list struct athlete *new_player; new_player = malloc(sizeof(struct athelete)); (*new_player).speed = 9.90; new_player next = NULL; first = new_player;
Insert a node: insert in the front new_player = malloc(sizeof(struct athelete)); new_player speed = 9.85; new_player next = first next; first = new_player;
Insert a node: insert at the end new_player = malloc(sizeof(struct athelete)); new_player speed = 10.10; new_player next = NULL; (first next) next = new_player;
Search and Delete Search: for (p = first; p != NULL; p = p next) { …. } Delete (almost reverse of insertion): locating the node to be deleted (see search a linked list) altering the previous node to bypass the deleted node calling free to reclaim the space occupied by the deleted node What to do to print everything out? (exercise)
Insert a node: insert in between new_player = malloc(sizeof(struct athelete)); new_player speed = 10.00; new_player next = NULL; struct athelete * q; for (p = first; p != NULL; p = p next) { if (p speed > 10.00) { new_player next = q next; q next = new_player; } q = p; } (first next) next = new_player;
Pointer to Functions Pass as an argument in other function Exception handling More flexible (and abstract) programming int (*f) (int); (a function declaration, not definition) Example: void qsort(void * base, size_t nmemb, size_t size, int (*compare) (const void *, const void *));
What’s next? Preprocessors Directives (#include, #define) are handled by preprocessor Preprocessing is handled prior to compilation C program Modified C prog Object Code Powerful, but can be hard to debug Recommended to be used carefully and moderately compilerpreprocessor
Preprocssor Directives Macro definition #define, #undef File inclusion #include Conditional Compilation #if, #ifdef, #ifndef, #elseif, #else Others #error, #line, #pragma
Q&A It’s your chance to ask the questions.
Summary Linked List Introduction to Preprocessor
Before you go Homework 5 late submission deadline tomorrow at 11:59 pm Make sure you spend enough time study for this quiz on pointers. Pointer is one of the most difficult concepts in C language. Although there are less questions and more time, you will need to understand the concept to do well in this quiz. Ask TA if you have any questions about pointers.