1 / 38

Structured Problem Solving

Structured Problem Solving. Week 12: Data Structures 1 Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113. What we have done already. Seen what an algorithm is a set of instructions that, if carried out, will lead to a successful conclusion

keira
Télécharger la présentation

Structured Problem Solving

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. Structured Problem Solving Week 12: Data Structures 1 Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113

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

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

  4. 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)

  5. Aims of the Presentation • To look at the definition of various data structures • To focus on arrays and records

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

  7. An example of a data structure • Set of record cards containing quiz league details

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

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

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

  11. type name type address type telephone number type home night Data types • Each part of the card holds a particular type of information Black Arrows The Red Lion West Road, Bolton 0204 3521345 Monday

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

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

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

  15. Arrays and tables e.g. array holding names of days of the week • If the number of items to be collected in a data structure is fixed and the items are of the same typethen an array may be used.

  16. Arrays and tables • If each item is a record then the array is known as a table • Each record has two data types: • Name of month • Number of days in month (non-leap year)

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

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

  19. 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?

  20. Special sequences – restricted way of joining and leaving • Stacks • LIFO • Queues • FIFO

  21. Trees • Sometimes more complex data structures needed

  22. Fixed size so use an array of 10 elements.

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

  24. End of Quick Overview • Records • Arrays • Sequences • Trees

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

  26. What data structure would you use to store details of an individual? • 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

  27. Table Example Different data types: Must be a record!

  28. A list of car manufactures • Ford • Volkswagen • Nissan • Rolls Royce What data structure would you use to store details of an individual?

  29. Array elements Array name Array indices (plural of index) Array Example Same data type and Only one field. Must be an array! • Index range is [4] • Each element is a name

  30. Identifying an array element • Ford is Manufacturers[1] • Volkswagen is Manufacturers[2]

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

  32. Quick question • What is the value of Manufacturers[3] ?

  33. Another example • What are the contents of the element in the array PlayOffTeams which has index 3 ? • What is the value of PlayOffTeams[2] ?

  34. Another example • Describe completely the structure of the array PlayOffTeams.

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

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

More Related