Programming - Data structures Flashcards
what are static data structures
organisation or collection of data in memory that is fixed in size
eg arrays
features of static data structures
- fixed memory allocation
- predictable memory usage
- elements are stored in consecutive memory locations
what is an array
a list with a fixed size
what is a dynamic data structure
a collection of data in memory that has the ability to grow or shrink in size
eg lists
features of dynamic data structures
- variable memory allocation
- can adjust size based on the needs of the program
- unused memory is allocated or reallocated as needed
advantages of dynamic structures
- makes efficient use of memory because the data structure only uses as much memory as it needs
- Can grow as more data is added to the data structure
- no limit on number of items
that can be added - Resources only allocated as they are needed
disadvantages of dynamic structures
- runs the risk of overflow. can also have under flow if empty
- Additional memory needed for pointers;
- can lead to memory leak
- Can take longer to access an item directly
advantages of static structures
- memory allocation is fixed so there will be no issues with adding and removing items from the data structure
- easier to program as there isn’t a requirement to check on the size of the data structure at any point before accessing it
- predictable memory usage, easy to estimate and manage memory requirements
- Faster access time - elements are stored in contiguous memory, allowing for quick access
features of a stack
LIFO - last in first out structure,
features of a queue structure
FIFO - first in first out
- you can add items onto the back of the queue and remove items from the front
What does a queue need to be able to do?
- add items to the back - enqueue()
- remove items from the front - dequeue()
- test if the queue is empty or full isEmpty() or isFull()
what does a stack need to be able to do
- add items to the stack, from the back
- remove items from the stack from the back
- look at the top item
- test if stack is empty or full
Describe the steps involved in adding a record to a hash table.
-Hash algorithm applied;
to key value ;
- result is location in table where the record should be stored (key % number of slots)
- if location is not empty;
then use next free location by incrementing by one
Describe the process that should be followed to add an item to a circular queue
implemented as a static data structure using an array.
1. Check the queue is not already full;
2. Compare the value of the (rear) pointer with the maximum size of the array;
3. If equal then (rear) pointer becomes zero;
4. Otherwise, add one to the (rear) pointer;
5. Insert new item in position indicated by (rear) pointer;
what are abstract data types
a logical description of how the data is viewed and the operations that can be performed on it
where are queues used
- output waiting to be printed/print spooling
- characters typed at a keyboard are held in a queue in a keyboard buffer
- simulation programs
what is a stack?
how does a stack operate?
what are some of the common uses of a stack?
- a stack is a linear data structure which follows the Last in first out principle
- elements are added (pushed) and removed (pop) from the top
- used in text editing, call management
uses of static data structures
- arrays, static stacks and queues
- used when max size of the data structure is known in advance
- when memory usage needs to be predictable and consistent
eg implementing simple buffers
uses of dynamic data structures
- linked lists, dynamic stacks and queues, trees, graphs
- used in situations where the size of the data structure may change during runtime
- where flexibility and efficient memory usage are required
- eg, implementing dta structures like trees and graphs
How do you add an item to a linear queue
check the queue is not full.
- if full, report an overflow error and stop
- else, increment the rear pointer
- insert new data item into the location pointed to by the rear pointer
How do you remove an item from a linear queue?
- Check queue is not empty
- if its empty report an underflow error and stop
- copy data item in the front pointer
- increment front pointer
How do you test for an empty linear queue
How do you test for a full linear queue
- Check if the front pointer is greater than the rear pointer or if the queue size is 0
- Condition: Check if the rear pointer is at the last index of the array (size - 1)
disadvantages of static data structures
- cannot grow or shrink during runtime, which could lead to wasted memory or insufficient space
- not suitable for applications where data size can vary significantly
How do you perform a push(insertion) operation on a stack
- Check that stack is not full
- if the stack is full, report error and stop
- increment the stack pointer
- insert new data item into location pointed to by the new pointer