Data Structures: Queues & Linked Lists
Objective: Understanding the concept of queues, circular queues, and linked lists.
This document will guide you through key concepts, operations, and common
implementations using arrays and linked lists.
1. What is a Queue?
- A queue is a linear data structure that follows the FIFO (First In, First Out) principle.
- Real-life Example: A line at a ticket counter.
- Operations:
- Enqueue: Insert an element into the queue.
- Dequeue: Remove an element from the queue.
- Peek/Front: Retrieve the front element without removing it.
- Size: Get the number of elements in the queue.
2. Types of Queues
- Simple Queue: Basic FIFO queue using an array or linked list.
- Circular Queue: A queue where the last element points back to the first element to form a
circle.
- Priority Queue: A queue where elements are dequeued based on priority.
3. Circular Queue Concept
- A queue in which the last element points to the first element.
- This avoids unused space in a linear queue and uses memory efficiently.
- Operations:
- Enqueue: Insert at the rear and if it reaches the end, it wraps around.
- Dequeue: Remove from the front and move the front pointer accordingly.
4. Circular Queue Using Array
- Array-based Queue with wraparound:
- **Front**: Points to the first element.
- **Rear**: Points to the last element.
- **Wraparound**: Rear wraps around to the beginning after reaching the end of the array.
- Operations:
- Enqueue: Insert at the rear.
- Dequeue: Remove from the front.
- Size: Calculated using the difference between front and rear.
5. Circular Queue Using Linked List
- A linked list-based circular queue where the last node points back to the first node.
- Operations:
- Enqueue: Insert a node after the rear.
- Dequeue: Remove the front node and adjust pointers.
- Traverse: Loop through nodes until the rear points to the front.
6. Linked List Basics
- A linked list is a linear data structure with nodes.
- Types:
- Singly Linked List: Each node points to the next node.
- Doubly Linked List: Each node points to both the next and the previous node.
- Circular Linked List: The last node points back to the first node.
- Operations:
- Insertion: Add a node at the beginning, end, or at a specific position.
- Deletion: Remove a node from the beginning, end, or a specific position.
- Traversal: Visit each node in the list.
7. Linked List Operations
- **Insertion**: Add a new node at a specific position (beginning, middle, or end).
- **Deletion**: Remove a node from a specific position.
- **Traversal**: Iterate through all nodes in the list.
8. Common Errors & Debugging
- Overflow: Trying to insert into a full queue.
- Underflow: Trying to dequeue from an empty queue.
- Memory Leaks: In linked list-based queues, always free nodes after use.
9. Conclusion & Exam Tips
- Summary: Focus on understanding the core operations of each data structure.
- Exam Tips: Practice writing code for insertion, deletion, and traversal for linked lists.
- Debugging: Pay attention to edge cases like when the queue is full or empty.