Data Structures Flashcards
Static Data Structures
A static data structure is a coollection of data in memory that is fixed in size - memory locations are set aside for these data structures
Dynamic Data Structures
A dynamic data structure is a collection of data in memory that is flexible in size - it can grow and shrink while a program is running.
Memory is allocated from a heap - a collection of all the available memory locations in the main memory
Pointer
A variable that holds the memory address of another variable
eg. The variable at the top of a stack
Static Advantages and Disadvantages
Static Advantages:
- Easier to program
- No issues with adding/removing data items since size is fixed
Static Disadvantages:
- Memory that’s set aside might not be used; wasteful
Dynamic Advantages and Disadvantages
Advantages:
- More memory efficient; only uses the data it needs
Disadvantages:
- Can lead to memory leaks if memory isn’t deallocated properly
- Harder to program
Queues
A queue is an ADT that has a First In First Out principle - data items are added to the back of the queue and leave from the front
Front and Rear are indicated with pointers
Uses = Buffers; Print queues
Priority Queues
A type of queue where data items are assigned a priority value - items with the highest priority value are dequeued before lower priority items
If 2 items have the same priority, the FIFO order is followed
Uses = Priority print queues, A&E systems, the processor to prioritise apps in use over apps running in the background
Circular Queues
A type of queue where the Front and Rear pointers can move over both ends of the queue - more memory efficient
Front and Rear pointing to the same element = Empty queue
Rear = Front - 1 = Full queue
Queues: Insertion
1 - Check if the queue is full
2 - If the queue is full, report an error and stop
3 - Increment the Rear pointer and stop
4 - Insert new data into the location pointed to by the Rear pointer
For circular queues:
1 - Check if the queue is full
2 - if the queue is full, report an error and stop
3 - Compare the value of the rear pointer to the max size of the array minus 1.
If they are equal, then the rear pointer becomes 0.
Otherwise, add one to the rear pointer.
4 - Insert new data into the location pointed to by the Rear pointer
Queues: Deletion
1 - Check if the queue is empty
2 - If the queue is empty, report an error and stop
3 - Copy data item in the location pointed to by the Front pointer
4 - Increment the Front pointer and stop
For circular queues:
1 - Check if the queue is empty
2 - If the queue is empty, report an error and stop
3 - Copy data item in the location pointed to by the Front pointer
4 - Compare the value of the front pointer to the max size of the array minus one.
If they are equal then the front pointer becomes 0.
5 - Increment the Front pointer and stop
Stacks
An ADT where data can only be accessed from the top - data items are added and removed from the top
Top of the stack is indicated with a Top pointer
Uses a Last In First Out principle
Stack: Insertion/Pushing
1 - Check if the stack is full
2 - If it is, report an error and stop
3 - Increment the Top pointer
4 - Insert new data item into the location pointed to by the Top pointer and stop
Stack: Deletion/Popping
1 - Check if the stack is empty
2 - If the stack is empty report an error and stop
3 - Copy data in location pointed to by the Top pointer
4 - Decrement the stack pointer
Graphs
A diagram of connected nodes and edges - each edge joins 2 nodes
Usually represents a complex relationship eg a network of computers
Adjacency Matrix and List
Adjacency matrix = A table that records info about which edges are connected
- Best used when the graph is dense
Adjacency list = A list that shows which nodes are connected
- Best used when the graph is sparse