Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 1402 Winter 2008/Summer 2008 Tutorial #10 Enumerations, Unions, Linked Lists.

Similar presentations


Presentation on theme: "COMP 1402 Winter 2008/Summer 2008 Tutorial #10 Enumerations, Unions, Linked Lists."— Presentation transcript:

1 COMP 1402 Winter 2008/Summer 2008 Tutorial #10 Enumerations, Unions, Linked Lists

2 Overview of Tutorial #7 Enumerations Structs review Unions Unions vs Structs Linked lists

3 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

4 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

5 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

6 Why enumerations? int s = 7; Which is clearer? Shape s = BOX; Without enum: With enum:

7 Review: Structs Basic structure construct: struct { type1 field1; type2 field2; … } structName; Defining a structure as a type: typedef struct { type1 field1; type2 field2; … } typeName;

8 Review: Structs in Memory A struct's members occupy consecutive blocks of memory id x y r typedef struct { char id; short x; short y; } Rect; Rect* r = (Rect*)malloc(sizeof(Rect));

9 Union Basic union construct: union { type1 field1; type2 field2; … } unionName; Defining a union as a type: typedef union { type1 field1; type2 field2; … } unionName; Declared similarly to structs Unlike structs, all members occupy same space

10 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!)‏ id r typedef union { char id; short x; short y; } Rect; Rect* r = (Rect*)malloc(sizeof(Rect)); xxxx yyyy

11 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

12 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

13 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: 1234 NIL

14 Limitations of Singly-Linked Lists Given (only) the pointer x, what is the previous node? 1234 NIL x No way to know, we only know the next node, not the previous node

15 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 NIL 234

16 Linked List vs Array Lists are simpler to modify than arrays Consider removing from an array: 12 3 4 5 12 4 5 Now there's a gap, shift everything left... 12 45 Now the array is too large, shrink... 12 45 Done. That was tedious and slow!

17 Removing from a List Removing from a list is much simpler 1234 NIL 124 Delete the node: 124 NIL Fix the preceding node's next pointer: But we need a pointer to the previous node

18 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 NIL 234 1 24 1 24

19 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


Download ppt "COMP 1402 Winter 2008/Summer 2008 Tutorial #10 Enumerations, Unions, Linked Lists."

Similar presentations


Ads by Google