1 / 29

ecs30 Summer 2014: Programming and Problem Solving #11: Linked List

ecs30 Summer 2014: Programming and Problem Solving #11: Linked List. Dr. S. Felix Wu Computer Science Department University of California, Davis http://www.cs.ucdavis.edu/~wu/ wu@cs.ucdavis.edu. Memory Cell Layout. main. Scan_address. Scanf. Malloc /free. a.out. char * strdup(char *s)

lorin
Download Presentation

ecs30 Summer 2014: Programming and Problem Solving #11: Linked List

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. ecs30 Summer 2014:Programming and Problem Solving#11: Linked List Dr. S. Felix Wu Computer Science Department University of California, Davis http://www.cs.ucdavis.edu/~wu/ wu@cs.ucdavis.edu ecs30 Winter 2012 Lecture #01

  2. Memory Cell Layout main Scan_address Scanf Malloc/free a.out ecs30 Winter 2012 Lecture #22 (guest lecture)

  3. char * strdup(char *s) { return strcpy( (char *) malloc(sizeof(char) *(strlen(s) + 1)), s); } ecs30b Fall 2008 Lecture #27

  4. Debugging Logic Errors for (i = 1; i < n; ++i) product *= i; Section 5.10 for (i = 1; i < n; ++i) { product *= i; printf(“product=%d, i=%d\n”, product, i); } printf(“OUT:product=%d, I =%d\n”, product, i); ecs30 Winter 2012 Lecture #11

  5. Preprocessor #define FACT_DEBUG … … for (i = 1; i < n; ++i) { product *= i; #ifdef FACT_DEBUG printf(“product=%d, i=%d\n”, product, i); #endif/* FACT_DEBUG */ } #ifdef FACT_DEBUG printf(“OUT:product=%d, I =%d\n”, product, i); #endif /* FACT_DEBUG */ % gcc -c fact.c -DFACT_DEBUG % gcc -c fact.c -DFACT_DEBUG=XYZ ecs30 Winter 2012 Lecture #11

  6. The C “TrIO” • printf, scanf(stdout, stdin) • [printf/scanf](<format string>) • fprintf, fscanf(file pointer) • Chapter 12 • [fprintf/fscanf](<FILE *>, <format string>) • fopen and fclose • sprintf, sscanf(string) • Chapter 9 • [sprintf/sscanf](<char *>, <format string>) ecs30 Winter 2012 Lecture #11

  7. File I/O stdout stdin Program stderr foo scanf(“%d”, &x); fscanf(f, “%d”, &x); sscanf(s, “%d”, &x); bar ecs30 Winter 2012 Lecture #11

  8. Standard File I/O stdout stdin Program stderr foo bar ecs30 Winter 2012 Lecture #11

  9. FILE *out_file_p; FILE *out_debug_file_p; intmain(void) {… /* initialization */ out_file_p = fopen("output.hw6_1", "w"); out_debug_file_ p = fopen("debug.hw6_1", "w"); If (out_file_p == NULL) {exit(-1);} /* using the FILE pointers */ /* regular output example */ fprintf(out_file_p, "...", ...); /* debug output example */ #ifdef ECS30_DEBUG fprintf(out_debug_file_p, "...", ...); #endif /*ECS30_DEBUG */ /* finishing/edning */ fclose(out_file_p); fclose(out_debug_file_p); ecs30 Winter 2012 Lecture #17

  10. GNU debugger • The -g flag • gdb a.out • run (r) • break (b) • next (n), nexti (ni) • step (s) • print (p) • display • continue (c) • watch • x ecs30 Winter 2012 Lecture #16

  11. gdb Examples $ script hw7_1_gdb.script • display i • c • n 2 • ni 9 • s • watch i == 3 • watch i != 0 • x /4 0xbffff862 • p i • p s • p s[14] • p s[12] = ‘\0’ • p i = 0 • b main • b strlen • b 12 http://www.digilife.be/quickreferences/QRC/GDB Quick Reference.pdf http://www.hlrs.de/organization/amt/services/tools/debugger/gdb/doc/gdb-6.3.pdf ecs30 Winter 2012 Lecture #16

  12. Abstractive Data Structures • Array • Stack • Queue • Linked-List ecs30b Fall 2008 Lecture #27

  13. Linked-List struct ecs30b Fall 2008 Lecture #27

  14. Linked-List struct struct struct ecs30b Fall 2008 Lecture #27

  15. Linked-List struct struct struct NULL NULL ecs30b Fall 2008 Lecture #27

  16. Linked-List struct struct struct NULL NULL struct ecs30b Fall 2008 Lecture #27

  17. Linked-List struct struct struct NULL NULL struct ecs30b Fall 2008 Lecture #27

  18. Linked-List struct struct struct NULL NULL struct ecs30b Fall 2008 Lecture #27

  19. Linked-List struct struct NULL NULL ecs30b Fall 2008 Lecture #27

  20. struct ll_element { // single link struct ll_element *next; // data, content … }; ecs30b Fall 2008 Lecture #27

  21. struct ll_element { // double links struct ll_element *previous; struct ll_element *next; // data, content … }; ecs30b Fall 2008 Lecture #27

  22. struct ll_element { // double links struct ll_element *previous; struct ll_element *next; // data, content void *data; // operations int (*_init)(void *); int (*_print)(void); int (*_find)(void *); }; ecs30b Fall 2008 Lecture #27

  23. struct ll_element *llp = head_llp; while (llp != NULL) { (*(llp->print))(); // printing data llp = llp->next; } ecs30b Fall 2008 Lecture #27

  24. Review ecs30 Winter 2012 Lecture #01

  25. (1) Problem Solving Variables, I/O, Rules/Logic Efficiency, Correctness (2) Programming Language C versus the rest of the world ecs30 Winter 2012 Lecture #01

  26. Rules/Logic Decision Repeat Functions call by value call by reference Recursion ecs30 Winter 2012 Lecture #01

  27. Data Types Simple types: int, double, char Structure types: struct <name> {…}; Pointers/Arrays ecs30 Winter 2012 Lecture #01

  28. Integer/long Address Pointer &, *, -> Array (multiple diminsional) ecs30 Winter 2012 Lecture #01

  29. smaller \0 larger dst src ecs30 Winter 2012 Lecture #01

More Related