PASCAL II - Data Structures Philip Fees CS341
Introduction  <ul><li>What will be studied? </li></ul><ul><ul><li>Data Types </li></ul></ul><ul><ul><li>Arrays </li></ul><...
Workshop 1 <ul><li>File I/O Concepts </li></ul><ul><li>Pascal File I/O </li></ul><ul><li>User Defined Types </li></ul>
File I/O Concepts <ul><li>Generally termed Device I/O </li></ul><ul><ul><li>Could be hard disk, CD-ROM, tape, etc. </li></...
Pascal File I/O - Handles <ul><li>Identify handle (symbolic name) </li></ul><ul><ul><li>Program myProgram (input, output, ...
Pascal File I/O - Open & Seek <ul><li>Open the file </li></ul><ul><ul><li>reset(fileHandle); </li></ul></ul><ul><li>rewind...
Pascal File I/O - Read & Write <ul><li>Read from file via handle </li></ul><ul><ul><li>readln(fileHandle, variable1, varia...
Exercises <ul><li>Pg. 448 25 - 30 </li></ul>
User Defined Types <ul><li>Characteristic of a data type (int, char, boolean) </li></ul><ul><ul><li>set of value </li></ul...
Enumerated Data Type <ul><li>Syntax </li></ul><ul><ul><li>Type </li></ul></ul><ul><ul><ul><li>Weekday = (Mon, Tues, Wed, T...
Subranges <ul><li>Define one type as subset of another type </li></ul><ul><li>Examples </li></ul><ul><ul><li>TYPE </li></u...
Exercises <ul><li>Pg. 454-455 # 2, 15 </li></ul><ul><li>Pg. 461 #22-26 </li></ul><ul><li>Pg. 465 #8, 20, 21, 22 </li></ul>
Workshop 2 <ul><li>Single Dimensional Arrays </li></ul><ul><li>Selection Sort </li></ul><ul><li>Arrays and Subroutines </l...
Arrays <ul><li>Collection of data elements </li></ul><ul><li>Data elements are the same type </li></ul><ul><li>Contiguous ...
Pascal Arrays <ul><li>name :  ARRAY [  index type  ] OF  type ; </li></ul><ul><li>Example 1: </li></ul><ul><ul><li>VAR </l...
Pascal Arrays (cont.) <ul><li>index type alternatives </li></ul><ul><ul><li>TYPE </li></ul></ul><ul><ul><ul><li>DAYS = (SU...
Use of Constants <ul><li>Good Pratice </li></ul><ul><ul><li>CONST </li></ul></ul><ul><ul><ul><li>ClassSize = 35; </li></ul...
Arrays and Loops <ul><li>“ Looping” over the array </li></ul><ul><li>Example: </li></ul><ul><ul><li>FOR J := 1 to ClassSiz...
Review <ul><li>Example 10.7 pg. 491 </li></ul><ul><li>Example 10.9 pg. 493 </li></ul><ul><li>Example 10.10 pg. 494  </li><...
Selection Sort <ul><li>Sorting a list of values </li></ul><ul><li>Algorithm </li></ul><ul><ul><li>start with first element...
Arrays and Subroutines <ul><li>See GetData example on pg. 508 </li></ul><ul><li>Call by value vs. call by reference </li><...
Search Algorithms <ul><li>Sequential Search (pg. 526) </li></ul><ul><ul><li>search entire array until value located or “hi...
Analysis <ul><li>Overhead of inserting new value in sorted array </li></ul><ul><li>What should maximum size of the array b...
Exercises <ul><li>Arrays: pp. 497-500 # 1-5, 10, 20 </li></ul><ul><li>Sorts: pg. 507 # 2, 3, 6 </li></ul><ul><li>Subroutin...
Workshop 3 <ul><li>Multi-dimensional arrays </li></ul>
Array in Memory <ul><li>Array stored in contiguous memory </li></ul><ul><li>Location is calculated: </li></ul><ul><ul><li>...
Pascal Syntax <ul><li>Syntax </li></ul><ul><ul><li><name> : ARRAY [ <row index>, <column index> ] OF <element type> </li><...
Two Dimensional Example <ul><li>Example 1: </li></ul><ul><ul><li>VAR </li></ul></ul><ul><ul><ul><li>Table : ARRAY [ 1..3, ...
Iteration <ul><li>Example: </li></ul><ul><ul><li>FOR row := 1 to 3 DO </li></ul></ul><ul><ul><ul><li>FOR column := 1 to 4 ...
Higher Dimensional Arrays <ul><li>Syntax </li></ul><ul><ul><li><identifier> : ARRAY [ A1 .. B1, A2 .. B2, …, An .. Bn] OF ...
Exercises <ul><li>pg. 567 # 4, 9-12, 13-15 </li></ul>
Workshop 4 <ul><li>Records </li></ul><ul><li>Variants </li></ul><ul><li>Binary Files </li></ul>
Workshop 5 <ul><li>Sets </li></ul>
Defining and Declaring <ul><li>Syntax </li></ul><ul><ul><li>TYPE </li></ul></ul><ul><ul><ul><li><type name>  = SET OF <bas...
Different than Subtypes <ul><li>Assignment </li></ul><ul><ul><li>numbers := [ 0, 2, 4, 6, 8 ]; </li></ul></ul><ul><li>Set ...
Set Operations <ul><li>Union:  A + B </li></ul><ul><li>Intersection: A * B </li></ul><ul><li>Difference: A - B </li></ul>
Relational Operators <ul><li>equal: A = B sets A and B are identical </li></ul><ul><li>not equal: A <> B set A and B are n...
Membership <ul><li>Syntax </li></ul><ul><ul><li><element> IN <set> </li></ul></ul><ul><li>Example </li></ul><ul><ul><li>2 ...
Membership Example <ul><li>Useful for “edit checks” </li></ul><ul><li>Example </li></ul><ul><ul><li>IF response IN [‘Y’, ‘...
Exercises <ul><li>Page 739 # 9-15, 26-30 </li></ul><ul><li>Page 743 # 2, 7-20, 27 </li></ul><ul><li>Programming Problem Pa...
Workshop 6 <ul><li>Model Builder </li></ul><ul><li>Abstract Data Type (ADT) </li></ul><ul><li>String ADT </li></ul><ul><li...
Model Builder <ul><li>Model, design, or abstraction - analysis without being concern for details  </li></ul><ul><li>Exampl...
Abstract Data Type <ul><li>collection of data (objects) </li></ul><ul><li>shared properties </li></ul><ul><li>shared opera...
ADT Example - String <ul><li>definition - finite sequence of characters </li></ul><ul><li>terminated with null character <...
ADT Example - Linked List <ul><li>Definition - list of data items associated by a link to one or more nodes. </li></ul><ul...
Workshop 7 <ul><li>Linked Lists </li></ul><ul><li>Array Implementation of Linked Lists </li></ul><ul><li>Pointers </li></u...
Why Linked Lists <ul><li>Arrays </li></ul><ul><ul><li>Size fixed at Compile Time, inefficient space utilization </li></ul>...
Linked Lists <ul><li>Diagram data structure (See page 872) </li></ul><ul><li>Define operations (See page 881) </li></ul><u...
Introduction to Pointers <ul><li>A pointer doesn’t contain the data, but contains a way of locating the data. </li></ul><u...
Array Implementation of Linked Lists <ul><li>Issues </li></ul><ul><ul><li>Still have compile time limit on number of nodes...
Array Implementation (cont.) <ul><li>Free nodes </li></ul><ul><ul><li>Mark all free node - sequential search </li></ul></u...
Pointers & Dynamic Memory <ul><li>Declaring a pointer (see page 874) </li></ul><ul><ul><li>Var </li></ul></ul><ul><ul><ul>...
Pointers & Dynamic Memory <ul><li>Declaring a pointer (see page 874) </li></ul><ul><ul><li>Var </li></ul></ul><ul><ul><ul>...
Pointers & Dynamic Memory <ul><li>Declaring a pointer (see page 874) </li></ul><ul><ul><li>Var </li></ul></ul><ul><ul><ul>...
Pointers & Dynamic Memory (cont.) <ul><li>NIL value - doesn’t point to anything </li></ul><ul><li>Memory allocated by new ...
Pointer Implementation of Linked Lists <ul><li>See page 889 </li></ul>
Variations on Linked List <ul><li>Dummy Header - avoid test for empty list </li></ul><ul><li>Circularly Linked - no NIL </...
Exercises <ul><li>Pg 872 # 16 </li></ul><ul><li>Pg 880 # 9-19 </li></ul>
Upcoming SlideShare
Loading in …5
×

Cs341

519 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
519
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
15
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Cs341

  1. 1. PASCAL II - Data Structures Philip Fees CS341
  2. 2. Introduction <ul><li>What will be studied? </li></ul><ul><ul><li>Data Types </li></ul></ul><ul><ul><li>Arrays </li></ul></ul><ul><ul><li>Records </li></ul></ul><ul><ul><li>Abstract Data </li></ul></ul><ul><ul><li>Pointers </li></ul></ul><ul><ul><li>Linked lists, Stacks, Queues </li></ul></ul><ul><li>What is a data structure? </li></ul>
  3. 3. Workshop 1 <ul><li>File I/O Concepts </li></ul><ul><li>Pascal File I/O </li></ul><ul><li>User Defined Types </li></ul>
  4. 4. File I/O Concepts <ul><li>Generally termed Device I/O </li></ul><ul><ul><li>Could be hard disk, CD-ROM, tape, etc. </li></ul></ul><ul><ul><li>Could be terminal, socket (IPC/Internet), etc. </li></ul></ul><ul><li>Delimited files (whitespace, eol, eof) vs. non delimited </li></ul><ul><li>Encoded (ASCII, EBCDIC) vs. binary </li></ul><ul><li>Open and Close </li></ul><ul><li>Read or write via file/device handle (symbolic name) </li></ul><ul><li>Seek vs. non-seek devices </li></ul><ul><li>Sequential vs. Indexed </li></ul>
  5. 5. Pascal File I/O - Handles <ul><li>Identify handle (symbolic name) </li></ul><ul><ul><li>Program myProgram (input, output, fileHandle ); </li></ul></ul><ul><ul><li>… </li></ul></ul><ul><ul><li>VAR </li></ul></ul><ul><ul><ul><li>fileHandle : text; </li></ul></ul></ul><ul><li>Associate file to handle </li></ul><ul><ul><li>“ create procedure file prior to compiling and running the program” </li></ul></ul><ul><ul><li>TP: assign(fileHandle,’myTextFile.txt’); </li></ul></ul>
  6. 6. Pascal File I/O - Open & Seek <ul><li>Open the file </li></ul><ul><ul><li>reset(fileHandle); </li></ul></ul><ul><li>rewind is part of “seeking” </li></ul><ul><li>delimiter tests </li></ul><ul><ul><li>WHILE not eol DO </li></ul></ul><ul><ul><li>WHILE not eol(fileHandle) DO </li></ul></ul><ul><ul><li>WHILE not eof DO </li></ul></ul><ul><ul><li>WHILE not eof(fileHandle) DO </li></ul></ul>
  7. 7. Pascal File I/O - Read & Write <ul><li>Read from file via handle </li></ul><ul><ul><li>readln(fileHandle, variable1, variable2, …); </li></ul></ul><ul><li>Write to file via handle </li></ul><ul><ul><li>writeln(fileHandle, ‘this is a test’, variable1, …); </li></ul></ul><ul><li>Ease old file contents </li></ul><ul><ul><li>rewrite(fileHandle); </li></ul></ul><ul><li>See example pg. 444 </li></ul>
  8. 8. Exercises <ul><li>Pg. 448 25 - 30 </li></ul>
  9. 9. User Defined Types <ul><li>Characteristic of a data type (int, char, boolean) </li></ul><ul><ul><li>set of value </li></ul></ul><ul><ul><li>fixed amount of memory </li></ul></ul><ul><li>Ordinal data type have pred() and succ() values </li></ul><ul><li>User defined a.k.a. enumerated data type </li></ul>
  10. 10. Enumerated Data Type <ul><li>Syntax </li></ul><ul><ul><li>Type </li></ul></ul><ul><ul><ul><li>Weekday = (Mon, Tues, Wed, Thur, Fri); </li></ul></ul></ul><ul><li>Usage </li></ul><ul><ul><li>VAR </li></ul></ul><ul><ul><ul><li>Day : Weekday; </li></ul></ul></ul><ul><ul><ul><li>… </li></ul></ul></ul><ul><ul><ul><li>Day := Mon; </li></ul></ul></ul><ul><ul><ul><li>if (Day = Mon) then </li></ul></ul></ul>
  11. 11. Subranges <ul><li>Define one type as subset of another type </li></ul><ul><li>Examples </li></ul><ul><ul><li>TYPE </li></ul></ul><ul><ul><ul><li>USYears = 1776..1998; </li></ul></ul></ul><ul><ul><ul><li>Days = (Sun, Mon, Tues, Wed, Thur, Fri, Sat); </li></ul></ul></ul><ul><ul><ul><li>Weekdays = Mon..Fri; </li></ul></ul></ul><ul><li>Operations: succ() and pred() </li></ul>
  12. 12. Exercises <ul><li>Pg. 454-455 # 2, 15 </li></ul><ul><li>Pg. 461 #22-26 </li></ul><ul><li>Pg. 465 #8, 20, 21, 22 </li></ul>
  13. 13. Workshop 2 <ul><li>Single Dimensional Arrays </li></ul><ul><li>Selection Sort </li></ul><ul><li>Arrays and Subroutines </li></ul><ul><li>Searching Arrays </li></ul>
  14. 14. Arrays <ul><li>Collection of data elements </li></ul><ul><li>Data elements are the same type </li></ul><ul><li>Contiguous in memory </li></ul><ul><li>Access individuals by subscript </li></ul><ul><li>Array size determined by: </li></ul><ul><ul><li>element size * number of elements </li></ul></ul>
  15. 15. Pascal Arrays <ul><li>name : ARRAY [ index type ] OF type ; </li></ul><ul><li>Example 1: </li></ul><ul><ul><li>VAR </li></ul></ul><ul><ul><ul><li>List : ARRAY [1..5] OF integer; </li></ul></ul></ul><ul><li>Example 2: </li></ul><ul><ul><li>TYPE </li></ul></ul><ul><ul><ul><li>Numbers = ARRAY [1..5] OF integer; </li></ul></ul></ul><ul><ul><li>VAR </li></ul></ul><ul><ul><ul><li>List : Numbers; </li></ul></ul></ul>
  16. 16. Pascal Arrays (cont.) <ul><li>index type alternatives </li></ul><ul><ul><li>TYPE </li></ul></ul><ul><ul><ul><li>DAYS = (SUN, MON, TUES, WED, THUR, FRI, SAT); </li></ul></ul></ul><ul><ul><li>VAR </li></ul></ul><ul><ul><ul><li>StockPriceList : ARRAY [9..16] OF real; </li></ul></ul></ul><ul><ul><ul><li>NegativeList : ARRAY [-2..3] OF char; </li></ul></ul></ul><ul><ul><ul><li>HoursWorked : ARRAY [MON..FRI] of real; </li></ul></ul></ul>
  17. 17. Use of Constants <ul><li>Good Pratice </li></ul><ul><ul><li>CONST </li></ul></ul><ul><ul><ul><li>ClassSize = 35; </li></ul></ul></ul><ul><ul><li>Type </li></ul></ul><ul><ul><ul><li>TestScores = 0..100; </li></ul></ul></ul><ul><ul><li>VAR </li></ul></ul><ul><ul><ul><li>Score : ARRAY [1..ClassSize] of TestScores; </li></ul></ul></ul>
  18. 18. Arrays and Loops <ul><li>“ Looping” over the array </li></ul><ul><li>Example: </li></ul><ul><ul><li>FOR J := 1 to ClassSize DO </li></ul></ul><ul><ul><li>BEGIN </li></ul></ul><ul><ul><ul><li>write(‘next: ‘); </li></ul></ul></ul><ul><ul><ul><li>readln(Score[J]); </li></ul></ul></ul><ul><ul><ul><li>writeln(‘value = ‘, Score[J]); </li></ul></ul></ul><ul><ul><li>END </li></ul></ul>
  19. 19. Review <ul><li>Example 10.7 pg. 491 </li></ul><ul><li>Example 10.9 pg. 493 </li></ul><ul><li>Example 10.10 pg. 494 </li></ul>
  20. 20. Selection Sort <ul><li>Sorting a list of values </li></ul><ul><li>Algorithm </li></ul><ul><ul><li>start with first element [1] (current) </li></ul></ul><ul><ul><li>find smallest element in array and exchange with current </li></ul></ul><ul><ul><li>current := next array element (current + 1) </li></ul></ul><ul><ul><li>continue to end of array </li></ul></ul><ul><li>What is the best and worst case? </li></ul>
  21. 21. Arrays and Subroutines <ul><li>See GetData example on pg. 508 </li></ul><ul><li>Call by value vs. call by reference </li></ul><ul><ul><li>Call by value: create a copy of the array </li></ul></ul><ul><ul><li>Call by reference: refer to the passed array </li></ul></ul><ul><ul><li>Performance implications </li></ul></ul>
  22. 22. Search Algorithms <ul><li>Sequential Search (pg. 526) </li></ul><ul><ul><li>search entire array until value located or “hit” the end of the array </li></ul></ul><ul><ul><li>Average of N iterations of loop </li></ul></ul><ul><li>Binary Search (pg. 528) </li></ul><ul><ul><li>Assumes sorted array </li></ul></ul><ul><ul><li>start in middle; look in upper or lower half </li></ul></ul><ul><ul><li>Average of log N iterations of loop </li></ul></ul>
  23. 23. Analysis <ul><li>Overhead of inserting new value in sorted array </li></ul><ul><li>What should maximum size of the array be? </li></ul><ul><li>When should an array be used? </li></ul>
  24. 24. Exercises <ul><li>Arrays: pp. 497-500 # 1-5, 10, 20 </li></ul><ul><li>Sorts: pg. 507 # 2, 3, 6 </li></ul><ul><li>Subroutines: pg. 516 # 16-19 </li></ul><ul><li>Searching: pg. #533 5, 15 </li></ul>
  25. 25. Workshop 3 <ul><li>Multi-dimensional arrays </li></ul>
  26. 26. Array in Memory <ul><li>Array stored in contiguous memory </li></ul><ul><li>Location is calculated: </li></ul><ul><ul><li>Starting address + (row index * # of columns) + column index </li></ul></ul><ul><li>Row Major vs. Column Major </li></ul>
  27. 27. Pascal Syntax <ul><li>Syntax </li></ul><ul><ul><li><name> : ARRAY [ <row index>, <column index> ] OF <element type> </li></ul></ul>
  28. 28. Two Dimensional Example <ul><li>Example 1: </li></ul><ul><ul><li>VAR </li></ul></ul><ul><ul><ul><li>Table : ARRAY [ 1..3, 1.. 4 ] OF integer; </li></ul></ul></ul><ul><li>Example 2: </li></ul><ul><ul><li>TYPE </li></ul></ul><ul><ul><ul><li>Maxtrix = [ 1.. 3, 1.. 4] OF integer; </li></ul></ul></ul><ul><ul><li>VAR </li></ul></ul><ul><ul><ul><li>Table : Matrix; </li></ul></ul></ul>
  29. 29. Iteration <ul><li>Example: </li></ul><ul><ul><li>FOR row := 1 to 3 DO </li></ul></ul><ul><ul><ul><li>FOR column := 1 to 4 DO </li></ul></ul></ul><ul><ul><ul><ul><li>writeln(‘Table[‘,row,’, ‘, column,’] = ‘, Table[row][column]); </li></ul></ul></ul></ul>
  30. 30. Higher Dimensional Arrays <ul><li>Syntax </li></ul><ul><ul><li><identifier> : ARRAY [ A1 .. B1, A2 .. B2, …, An .. Bn] OF <type> </li></ul></ul><ul><li>Example1: </li></ul><ul><ul><li>cube : ARRAY [ 1..3, 1..4, 1..5] OF integer; </li></ul></ul><ul><li>Example 2: </li></ul><ul><ul><li>cube : ARRAY [ 1..3 ] OF ARRAY [ 1..4, 1..5 ] OF integer; </li></ul></ul>
  31. 31. Exercises <ul><li>pg. 567 # 4, 9-12, 13-15 </li></ul>
  32. 32. Workshop 4 <ul><li>Records </li></ul><ul><li>Variants </li></ul><ul><li>Binary Files </li></ul>
  33. 33. Workshop 5 <ul><li>Sets </li></ul>
  34. 34. Defining and Declaring <ul><li>Syntax </li></ul><ul><ul><li>TYPE </li></ul></ul><ul><ul><ul><li><type name> = SET OF <base type> ; </li></ul></ul></ul><ul><ul><li>VAR </li></ul></ul><ul><ul><ul><li><variable name> : <type name> ; </li></ul></ul></ul><ul><li>Example </li></ul><ul><ul><li>TYPE </li></ul></ul><ul><ul><ul><li>Digits = SET OF 0..9; </li></ul></ul></ul><ul><ul><li>VAR </li></ul></ul><ul><ul><ul><li>numbers : Digits; </li></ul></ul></ul>
  35. 35. Different than Subtypes <ul><li>Assignment </li></ul><ul><ul><li>numbers := [ 0, 2, 4, 6, 8 ]; </li></ul></ul><ul><li>Set is undefined until assignment </li></ul><ul><li>Assigned values must be in base type - elements of the set </li></ul><ul><li>Universal sets contain all values </li></ul><ul><li>Subsets - one set contains all members of another set </li></ul><ul><li>See set definitions on top of page 735. </li></ul>
  36. 36. Set Operations <ul><li>Union: A + B </li></ul><ul><li>Intersection: A * B </li></ul><ul><li>Difference: A - B </li></ul>
  37. 37. Relational Operators <ul><li>equal: A = B sets A and B are identical </li></ul><ul><li>not equal: A <> B set A and B are not identical </li></ul><ul><li>subset: A <= B A is a subset of B </li></ul><ul><li>superset A >= B A is a superset of B (A<=B) </li></ul>
  38. 38. Membership <ul><li>Syntax </li></ul><ul><ul><li><element> IN <set> </li></ul></ul><ul><li>Example </li></ul><ul><ul><li>2 IN numbers </li></ul></ul><ul><li>Boolean value: is 2 in the numbers set? </li></ul>
  39. 39. Membership Example <ul><li>Useful for “edit checks” </li></ul><ul><li>Example </li></ul><ul><ul><li>IF response IN [‘Y’, ‘y’] THEN </li></ul></ul><ul><ul><ul><li>(continue action here) </li></ul></ul></ul><ul><ul><li>ELSE </li></ul></ul><ul><ul><ul><li>(alternative action here) </li></ul></ul></ul><ul><li>Review example on page 744. </li></ul>
  40. 40. Exercises <ul><li>Page 739 # 9-15, 26-30 </li></ul><ul><li>Page 743 # 2, 7-20, 27 </li></ul><ul><li>Programming Problem Page 759 # 1, 3 </li></ul>
  41. 41. Workshop 6 <ul><li>Model Builder </li></ul><ul><li>Abstract Data Type (ADT) </li></ul><ul><li>String ADT </li></ul><ul><li>Linked List ADT </li></ul>
  42. 42. Model Builder <ul><li>Model, design, or abstraction - analysis without being concern for details </li></ul><ul><li>Examples </li></ul><ul><ul><li>World Wide Web </li></ul></ul><ul><ul><li>Windows </li></ul></ul><ul><ul><li>Virus </li></ul></ul><ul><ul><li>UNIX: parent process, child process, kill, fork, etc. </li></ul></ul>
  43. 43. Abstract Data Type <ul><li>collection of data (objects) </li></ul><ul><li>shared properties </li></ul><ul><li>shared operations </li></ul><ul><li>Examples: Array, Integers, </li></ul>
  44. 44. ADT Example - String <ul><li>definition - finite sequence of characters </li></ul><ul><li>terminated with null character </li></ul><ul><li>access individual elements (substring) </li></ul><ul><li>Operations: create, read, write, assign, length </li></ul><ul><li>reconsider operations if string is defined by length not null terminator. </li></ul>
  45. 45. ADT Example - Linked List <ul><li>Definition - list of data items associated by a link to one or more nodes. </li></ul><ul><li>Typically point to next node (single linked) or previous node (double linked) </li></ul><ul><li>Head node is first node in list </li></ul><ul><li>To “walk” the list, must start at head and proceed sequentially. </li></ul><ul><li>Task: Define operations, define interfaces, implement </li></ul>
  46. 46. Workshop 7 <ul><li>Linked Lists </li></ul><ul><li>Array Implementation of Linked Lists </li></ul><ul><li>Pointers </li></ul><ul><li>Pointer Implementation of Linked Lists </li></ul>
  47. 47. Why Linked Lists <ul><li>Arrays </li></ul><ul><ul><li>Size fixed at Compile Time, inefficient space utilization </li></ul></ul><ul><ul><li>Inserting, deleting, organizing are costly. Why? </li></ul></ul><ul><li>Requirement </li></ul><ul><ul><li>Size determined at Run Time </li></ul></ul><ul><ul><li>Minimal cost to insert, delete, and organize data </li></ul></ul><ul><li>Examples </li></ul><ul><ul><li>Sorting, data with undetermined number of items </li></ul></ul>
  48. 48. Linked Lists <ul><li>Diagram data structure (See page 872) </li></ul><ul><li>Define operations (See page 881) </li></ul><ul><li>Define implementation base data structures (See page 873) </li></ul><ul><li>Diagram operation’s effect on sample data structure (See page 887) </li></ul>
  49. 49. Introduction to Pointers <ul><li>A pointer doesn’t contain the data, but contains a way of locating the data. </li></ul><ul><li>Example: Array subscript </li></ul><ul><ul><ul><li>pointer : integer </li></ul></ul></ul><ul><ul><ul><li>data : array [1..100] of char; </li></ul></ul></ul><ul><ul><ul><li>… </li></ul></ul></ul><ul><ul><ul><li>pointer := 5; </li></ul></ul></ul><ul><ul><ul><li>writeln(array[pointer]); </li></ul></ul></ul>
  50. 50. Array Implementation of Linked Lists <ul><li>Issues </li></ul><ul><ul><li>Still have compile time limit on number of nodes, wasted space </li></ul></ul><ul><ul><li>Which nodes are “free” </li></ul></ul><ul><li>Benefits </li></ul><ul><ul><li>Simple base data type </li></ul></ul><ul><ul><li>Ok if max nodes know at compile time </li></ul></ul><ul><ul><li>Still have benefits of low cost insert, update, and organize </li></ul></ul>
  51. 51. Array Implementation (cont.) <ul><li>Free nodes </li></ul><ul><ul><li>Mark all free node - sequential search </li></ul></ul><ul><ul><li>Keep linked list of free nodes </li></ul></ul><ul><li>Book has special procedures to handle free nodes </li></ul><ul><li>Modify linked list procedures to handle free linked list </li></ul><ul><ul><li>Move InitializeSpace logic into Create Procedure </li></ul></ul><ul><ul><li>Pass InsertNode the data in place of the pointer P </li></ul></ul>
  52. 52. Pointers & Dynamic Memory <ul><li>Declaring a pointer (see page 874) </li></ul><ul><ul><li>Var </li></ul></ul><ul><ul><ul><li>intPtr : ^integer; </li></ul></ul></ul><ul><li>Allocate memory </li></ul><ul><ul><li>new(intPtr); </li></ul></ul><ul><li>Access and deallocate memory </li></ul><ul><ul><li>intPtr^ := 10; </li></ul></ul><ul><ul><li>writeln(intPtr^); </li></ul></ul><ul><ul><li>dispose(intPtr^); </li></ul></ul>
  53. 53. Pointers & Dynamic Memory <ul><li>Declaring a pointer (see page 874) </li></ul><ul><ul><li>Var </li></ul></ul><ul><ul><ul><li>intPtr : ^integer; </li></ul></ul></ul><ul><li>Allocate memory </li></ul><ul><ul><li>new(intPtr); </li></ul></ul><ul><li>Access and deallocate memory </li></ul><ul><ul><li>intPtr^ := 10; </li></ul></ul><ul><ul><li>writeln(intPtr^); </li></ul></ul><ul><ul><li>dispose(intPtr^); </li></ul></ul>
  54. 54. Pointers & Dynamic Memory <ul><li>Declaring a pointer (see page 874) </li></ul><ul><ul><li>Var </li></ul></ul><ul><ul><ul><li>intPtr : ^integer; </li></ul></ul></ul><ul><li>Allocate memory </li></ul><ul><ul><li>new(intPtr); </li></ul></ul><ul><li>Access and deallocate memory </li></ul><ul><ul><li>intPtr^ := 10; </li></ul></ul><ul><ul><li>writeln(intPtr^); </li></ul></ul><ul><ul><li>dispose(intPtr); </li></ul></ul>
  55. 55. Pointers & Dynamic Memory (cont.) <ul><li>NIL value - doesn’t point to anything </li></ul><ul><li>Memory allocated by new comes from heap </li></ul><ul><li>Must deallocate via dispose </li></ul><ul><li>Must keep track of all allocated memory - memory leaks </li></ul>
  56. 56. Pointer Implementation of Linked Lists <ul><li>See page 889 </li></ul>
  57. 57. Variations on Linked List <ul><li>Dummy Header - avoid test for empty list </li></ul><ul><li>Circularly Linked - no NIL </li></ul><ul><li>Doubly Linked List - don’t need previous node </li></ul>
  58. 58. Exercises <ul><li>Pg 872 # 16 </li></ul><ul><li>Pg 880 # 9-19 </li></ul>

×