Data Structures Final Revision Notes - pt 2
Data Structures Final Revision Notes - pt 2
🔄 Priority Queue
Definition: A priority queue is a queue where elements have priorities and are served based on their
priority rather than arrival time.
How it works:
Real-life example: Hospital emergency room - patients with critical conditions are treated before
those with minor issues, regardless of arrival time.
Implementation options:
1. Array-based: Use an array with special insertion logic to maintain priority order
Common operations:
Key features:
Elements can be added or removed from either the front or the rear
More flexible than standard queues and stacks
Common operations:
// Global variables
int deque[MAX]; // Array to store deque elements
int front = -1; // Index of front element
int rear = -1; // Index of rear element
Algorithm Explanation:
2. insertFront(item):
If deque is full, show error
3. insertRear(item):
If deque is full, show error
If deque is empty, set both front and rear to 0
4. deleteFront():
If deque is empty, show error
5. deleteRear():
If deque is empty, show error
4. Q: How would a deque implementation differ if using a linked list instead of an array?
A: In a linked list implementation, we wouldn't need to worry about circular behavior as there's no
fixed size. It would also eliminate overflow conditions, but might use more memory due to
pointers.
🔄 Multiple Queues
Definition: Implementing multiple queues in a single array.
Key strategies:
1. Fixed division: Divide the array into separate sections for each queue
2. Dynamic division: Allow queues to grow and shrink as needed
3. Separate pointers: Maintain separate front and rear pointers for each queue
Implementation approach:
Advantages:
4. Q: What is the difference between fixed and dynamic division in multiple queue
implementation?
A: Fixed division allocates a predetermined amount of array space to each queue that cannot
change, while dynamic division allows queues to grow and shrink based on their requirements.
Memory usage May waste memory if not fully used No wastage (allocate as needed)
No NULL pointers
Basic Operations:
Insertion: Add a node at the beginning, end, or specific position
Implementation:
c
#include <stdio.h>
#include <stdlib.h> // For malloc
// Search for the data to be deleted, keep track of the previous node
while (temp != NULL && temp->data != data) {
prev = temp;
temp = temp->next;
}
printf("NULL\n");
}
Algorithm Explanation:
1. Create Node:
Allocate memory for a new node
2. Insert at Beginning:
Create a new node
3. Insert at End:
Create a new node
Insert new node between the position node and its next node
5. Delete Node:
If list is empty, show error
If the node to delete is the head:
Update head to next node
Free the old head
Update the previous node's next pointer to skip the node to delete
Free the deleted node
6. Update Node:
Traverse to find the node with the old data
7. Display:
Traverse the list from head to tail
Implementation:
c
#include <stdio.h>
#include <stdlib.h> // For malloc
printf("NULL\n");
}
printf("NULL\n");
}
Algorithm Explanation:
1. Create Node:
Allocate memory for a new node
Set the data value
2. Insert at Beginning:
Create a new node
If list is empty, set head to new node
Otherwise:
Make new node's next point to current head
Make current head's prev point to new node
4. Delete Node:
If list is empty, show error
Otherwise:
Traverse to find the node to delete
Update previous node's next pointer to skip the node to delete
5. Display Forward:
Traverse the list from head to tail
6. Display Backward:
Traverse to the last node
Traverse backward using prev pointers
5. Q: What is the space complexity of a doubly linked list compared to a singly linked list?
A: Doubly linked list has a higher space complexity as it requires an extra pointer (prev) for each
node.
No NULL pointers
Can be singly circular (last node points to first) or doubly circular (last node's next points to first,
first node's prev points to last)
Useful for applications where you need to cycle through all elements repeatedly
Applications:
Round-robin scheduling