190 likes | 373 Views
Winter 2008/Summer 2008 Tutorial #10 Enumerations, Unions, Linked Lists. COMP 1402. Overview of Tutorial #7. Enumerations Structs review Unions Unions vs Structs Linked lists. Enumerations. Many variables only have a small number of sensible values, e.g. alignment
E N D
Winter 2008/Summer 2008 Tutorial #10 Enumerations, Unions, Linked Lists COMP 1402
Overview of Tutorial #7 • Enumerations • Structs review • Unions • Unions vs Structs • Linked lists
Enumerations • Many variables only have a small number of sensible values, e.g. alignment • You could use an int: 1=left, 2=center, 3=right; but this is confusing (which is which?) and unsafe (what's 4?) • Enums provide a nice way of handling variables like this • Like structs, often used with typedef, but not necessarily
Defining Enumerations • typedef enum { • LEFT, • CENTER, • RIGHT • } Alignment; • /* ... */ • Alignment a = RIGHT; • At runtime, enum is really just an int • By default, the first value = 0, then 1, 2, etc. • Printing an enum will print an int, not the name • printf(“%d”, a) => 2
Specific enum values • The default 0, 1, 2... can be overridden, fully or partially: • typedef enum { • SMALL=1, • MEDIUM=5, • LARGE=10 • LARGER • LARGEST • } Size; • By default: • LARGER=11 • LARGEST=12
Why enumerations? • Which is clearer? Without enum: • int s = 7; With enum: • Shape s = BOX;
Review: Structs Basic structure construct: Defining a structure as a type: • struct { • type1 field1; • type2 field2; • … • } structName; • typedef struct { • type1 field1; • type2 field2; • … • } typeName;
Review: Structs in Memory • A struct's members occupy consecutive blocks of memory • typedef struct { • char id; • short x; • short y; • } Rect; • Rect* r = (Rect*)malloc(sizeof(Rect)); id x x y y r
Union • Declared similarly to structs • Unlike structs, all members occupy same space Basic union construct: Defining a union as a type: • union { • type1 field1; • type2 field2; • … • } unionName; • typedef union { • type1 field1; • type2 field2; • … • } unionName;
Unions in Memory • A union's members all occupy the same space in memory (contrast with the Rect struct) • (You'd never actually define a Rect like this!) • typedef union { • char id; • short x; • short y; • } Rect; • Rect* r = (Rect*)malloc(sizeof(Rect)); r id x x y y
Struct vs Union • Structs hold many values at one time • Unions hold a single value at one time • Changing a member of a struct does not change the other members • Changing a member of a union does change the other members (they overlap) • Struct: value1 and value2 and value3 • Union: value1 or value2 or value3
Linked Lists • Ordered sequence of items • Unlike arrays, do not occupy a single chunk of memory • Each item has it's own place in memory, and points to the next item • Lists are more dynamic than arrays (e.g. it is easier/faster to remove items from a list) • Two types: singly-linked and doubly-linked
Singly-Linked Lists • Each list “node” contains a value, and pointer to the next node • Often shown in “box and pointer” notation, e.g. the list 1, 2, 3, 4: 1 2 3 4 NIL
Limitations of Singly-Linked Lists • Given (only) the pointer x, what is the previous node? x 1 2 3 4 NIL • No way to know, we only know the next node, not the previous node
Doubly-Linked Lists • Identical to singly-linked lists, but with a pointer to the previous node • Allows walking forwards and backwards • Downside: More space overhead 1 2 3 4 NIL NIL
Linked List vs Array • Lists are simpler to modify than arrays • Consider removing from an array: 5 4 1 2 3 5 Now there's a gap, shift everything left... 4 1 2 Now the array is too large, shrink... 4 5 1 2 Done. That was tedious and slow! 4 5 1 2
Removing from a List • Removing from a list is much simpler 1 2 3 4 NIL Delete the node: 1 2 4 NIL Fix the preceding node's next pointer: 1 2 4 NIL • But we need a pointer to the previous node
Removing from a List (cont.) • Need the previous node, so removing from a doubly-linked list can be simpler • Remember to fix previous pointers too: 1 2 3 4 NIL NIL 1 2 4 NIL NIL 1 2 4 NIL NIL
Tutorial #7 Exercises • Go to the class webpage and do the exercises for Tutorial 7. • Use the Makefile to compile the exercises • The questions build on each other, so be sure your code works before moving on • Remember to use gdb if you get crashes