Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS341 PASCAL II - Data Structures1 PASCAL II - Data Structures Philip Fees CS341.

Similar presentations


Presentation on theme: "CS341 PASCAL II - Data Structures1 PASCAL II - Data Structures Philip Fees CS341."— Presentation transcript:

1 CS341 PASCAL II - Data Structures1 PASCAL II - Data Structures Philip Fees CS341

2 CS341 PASCAL II - Data Structures2 Introduction l What will be studied? –Data Types –Arrays –Records –Abstract Data –Pointers –Linked lists, Stacks, Queues l What is a data structure?

3 CS341 PASCAL II - Data Structures3 Workshop 1 l File I/O Concepts l Pascal File I/O l User Defined Types

4 CS341 PASCAL II - Data Structures4 File I/O Concepts l Generally termed Device I/O –Could be hard disk, CD-ROM, tape, etc. –Could be terminal, socket (IPC/Internet), etc. l Delimited files (whitespace, eol, eof) vs. non delimited l Encoded (ASCII, EBCDIC) vs. binary l Open and Close l Read or write via file/device handle (symbolic name) l Seek vs. non-seek devices l Sequential vs. Indexed

5 CS341 PASCAL II - Data Structures5 Pascal File I/O - Handles l Identify handle (symbolic name) Program myProgram (input, output, fileHandle); … VAR fileHandle : text; l Associate file to handle –“create procedure file prior to compiling and running the program” –TP: assign(fileHandle,’myTextFile.txt’);

6 CS341 PASCAL II - Data Structures6 Pascal File I/O - Open & Seek l Open the file reset(fileHandle); l rewind is part of “seeking” l delimiter tests WHILE not eol DO WHILE not eol(fileHandle) DO WHILE not eof DO WHILE not eof(fileHandle) DO

7 CS341 PASCAL II - Data Structures7 Pascal File I/O - Read & Write l Read from file via handle readln(fileHandle, variable1, variable2, …); l Write to file via handle writeln(fileHandle, ‘this is a test’, variable1, …); l Ease old file contents rewrite(fileHandle); l See example pg. 444

8 CS341 PASCAL II - Data Structures8 Exercises l Pg. 448 25 - 30

9 CS341 PASCAL II - Data Structures9 User Defined Types l Characteristic of a data type (int, char, boolean) –set of value –fixed amount of memory l Ordinal data type have pred() and succ() values l User defined a.k.a. enumerated data type

10 CS341 PASCAL II - Data Structures10 Enumerated Data Type l Syntax Type Weekday = (Mon, Tues, Wed, Thur, Fri); l Usage VAR Day : Weekday; … Day := Mon; if (Day = Mon) then

11 CS341 PASCAL II - Data Structures11 Subranges l Define one type as subset of another type l Examples TYPE USYears = 1776..1998; Days = (Sun, Mon, Tues, Wed, Thur, Fri, Sat); Weekdays = Mon..Fri; l Operations: succ() and pred()

12 CS341 PASCAL II - Data Structures12 Exercises l Pg. 454-455 # 2, 15 l Pg. 461 #22-26 l Pg. 465 #8, 20, 21, 22

13 CS341 PASCAL II - Data Structures13 Workshop 2 l Single Dimensional Arrays l Selection Sort l Arrays and Subroutines l Searching Arrays

14 CS341 PASCAL II - Data Structures14 Arrays l Collection of data elements l Data elements are the same type l Contiguous in memory l Access individuals by subscript l Array size determined by: element size * number of elements

15 CS341 PASCAL II - Data Structures15 Pascal Arrays l name : ARRAY [ index type ] OF type; l Example 1: VAR List : ARRAY [1..5] OF integer; l Example 2: TYPE Numbers = ARRAY [1..5] OF integer; VAR List : Numbers;

16 CS341 PASCAL II - Data Structures16 Pascal Arrays (cont.) l 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;

17 CS341 PASCAL II - Data Structures17 Use of Constants l Good Pratice CONST ClassSize = 35; Type TestScores = 0..100; VAR Score : ARRAY [1..ClassSize] of TestScores;

18 CS341 PASCAL II - Data Structures18 Arrays and Loops l “Looping” over the array l Example: FOR J := 1 to ClassSize DO BEGIN write(‘next: ‘); readln(Score[J]); writeln(‘value = ‘, Score[J]); END

19 CS341 PASCAL II - Data Structures19 Review l Example 10.7 pg. 491 l Example 10.9 pg. 493 l Example 10.10 pg. 494

20 CS341 PASCAL II - Data Structures20 Selection Sort l Sorting a list of values l 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 l What is the best and worst case?

21 CS341 PASCAL II - Data Structures21 Arrays and Subroutines l See GetData example on pg. 508 l 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

22 CS341 PASCAL II - Data Structures22 Search Algorithms l Sequential Search (pg. 526) –search entire array until value located or “hit” the end of the array –Average of N iterations of loop l Binary Search (pg. 528) –Assumes sorted array –start in middle; look in upper or lower half –Average of log N iterations of loop

23 CS341 PASCAL II - Data Structures23 Analysis l Overhead of inserting new value in sorted array l What should maximum size of the array be? l When should an array be used?

24 CS341 PASCAL II - Data Structures24 Exercises l Arrays: pp. 497-500 # 1-5, 10, 20 l Sorts: pg. 507 # 2, 3, 6 l Subroutines: pg. 516 # 16-19 l Searching: pg. #533 5, 15

25 CS341 PASCAL II - Data Structures25 Workshop 3 l Multi-dimensional arrays

26 CS341 PASCAL II - Data Structures26 Array in Memory l Array stored in contiguous memory l Location is calculated: –Starting address + (row index * # of columns) + column index l Row Major vs. Column Major

27 CS341 PASCAL II - Data Structures27 Pascal Syntax l Syntax : ARRAY [, ] OF

28 CS341 PASCAL II - Data Structures28 Two Dimensional Example l Example 1: VAR Table : ARRAY [ 1..3, 1.. 4 ] OF integer; l Example 2: TYPE Maxtrix = [ 1.. 3, 1.. 4] OF integer; VAR Table : Matrix;

29 CS341 PASCAL II - Data Structures29 Iteration l Example: FOR row := 1 to 3 DO FOR column := 1 to 4 DO writeln(‘Table[‘,row,’, ‘, column,’] = ‘, Table[row][column]);

30 CS341 PASCAL II - Data Structures30 Higher Dimensional Arrays l Syntax : ARRAY [ A1.. B1, A2.. B2, …, An.. Bn] OF l Example1: cube : ARRAY [ 1..3, 1..4, 1..5] OF integer; l Example 2: cube : ARRAY [ 1..3 ] OF ARRAY [ 1..4, 1..5 ] OF integer;

31 CS341 PASCAL II - Data Structures31 Exercises l pg. 567 # 4, 9-12, 13-15

32 CS341 PASCAL II - Data Structures32 Workshop 4 l Records l Variants l Binary Files

33 CS341 PASCAL II - Data Structures33 Workshop 5 l Sets

34 CS341 PASCAL II - Data Structures34 Defining and Declaring l Syntax TYPE = SET OF ; VAR : ; l Example TYPE Digits = SET OF 0..9; VAR numbers : Digits;

35 CS341 PASCAL II - Data Structures35 Different than Subtypes l Assignment numbers := [ 0, 2, 4, 6, 8 ]; l Set is undefined until assignment l Assigned values must be in base type - elements of the set l Universal sets contain all values l Subsets - one set contains all members of another set l See set definitions on top of page 735.

36 CS341 PASCAL II - Data Structures36 Set Operations l Union: A + B l Intersection: A * B l Difference: A - B

37 CS341 PASCAL II - Data Structures37 Relational Operators l equal: A = B sets A and B are identical l not equal: A <> B set A and B are not identical l subset: A <= B A is a subset of B l superset A >= B A is a superset of B (A<=B)

38 CS341 PASCAL II - Data Structures38 Membership l Syntax IN l Example 2 IN numbers l Boolean value: is 2 in the numbers set?

39 CS341 PASCAL II - Data Structures39 Membership Example l Useful for “edit checks” l Example IF response IN [‘Y’, ‘y’] THEN (continue action here) ELSE (alternative action here) l Review example on page 744.

40 CS341 PASCAL II - Data Structures40 Exercises l Page 739 # 9-15, 26-30 l Page 743 # 2, 7-20, 27 l Programming Problem Page 759 # 1, 3

41 CS341 PASCAL II - Data Structures41 Workshop 6 l Model Builder l Abstract Data Type (ADT) l String ADT l Linked List ADT

42 CS341 PASCAL II - Data Structures42 Model Builder l Model, design, or abstraction - analysis without being concern for details l Examples –World Wide Web –Windows –Virus –UNIX: parent process, child process, kill, fork, etc.

43 CS341 PASCAL II - Data Structures43 Abstract Data Type l collection of data (objects) l shared properties l shared operations l Examples: Array, Integers,

44 CS341 PASCAL II - Data Structures44 ADT Example - String l definition - finite sequence of characters l terminated with null character l access individual elements (substring) l Operations: create, read, write, assign, length l reconsider operations if string is defined by length not null terminator.

45 CS341 PASCAL II - Data Structures45 ADT Example - Linked List l Definition - list of data items associated by a link to one or more nodes. l Typically point to next node (single linked) or previous node (double linked) l Head node is first node in list l To “walk” the list, must start at head and proceed sequentially. l Task: Define operations, define interfaces, implement

46 CS341 PASCAL II - Data Structures46 Workshop 7 l Linked Lists l Array Implementation of Linked Lists l Pointers l Pointer Implementation of Linked Lists

47 CS341 PASCAL II - Data Structures47 Why Linked Lists l Arrays –Size fixed at Compile Time, inefficient space utilization –Inserting, deleting, organizing are costly. Why? l Requirement –Size determined at Run Time –Minimal cost to insert, delete, and organize data l Examples –Sorting, data with undetermined number of items

48 CS341 PASCAL II - Data Structures48 Linked Lists l Diagram data structure (See page 872) l Define operations (See page 881) l Define implementation base data structures (See page 873) l Diagram operation’s effect on sample data structure (See page 887)

49 CS341 PASCAL II - Data Structures49 Introduction to Pointers l A pointer doesn’t contain the data, but contains a way of locating the data. l Example: Array subscript pointer : integer data : array [1..100] of char; … pointer := 5; writeln(array[pointer]);

50 CS341 PASCAL II - Data Structures50 Array Implementation of Linked Lists l Issues –Still have compile time limit on number of nodes, wasted space –Which nodes are “free” l Benefits –Simple base data type –Ok if max nodes know at compile time –Still have benefits of low cost insert, update, and organize

51 CS341 PASCAL II - Data Structures51 Array Implementation (cont.) l Free nodes –Mark all free node - sequential search –Keep linked list of free nodes l Book has special procedures to handle free nodes l 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

52 CS341 PASCAL II - Data Structures52 Pointers & Dynamic Memory l Declaring a pointer (see page 874) Var intPtr : ^integer; l Allocate memory new(intPtr); l Access and deallocate memory intPtr^ := 10; writeln(intPtr^); dispose(intPtr^);

53 CS341 PASCAL II - Data Structures53 Pointers & Dynamic Memory l Declaring a pointer (see page 874) Var intPtr : ^integer; l Allocate memory new(intPtr); l Access and deallocate memory intPtr^ := 10; writeln(intPtr^); dispose(intPtr^);

54 CS341 PASCAL II - Data Structures54 Pointers & Dynamic Memory l Declaring a pointer (see page 874) Var intPtr : ^integer; l Allocate memory new(intPtr); l Access and deallocate memory intPtr^ := 10; writeln(intPtr^); dispose(intPtr);

55 CS341 PASCAL II - Data Structures55 Pointers & Dynamic Memory (cont.) l NIL value - doesn’t point to anything l Memory allocated by new comes from heap l Must deallocate via dispose l Must keep track of all allocated memory - memory leaks

56 CS341 PASCAL II - Data Structures56 Pointer Implementation of Linked Lists l See page 889

57 CS341 PASCAL II - Data Structures57 Variations on Linked List l Dummy Header - avoid test for empty list l Circularly Linked - no NIL l Doubly Linked List - don’t need previous node

58 CS341 PASCAL II - Data Structures58 Exercises l Pg 872 # 16 l Pg 880 # 9-19


Download ppt "CS341 PASCAL II - Data Structures1 PASCAL II - Data Structures Philip Fees CS341."

Similar presentations


Ads by Google