DATA STRUCTURES/Student Engineering Notebook
Page 02·notes
ARRAYS, STACKS, QUEUES & LINKED LISTS—~32 MIN READ

Queues, Circular Buffers & Linked List Pointer Mechanics

“Circular queue boundary arithmetic, Deques, queue via two stacks, Floyd's cycle detection, and list reversals.”

1.Circular Queues & Amortized Two-Stack Queue

A Queue enforces First-In First-Out (FIFO) access.

Circular Queue Mechanics:

To prevent false overflow where unused memory accumulates at the front of an array, the end of the array wraps around to index 0 using modulo arithmetic.

- Enqueue: rear = (rear + 1) % N; Q[rear] = item;

- Dequeue: front = (front + 1) % N; return Q[front];

- Empty: front == rear.

- Full: (rear + 1) % N == front (sacrifices 1 physical slot to disambiguate full from empty).

Queue Implemented with Two Stacks:

- Push to S1 for Enqueue(x): O(1).

- Dequeue(): If S2 is non-empty, pop S2. If S2 is empty, transfer all elements from S1 to S2 (inverting order) and pop S2.

Each element is pushed to S1 once, popped from S1 once, pushed to S2 once, and popped from S2 once. Total 4 operations over its lifetime: Amortized O(1) time.

2.Linked Lists & Floyd's Cycle Detection Theorem

A Linked List stores elements in dynamically allocated nodes containing data and pointers.

Floyd's Tortoise and Hare Cycle Finding Theorem:

Let a singly linked list have a linear non-cyclic prefix of length k, followed by a cycle of length C.

- Slow pointer S advances 1 node per tick.

- Fast pointer F advances 2 nodes per tick.

When S enters the cycle at time k, F is at position k mod C inside the cycle. The relative distance between F and S increases by 1 each step. Thus, F catches S in at most C steps.

Finding the Cycle Entry:

When they meet at distance d from the start of the cycle: the distance from head to cycle start is k. The distance from the meeting point to cycle start is also k (mod C). Moving S to head and advancing both 1 step per tick guarantees they meet exactly at the cycle entrance!

WORKED EXAM ANALYSIS · STEP-BY-STEP DERIVATIONOriginal Practice Problem · Modeled on GATE Pattern (2 Marks)

3. Worked Problem: Circular Queue Full/Empty State Sequence

⚠️ Pitfall Warning:

Remember that front points to the position before the first element, while rear points directly to the last inserted element.

Problem: A circular queue of array size N = 6 is initially empty with front = 0 and rear = 0. Perform the following sequence of operations: Enqueue(10), Enqueue(20), Enqueue(30), Dequeue(), Enqueue(40), Enqueue(50), Enqueue(60), Dequeue(). What are the final values of front, rear, and the number of stored elements?

Step 1: Initial state: front = 0, rear = 0 (empty).

Step 2: Trace operations:

- Enqueue(10): rear = (0 + 1) % 6 = 1. Q[1] = 10.

- Enqueue(20): rear = (1 + 1) % 6 = 2. Q[2] = 20.

- Enqueue(30): rear = (2 + 1) % 6 = 3. Q[3] = 30.

- Dequeue(): front = (0 + 1) % 6 = 1. Returns Q[1] = 10.

- Enqueue(40): rear = (3 + 1) % 6 = 4. Q[4] = 40.

- Enqueue(50): rear = (4 + 1) % 6 = 5. Q[5] = 50.

- Enqueue(60): rear = (5 + 1) % 6 = 0. Q[0] = 60.

- Dequeue(): front = (1 + 1) % 6 = 2. Returns Q[2] = 20.

Step 3: Evaluate final state:

Final front = 2, final rear = 0.

Stored elements are at indices: 3 (val 30), 4 (val 40), 5 (val 50), 0 (val 60).

Count = (rear - front + N) % N = (0 - 2 + 6) % 6 = 4 elements.

Final Answer: front = 2, rear = 0, Stored count = 4.

SOURCE TRANSPARENCY & CITATIONS

Primary Academic References & Exam Sources

Every concept in Notes From a B.Tech Brain is synthesized from authoritative academic texts, seminal peer-reviewed research, verified exam archives, and renowned university lectures.

Curated University Lecture Series
Data Structures and Algorithm Analysis in C (2nd Edition) ·
LECTURE SERIES
RECOMMENDED YOUTUBE EDUCATORS & FULL-COURSE PLAYLISTS

Curated for clarity, rigorous numerical solving, and semester excellence:

Abdul Bari · Data Structures & Algorithms↗Instructor: Abdul Bari · Covers: Trees, graphs, hashing, sorting — with visual algorithm animations

Visual thinkers who want to see each algorithm step animated clearly.

Open YouTube↗
Gate Smashers · Data Structures Full Course↗Instructor: Varun Singla · Covers: Arrays, linked lists, trees, BST, AVL, heaps, hashing — GATE numericals

GATE-targeted coverage with step-by-step GATE PYQ walkthroughs.

Open YouTube↗