TheHingineer

  • Data Structures & Algorithms


  • DSA Part-1

  • DSA Part-2

  • DSA Part-3

  • DSA Part-4

  • DSA Part-5

  • Linear Queue: Array Implementation and Operations

    Linear Queue ek linear data structure hai jo FIFO (First In First Out) principle follow karta hai. Iska matlab yeh hai ki jo element sabse pehle add hota hai, wahi sabse pehle remove hota hai. Real-life example: Railway ticket counter ya movie ticket booking queue.

    Two Pointers in Queue

    • FRONT Pointer: Always points to the first element (jahan se deletion hota hai).
    • REAR Pointer: Always points to the last inserted element (jahan naya insertion hota hai).
    Index:      0       1       2       3       4
              +----+  +----+  +----+  +----+  +----+
    Queue:    | 10 |  | 20 |  | 30 |  |    |  |    |
              +----+  +----+  +----+  +----+  +----+
                ^               ^
              FRONT            REAR
    (Dequeue from FRONT)    (Enqueue at REAR)
    

    Enqueue and Dequeue Algorithms

    ENQUEUE(Q, N, val)
    1. If REAR == N - 1 then:
           Print "Queue Overflow!"; Return
    2. If FRONT == -1 then: Set FRONT = 0
    3. Set REAR = REAR + 1
    4. Set Q[REAR] = val
    
    DEQUEUE(Q)
    1. If FRONT == -1 or FRONT > REAR then:
           Print "Queue Underflow!"; Return -1
    2. val = Q[FRONT]
    3. Set FRONT = FRONT + 1
    4. If FRONT > REAR then: Set FRONT = REAR = -1  [Reset empty queue]
    5. Return val

    Agli post me hum cover karenge Drawbacks of Linear Queue and Memory Wastage.

    Scroll to Top