ADS Notes
ADS Notes
2. Circular Queue
• A variation of the simple queue where the last position is connected back
to the first position (circular).
• Efficient use of memory as spaces are reused.
• Prevents the "Queue Overflow" problem seen in simple queues.
Example:
Enqueue: 10 → 20 → 30 → 40
Dequeue: 10 is removed
Now, the space of 10 can be reused.
• Instead of shifting elements, the rear wraps around when it reaches the
end.
4. Priority Queue
• Each element is assigned a priority.
• Elements are dequeued based on priority, not just FIFO.
• Two types:
1. Ascending Priority Queue – Elements with lower priority values
are dequeued first.
2. Descending Priority Queue – Elements with higher priority
values are dequeued first.
Example:
Element Priority
Task A 3
Task B 1
Task C 2
Applications of Queue
• CPU Scheduling (Round Robin uses Circular Queue)
• Printers & Task Scheduling (FIFO)
• Call Centre Systems (Priority Queue)
• Deque in Palindrome Checking
#define MAX 5
int stack[MAX], top = -1;
// Push operation
void push(int value) {
if (top == MAX - 1)
printf("Stack Overflow! \n");
else
stack[++top] = value;
}
// Pop operation
int pop() {
if (top == -1)
printf ("Stack Underflow! \n");
else
return stack[top--];
return -1;
}
return 0;
}
Output:
Stack: 30 20 10
Popped: 30
Stack: 20 10
Comparison Table
Right Threaded Left Threaded Tree
Feature
Tree
Pointer Used Right NULL pointers Left NULL pointers
// Push function
void push(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = top;
top = newNode;
printf("%d pushed\n", value);
}
// Pop function
void pop() {
if (top == NULL) {
printf("Stack is empty\n");
return;
}
struct Node* temp = top;
printf("%d popped\n", top->data);
top = top->next;
free(temp);
}
// Display function
void display() {
struct Node* temp = top;
while (temp) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
// Main function
int main() {
push(10);
push(20);
push(30);
display();
pop();
display();
return 0;
}
Output:
10 pushed
20 pushed
30 pushed
30 -> 20 -> 10 -> NULL
30 popped
20 -> 10 -> NULL