3.1 Data Structures Flashcards
Data structure
An organised means of storing related data.
7 examples of data structures
- Arrays
- Records
- Stacks
- Queues
- Trees
- Linked Lists
- Hash tables.
Array
A data structure that stores multiple data items of the same data type.
Element of an array (2)
- The index of each data value in the array.
- Accessed by its index - 0,1,2,3 etc.
3 advantages of an array.
- Allows direct accessing of elements.
- Can be used to implement other data structures e.g. stacks/queues.
- No need to declare large number of variables individually.
Disadvantage of an array.
Cannot be dynamically resized in most languages.
Record
A data structure to store multiple pieces of data with may have different data types.
Field
A single item of data within a record.
Stack
A FILO or LIFO data structure where data items can be pushed or popped.
FILO
First In Last Out
LIFO
Last In First Out
Pushed
When an item is added to the top of the stack.
Popped
When an item is taken from the top of the stack.
What is significant about the top of the stack?
It is the only position where a record can be added or removed.
How can a stack be implemented? (2)
- Can be implemented using an array and one integer variable.
- A variable, called “Top” can be used as the stack pointer.
Stack pointer
To contain the index of the most recent data item added.
How do you push records using arrays and variables? (2)
- Add new data in “Top” + 1
- Add 1 to “Top” variable
How do you pop records using arrays and variables? (3)
- Subtract 1 from the “Top” variable
- Data that has been popped can also been deleted, but does not need to be.
- The next time data is pushed the old data will be overwritten.
*Practical uses for stacks (3)
- ‘Undo’ function in a word processing package.
- ‘Back’ button on a browser to take user to the most recently visited page.
- Sub-program return address
*Practical uses for stacks (3)
- Recursion values
- Reverse polish calculations
- As part of a compiler, check that bracket are closed )}]> in the reverse order to that in which they were opened <[{(