Queues · الطوابير (Queues)
A queue is FIFO
- A queue is First In, First Out (FIFO).
- The first item you add is the first one to leave.
- Think of a line of people: the person at the front is served first.
الطابور FIFO
- الطابور هو أول يدخل، أول يخرج (FIFO).
- أول عنصر تضيفه هو الأول الذي يغادر.
- فكر في طابور من الناس: الشخص في الأمام يتم خدمته أولاً.
enqueue and dequeue with a list
- We can build a queue from a Python list.
- enqueue =
queue.append(x)— add to the back. - dequeue =
queue.pop(0)— remove and return the front.
enqueue و dequeue مع قائمة
- يمكننا بناء طابور من قائمة بايثون.
- enqueue =
queue.append(x)— أضف إلى الخلف. - dequeue =
queue.pop(0)— أزل وأعد القيمة الأمامية.
queue = []
queue.append("first")
queue.append("second")
print(queue.pop(0))
print(queue)
front and empty
- Look at the front without removing it:
queue[0]. - A queue is empty when
len(queue) == 0. - Check it is not empty before you dequeue.
front و empty
- انظر إلى الأمام دون إزالته:
queue[0]. - يكون الطابور فارغًا عندما
len(queue) == 0. - تحقق من أنه ليس فارغًا قبل dequeue.
queue = [10, 20, 30]
print(queue[0]) # the front
print(len(queue) == 0) # is it empty?
A note on speed
pop(0)has to shift every other item forward by one place.- For a big queue that is slow, so it uses more time.
- Real systems use a circular buffer with front and rear pointers instead.
ملاحظة حول السرعة
- يجب على
pop(0)تحريك كل عنصر آخر للأمام بمكان واحد. - بالنسبة لطابور كبير، يكون بطيئًا، لذا فإنه يستهلك المزيد من الوقت.
- الأنظمة الحقيقية تستخدم مُخبّر دائري مع مؤشرات أمامية وخلفية بدلاً من ذلك.
In Cambridge pseudocode
- The exam builds a queue from an array with a
frontand arearpointer.
في الرمز الوهمي لكامبريدج
- يبني الامتحان طابورًا من مصفوفة مع مؤشر
frontومؤشرrear.
DECLARE queue : ARRAY[1:10] OF INTEGER
DECLARE front, rear : INTEGER
front ← 1
rear ← 0 // empty
// enqueue value
rear ← rear + 1
queue[rear] ← value
// dequeue into value
value ← queue[front]
front ← front + 1
Common mistakes
- A queue is first-in, first-out: join the back, leave from the front.
- Check it is not empty before you dequeue.
أخطاء شائعة
- الطابور first-in, first-out: join the back, leave from the front.
- تحقق من أنه ليس فارغًا قبل dequeue.
Now you try
- Use a list as your queue (
appendto enqueue,pop(0)to dequeue). - Press Check answer to test your code.
الآن جرب بنفسك
- استخدم قائمة كطابور (
appendلـ enqueue،pop(0)لـ dequeue). - اضغط على تحقق من الإجابة لاختبار الكود الخاص بك.
A queue is FIFO · الطابور يعمل بنظام FIFO
A queue adds at the back and removes at the front — first in, first out. · يضيف الطابور في الخلف ويزيل من الأمام — أول يدخل، أول يخرج.
Start with an empty queue. Enqueue "a", then "b", then "c". Then dequeue once, storing the removed value in first. (first should be "a" and the queue should be ["b", "c"].) · ابدأ بطابور فارغ. أضف "a"، ثم "b"، ثم "c". ثم أخرج مرة واحدة، واحفظ القيمة المحذوفة في first. (يجب أن يكون first هو "a" والطابور يجب أن يكون ["b", "c"].)
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Enqueue 1, 2, 3 onto a queue, then dequeue them all into result. Unlike a stack, a queue keeps the same order. (result should be [1, 2, 3].) · أضف 1، 2، 3 إلى طابور، ثم أخرجهم جميعاً إلى result. على عكس المكدس، يحافظ الطابور على نفس الترتيب. (يجب أن يكون result هو [1, 2, 3].)
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Write serve(queue) that dequeues the front item: remove it from the list and return it. The caller's list should get shorter. · اكتب serve(queue) التي تخرج العنصر الأمامي: أزلها من القائمة و ارجع إياها. يجب أن تصبح قائمة المُدْعى أقصر.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Write front(queue) that returns the front item without removing it, so the queue is unchanged. If the queue is empty, return None. · اكتب front(queue) التي ترجع العنصر الأمامي دون إزالته، بحيث يبقى الطابور كما هو. إذا كان الطابور فارغاً، ارجع None.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.