1 / 19

COMP 1402

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

golda
Download Presentation

COMP 1402

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. Winter 2008/Summer 2008 Tutorial #10 Enumerations, Unions, Linked Lists COMP 1402

  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? • Which is clearer? Without enum: • int s = 7; With enum: • Shape s = BOX;

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

  8. 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

  9. 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;

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

  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: 1 2 3 4 NIL

  14. 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

  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 2 3 4 NIL NIL

  16. 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

  17. 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

  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 2 3 4 NIL NIL 1 2 4 NIL NIL 1 2 4 NIL NIL

  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

More Related