1.4.2 Data Structures Flashcards
(44 cards)
What’s an array?
An ordered, static set of elements that can only store 1 data type.
What is a 1D array?
A linear array.
- Create a 1D array called fruits with 5 elements in psuedocode.
- Write code to access an element in the array.
- Write code to change an element in the array.
- Write code to print the length of the array.
- Write code to iterate over the array
- array fruits[5]
fruits[0] = “banana”
fruits[1] = “apple”
fruits[2] = “orange”
fruits[3] = “blueberry”
fruits[4] = “cherry” - print(fruits[0])
print(fruits[2]) - fruits[1] = pear
- length = len(fruits)
print(length) - for element in fruits
print(element)
What’s a record?
A group of related fields which is a single entity.
- In psuedocode define a record type called “personDataType” with fields: “ID”, “firstName” and “surname”.
- Declare an instance of the Person record.
- Assign values to that variable’s fields.
- Access the fields of the Person record.
- personDataType = record
integer ID
string firstName
string surname
endRecord - DECLARE person1:personDataType
- person1.ID = 101
person1.firstName = “John”
person1.surname = “Smith” - OUTPUT person1.firstName
How can a 2D array be visualised?
As a table.
- Write psuedocode for a 2D array named “board” with rows 8, and columns 9.
- Write code to assign “rook” to the element in the first row, first column.
- Array board[8,9]
- board[0,0]=“rook”
How can a 3D array be visualised?
A multi-page spreadsheet.
What is a list?
A data structure that consists of a number of items where the items can occur more than once. And can store elements of more than one data type and each element is stored non-contiguously.
Give 1 difference of lists and arrays
Lists can contain elements of more than one data type, arrays can only contain elements of one data type.
- Create a list named “pets” in pseudocode with elements “dog”, “cat” and “fish”.
- Write a for loop that runs through the whole list, printing each element individually.
- list pets = [“dog”, “cat”, “rabbit”, “fish”]
- for pet in pets:
print pet
What do these list operations do:
1. isEmpty()
2. append(value)
3. remove(value)
4. length()
5. index(value)
6. insert(position, value)
7. pop()
- isEmpty() checks if the list is empty.
- append(value) adds a new value to the end of the list.
- remove(value) removes the value the first time it appears in the list.
- length() returns the length of the list.
- index(value) returns the position of the list.
- insert(position, value) inserts a value at a given position.
- pop() returns and removes the last value.
What is a tuple?
An immutable data structure that stores an ordered set of values of any type.
What is a linked list?
A dynamic data structure that is used to hold an ordered sequence of items.
The items which form the sequence do not have to be in contiguous data locations.
What does a node contain?
A data field and a point field.
What’s a data field contain?
The value of the actual data which is part of the list.
What’s a pointer field contain?
The address of the next item in the list.
Give 1 advantage of linked lists
Values can easily be added or removed by editing the pointers.
Give 2 disadvantages of linked lists compared to arrays
- Linked lists store pointers which means that more memory is required compared to an array.
- Items in a linked list are stored in a sequence, meaning they can only be traversed within that order so an item cannot be directly accessed as is possible in an array.
What’s a stack?
A last in first out (LIFO) data structure where items can only be added to or removed from the top of the stack.
Give 2 examples of stacks in a computer
- Undo button
- Back page button
What do these stack operations do:
1. isEmpty()
2. push(value)
3. peek()
4. pop()
5. size()
6. isFull()
- isEmpty() checks if the stack is empty by checking the value of the top pointer.
- push(value) adds a new value to the top of the stack.
- peek() returns the top value of the stack.
- pop() removes and returns the top value of the stack.
- size() returns the size of the stack.
- isFull() checks if the stack is full and returns the boolean value.
What is a queue?
A First in First out (FIFO) data structure where items are added to the end of the queue and removed from the front, that holds an ordered sequence of items.
What are 3 types of queues?
- Linear Queue
- Circular Queue
- Priority Queue