Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🧱 Unit 1 – Data Structures और Algorithms  ·  Lesson 11

Queue और Deque

Queue क्या है?

Queue दोनों ends पर खुला linear data structure है: items rear पर जुड़ते हैं और front से निकलते हैं। नियम है FIFO – First In, First Out।

Queue →Asha0FRONTRavi1Neha2REAR
FRONT से dequeue, REAR पर enqueue

असल ज़िंदगी में queue

🎟️
Ticket counterलाइन में पहला व्यक्ति पहले serve होता है।
🖨️
PrinterDocuments भेजे गए क्रम में print होते हैं।
📞
Call centreCalls आने के क्रम में उठाई जाती हैं।
⌨️
Keyboard bufferKeys दबाए गए क्रम में process होती हैं।
रोज़मर्रा का FIFO

Queue operations

OperationमतलबPython
ENQUEUERear पर जोड़ेंq.append(x)
DEQUEUEFront से हटाएँq.pop(0)
PEEKFront item देखेंq[0]
isEmpty / sizeCheck / गिनतीlen(q)

List से queue

Python
queue = []
queue.append('Asha')      # enqueue at REAR
queue.append('Ravi')
queue.append('Neha')
print('Queue:', queue, '| front =', queue[0], '| rear =', queue[-1])
print('Served:', queue.pop(0))   # dequeue from FRONT
print('Queue now:', queue)
Queue: ['Asha', 'Ravi', 'Neha'] | front = Asha | rear = Neha Served: Asha Queue now: ['Ravi', 'Neha']

Functions के साथ queue

Python
def isEmpty(q):
    return len(q) == 0
def enqueue(q, item):
    q.append(item)
def dequeue(q):
    if isEmpty(q):
        return 'Underflow'
    return q.pop(0)
def peek(q):
    return 'Underflow' if isEmpty(q) else q[0]
def size(q):
    return len(q)

tickets = []
for t in [101, 102, 103]:
    enqueue(tickets, t)
print('Size:', size(tickets), '| next:', peek(tickets))
print(dequeue(tickets), dequeue(tickets), dequeue(tickets))
print('Again:', dequeue(tickets))
Size: 3 | next: 101 101 102 103 Again: Underflow

Printer queue simulation

Python
# Simulating a printer queue
jobs = []
for doc in ['notes.pdf', 'marks.xlsx', 'photo.png']:
    jobs.append(doc)
    print('Added', doc, '->', jobs)
while jobs:
    print('Printing', jobs.pop(0), '| waiting:', len(jobs))
Added notes.pdf -> ['notes.pdf'] Added marks.xlsx -> ['notes.pdf', 'marks.xlsx'] Added photo.png -> ['notes.pdf', 'marks.xlsx', 'photo.png'] Printing notes.pdf | waiting: 2 Printing marks.xlsx | waiting: 1 Printing photo.png | waiting: 0

Deque क्या है?

Deque (“deck” बोलते हैं, double-ended queue) में दोनों ends पर insertion और deletion हो सकता है। यह stack या queue दोनों की तरह काम कर सकता है।

Deque →10FRONT ⇄2030⇄ REAR
किसी भी end पर जोड़ें या हटाएँ

Python में deque

Python
dq = []
dq.append(20)          # insert at rear
dq.insert(0, 10)       # insert at front
dq.append(30)
print('Deque:', dq)
print('Remove front:', dq.pop(0))
print('Remove rear :', dq.pop())
print('Left:', dq)
Deque: [10, 20, 30] Remove front: 10 Remove rear : 30 Left: [20]

बड़े data के लिए collections.deque तेज़ है, क्योंकि list के front से हटाने पर हर element खिसकता है।

Python
from collections import deque
d = deque()
d.append('B'); d.appendleft('A'); d.append('C')
print(d)
print(d.popleft(), d.pop(), list(d))
deque(['A', 'B', 'C']) A C ['B']

Deque से palindrome

Python
def is_palindrome(word):
    dq = list(word.lower())
    while len(dq) > 1:
        if dq.pop(0) != dq.pop():   # compare front with rear
            return False
    return True
for w in ['Madam', 'Level', 'Python']:
    print(w, is_palindrome(w))
Madam True Level True Python False

Stack vs Queue vs Deque

StackQueueDeque
RuleLIFOFIFOBoth ends
Inserttop: append()rear: append()append() / insert(0,x)
Deletetop: pop()front: pop(0)pop() / pop(0)
ExampleUndoTicket counterBrowser history, palindrome

अक्सर पूछे जाने वाले प्रश्न (FAQ)

FIFO का मतलब क्या है?
First In, First Out: जो item queue में पहले आता है, वही पहले serve होता है।
Queue में items कहाँ जोड़े और हटाए जाते हैं?
REAR पर जोड़े जाते हैं (enqueue) और FRONT से हटाए जाते हैं (dequeue)।
Deque क्या है?
Double-ended queue जिसमें front और rear दोनों तरफ़ insertion और deletion हो सकता है।
← Class 12 Computer Science
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

अगला topicSorting: Bubble, Selection, Insertion आपकी प्रगति: 11 / 26 (42%) · Lesson 12 / 26 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें