Storage and data organisation - EduqasChoosing a data structure

Arrays form the basis for how data is stored within a program. They must be declared with meaningful identifier names and appropriate data types that match the data. Without the correct data structures and data types, programs will not work.

Part ofComputer ScienceUnderstanding Computer Science

Choosing a data structure

In programming, structures are used to store and organise data. Selecting the appropriate data structure will help the code to run more efficiently and make it easier to write and understand.

Examples of data structures are:

  • an array - a set of data values of the same type, stored in a sequence within a computer program (also known as a list)
  • a 2D array – a matrix with rows and columns
  • a record - a data structure that allows for multiple variables of differing individual types to be held within it
  • a dictionary - a data structure using a unique key to look up values. For example, a key could be a word, a number or a combination of the two. A value can be any data, such as text, numbers and lists.

In order to select the most appropriate data structure, consider the following:

  • the type of data being stored (numbers, text or combinations of data types)
  • how the data will be accessed (by position, by name or in a grid)
  • if items need to be added or removed.

Examples of using data structures for different situations:

ExampleData structure
Storing the test results for a class of studentsAn array could be used, as there are a fixed number of students and each student’s score can be accessed by its position or index.
Representing a game of noughts and crossesA 2D array would work, as the board is a grid of rows and columns. It would be easy to check rows, columns or diagonals in this way.
Finding a capital city by the name of the countryA dictionary might be best, as each country links to one capital city and it can be looked up quickly using the country name.
Storing student details (name, age, tutor group)A record would help, as each student has different types of data. The user might want to keep all the information about each student together.
ExampleStoring the test results for a class of students
Data structureAn array could be used, as there are a fixed number of students and each student’s score can be accessed by its position or index.
ExampleRepresenting a game of noughts and crosses
Data structureA 2D array would work, as the board is a grid of rows and columns. It would be easy to check rows, columns or diagonals in this way.
ExampleFinding a capital city by the name of the country
Data structureA dictionary might be best, as each country links to one capital city and it can be looked up quickly using the country name.
ExampleStoring student details (name, age, tutor group)
Data structureA record would help, as each student has different types of data. The user might want to keep all the information about each student together.