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