Presentation is loading. Please wait.

Presentation is loading. Please wait.

Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 12: Data Structures 1 Stewart Blakeway FML 213

Similar presentations


Presentation on theme: "Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 12: Data Structures 1 Stewart Blakeway FML 213"— Presentation transcript:

1 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 12: Data Structures 1 Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113

2 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What we have done already Seen what an algorithm is – a set of instructions that, if carried out, will lead to a successful conclusion Learned how to represent algorithms in – Structured English – Flow charts Used variables to remember 2

3 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What we have done already Applied the top down, stepwise refinement approach to creating algorithms Looked at problems more oriented towards being solved on a computer – stacks, queues, functions, procedures Seen how high level languages like Java are used to create programs with the aid of compilers The problem solving constructs in Java Nested for loops Used and manipulated strings of characters 3

4 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What we shall do in this part of the course – next three weeks Concept of a data structure Examples of data structures – records – arrays – sequences (lists, queues, stacks) 4

5 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Aims of the Presentation To look at the definition of various data structures To focus on arrays and records 5

6 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Why data structures? We use variables to remember data If we have lots of things to remember we need to organise them (“structure” them) in some way Examples – list of jobs – shopping list – a diary – record cards for a quiz league 6

7 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE An example of a data structure Set of record cards containing quiz league details 7

8 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Where is the structure in this? The information is organised in a logical way – One card for each team – Same 5 pieces of information for each team – Each piece of information made up of words and numbers – Each word made up of letters – Each number made up of digits 8

9 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE A question The simplest level of data structure in the English Football League is the player. What levels of data structure can you identify? – individual players – players forming a team – teams forming a division – divisions forming the Football League 9

10 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Important point There is a difference between a data structure and the data stored in it In the quiz team example the data structure is: – Box for the cards – Blank cards 10

11 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Data types Each part of the card holds a particular type of information 11 Black Arrows The Red LionWest Road, Bolton 0204 3521345 Monday type nametype address type telephone number type home night

12 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Types of Data structures What we have already seen – sequences (queues and stacks) – Scalar data structures (variables) What we will see – records – arrays (including tables) – sequences (lists) – trees 12

13 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Record A collection of different data types describing a person or thing – E.g. name, address, telephone number held in a record describing a person – E.g. name, description, unit price, quantity in stock in a record describing a product for sale A record is the only data structure that is a collection of different data types Normally requires a database to implement this structure We can also use multi-dimensional arrays with a little tweaking 13

14 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Record structure Each piece of information is called a field For each field we need to know: – its identifier or label – the type of information it can contain No two fields can have the same name The order of the fields is important 14

15 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Arrays and tables 15 e.g. array holding names of days of the week 0Sunday 1Monday 2Tuesday 3Wednesday 4Thursday 5Friday 6Saturday If the number of items to be collected in a data structure is fixed and the items are of the same type then an array may be used.

16 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Arrays and tables If each item is a record then the array is known as a table 16 0January31 1February28 2March31 3April30 4May31 5June30 6July31 7August31 8September30 9October31 10November30 11December31 Each record has two data types: Name of month Number of days in month (non-leap year)

17 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Power of arrays Information can be found quickly in arrays E.g. searching ten million records for owner of a car with a given registration number – Searching covered next week! Requires the size of the array to be fixed! 17

18 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Sequences The size of the sequence data structure can grow and shrink E.g. quiz league secretary’s box of record cards – Number of cards grows as teams join the league – Number shrinks as teams leave 18

19 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Sequences The size of the sequence data structure can grow and shrink E.g. list of airplanes in an air traffic control system’s airspace – As planes enter the airspace the list grows – As planes land or leave the list shrinks What does an empty list signify? 19

20 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Special sequences – restricted way of joining and leaving Stacks – LIFO Queues – FIFO 20

21 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Trees Sometimes more complex data structures needed 21

22 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Fixed size so use an array of 10 elements. 22

23 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE We need to store two items of information in each element so use a table of 10 records We could also use a multi-dimensional array as the data types are the same 23

24 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 24

25 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 25

26 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE End of Quick Overview Records Arrays Sequences Trees 26

27 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Let’s Recap An Array is a data structure that contains more than 2 of the same data type. Referenced by using an index A record is similar to an array, with the exception that is can use different data types A multidimensional array is similar to a record, except we must use the same data type

28 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Text fields: name, nationality, sex, ID number Date fields: date of birth Image field Could also have, for example, – audio field(voice print) – fingerprint field 28

29 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Table Example Different data types: Must be a record! NameNationalityDOBSexID Number Image String DateCharStringObject

30 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE A list of car manufactures Ford Volkswagen Nissan Rolls Royce

31 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Array Example Index range is [4] Each element is a name 31 Array name Array elements Array indices (plural of index) Same data type and Only one field. Must be an array!

32 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Identifying an array element Ford is Manufacturers[1] Volkswagen is Manufacturers[2] 32

33 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Describing an array completely The index for the array Manufacturers lies in the range 1..4 The elements are of type Name. Name stores String data type 33

34 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Quick question What is the value of Manufacturers[3] ? 34

35 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Another example What are the contents of the element in the array PlayOffTeams which has index 3 ? What is the value of PlayOffTeams[2] ? 35

36 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Another example Describe completely the structure of the array PlayOffTeams. 36

37 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Another example The index for the array PlayOffTeams lies in the range 1..4 The elements are of type Name Type name as a data type of String 37

38 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Summary A record is a collection of different data types An array is a fixed size collection of same data types – 1D – Multidimensional A sequence is a variable size collection of same data types To come Multidimensional arrays Sorting Arrays Implementing arrays / multidimensional arrays in Java 38


Download ppt "Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 12: Data Structures 1 Stewart Blakeway FML 213"

Similar presentations


Ads by Google