Data Structures Flashcards
What are the 4 basic data types
Integer
Real
String
Boolean
Define a data structure
A collection or group of related data items
Collects together the basic data types and allows data to be organised in a convenient and logical manner related to the program being developed
Where are data structures held
They are held in memory and therefore the data item is lost when the computer is turned off.
Usually not a problem as it is easy to write a procedure to copy a data structure into a file and then load it back in again when a program starts
Advantage of data structures
Data structures are held in memory it means the data can be accessed, searched, sorted etc. much more quickly than data held in a file
Can relate to specific problems
What is a 1d array
A 1d array is used to represent a list of data
Allows for the efficient processing of the data e.g. sorting, searching as it is a data structure
What is a 2d array
Used to represent a table of data
E.g. record sales figures for 4 sales people for each month of the year
What is a 3d array?
Used to represent tables of data
If a company wanted to record sales figures over a 10 year period a 3d array could be used
Advantage of array
Access elements in an array without any search being required
E.g. print sales(3,5 8) would immediately print the contents of that cell without needing to search through other elements
Disadvantages of array
Static data structures - the size is set and can’t be changed dynamically as the program runs
E.g. an array that might have 1000 names needs to be set to have 1000 elements, even if 800 are currently blank
If a new item of data needs to be inserted into an element in an array, all the elements after it need to be shifted up to make space for it
And vice versa if an element needs to be removed (items shifted down to fill the gap)
All elements in an array need to be of the same type
What does the record data structure allow
Allows items of data relating to something to be grouped together into a single record type
What is a stack
A last in first out data structure (LIFO)
E.g. like a stack of plates, last one that goes in comes out first
Limited access data structure, only the top item can be removed (popped)
Items are added (pushed) on to the top of the stack
A stack pointer always points at the last element added to the stack
A stack can be used as a recursive data structure
Underflow occurs when an attempt is made to pop an empty stack
Overflow occurs when an attempt is made to add to a full stack
Define popped
Removing an item from a stack
Display item indicated by stackpointer and then decrement from stackpointer
Define pushed
Item is added to the top of the stack
Increment the stack pointer and place the value in position indicated by stackpointer
Where is a stack (LIFO) Data structure used
Run time stack mechanism - an OS uses when running a program to keep track or where it is up to when making procedure calls
Back button in a Web browser
Undo feature
How is a stack implemented
Using an array and a variable known as a stack pointer which always points to the last element added to the stack
To add:
Increment stack pointer and place the value in position indicated by stackpointer
To remove:
Decrement stack pointer and place the value in the position indicated by the stack pointer
Why can a stack be considered a recursive data structure
A stack is either empty
Or has a top item and a pointer to a stack (e.g. a stack with an item c points to another stack with an item b which points to another stack and etc.)
What type of data structure is a queue
A first in first out data structure (FIFO)
E.g. queueing in a shop
A queue data structure is circular so if the end pointer goes beyond the last bit, it starts at the beginning again at e.g. 1
How are queues implemented
Using an array and two pointers to indicate the start and the end of the queue
End is the end of the queue where the new item will be added
How do you add data in a queue (FIFO)
Putting data into the end pointer position and incrementing(+1) the End Pointer
How do you remove data in a queue (FIFO)
Removing data involves displaying the data in a position indicated by start pointer and then incrementing(+1) the start pointer
Define linked list
A dynamic data structure, its size can be increased and decreased as required. An array is static and therefore its size is fixed
Items can be added into a linked list without having to move the existing items to make space
When being compared to an array:
Items can be removed from a linked list without having to shift the other items to fill in the gap left
How is a linked list represented
Using 2 arrays for the data and the pointer to the next item in the list, that item will point to the next and so on
There is also another linked list of free locations
Removing an item from a linked list
..
What type of data structures are trees and why is it useful
Non-linear
Useful for storing data that could he hierarchical e.g. car parts, organisation structure
What is a binary tree
A binary tree is a type of tree where all nodes can either have 0,1 or 2 children
I.e. a node with data and pointers left and right or either
What can binary trees be used for
To keep an ordered list for searching using the rule that anything ‘less than’ or before the data in the alphabet is to the left and everything ‘greater than’ or after the data to the right