290 likes | 417 Views
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)
E N D
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
Memory Cell Layout main Scan_address Scanf Malloc/free a.out ecs30 Winter 2012 Lecture #22 (guest lecture)
char * strdup(char *s) { return strcpy( (char *) malloc(sizeof(char) *(strlen(s) + 1)), s); } ecs30b Fall 2008 Lecture #27
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
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
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
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
Standard File I/O stdout stdin Program stderr foo bar ecs30 Winter 2012 Lecture #11
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
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
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
Abstractive Data Structures • Array • Stack • Queue • Linked-List ecs30b Fall 2008 Lecture #27
Linked-List struct ecs30b Fall 2008 Lecture #27
Linked-List struct struct struct ecs30b Fall 2008 Lecture #27
Linked-List struct struct struct NULL NULL ecs30b Fall 2008 Lecture #27
Linked-List struct struct struct NULL NULL struct ecs30b Fall 2008 Lecture #27
Linked-List struct struct struct NULL NULL struct ecs30b Fall 2008 Lecture #27
Linked-List struct struct struct NULL NULL struct ecs30b Fall 2008 Lecture #27
Linked-List struct struct NULL NULL ecs30b Fall 2008 Lecture #27
struct ll_element { // single link struct ll_element *next; // data, content … }; ecs30b Fall 2008 Lecture #27
struct ll_element { // double links struct ll_element *previous; struct ll_element *next; // data, content … }; ecs30b Fall 2008 Lecture #27
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
struct ll_element *llp = head_llp; while (llp != NULL) { (*(llp->print))(); // printing data llp = llp->next; } ecs30b Fall 2008 Lecture #27
Review ecs30 Winter 2012 Lecture #01
(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
Rules/Logic Decision Repeat Functions call by value call by reference Recursion ecs30 Winter 2012 Lecture #01
Data Types Simple types: int, double, char Structure types: struct <name> {…}; Pointers/Arrays ecs30 Winter 2012 Lecture #01
Integer/long Address Pointer &, *, -> Array (multiple diminsional) ecs30 Winter 2012 Lecture #01
smaller \0 larger dst src ecs30 Winter 2012 Lecture #01