You are on page 1of 11

BY NsM [DATA STRUCTURES IN ARABIC]

Chapter five
The Queue
What is Queue data structure?‫ما هو هيكل بيانات الطابور‬

Queues are used to prioritize operating system processes and to


simulate events in the real world, such as teller lines at banks.
‫م ٔيغاكاة‬ٛ‫اث َظاو انخشغ‬ٛ‫اث ػًه‬ٕٚ‫ذ أٔن‬ٚ‫خى اسخخذاو انغابٕس نخغذ‬ٚ
.‫ انبُٕك‬ٙ‫ يزم خغٕط انصشاف ف‬، ٙ‫ق‬ٛ‫ انؼانى انغق‬ٙ‫األعذاد ف‬

A queue is a data structure where data enters at the rear(last) of a
list and is removed from the front (first) of the list.
ٙ‫ انضضء انخهف‬ٙ‫اَاث ف‬ٛ‫خى إدخال انب‬ٚ ‫ذ‬ٛ‫اَاث ع‬ٛ‫كم ب‬ْٛ ْٙ ‫انغابٕس‬
.‫خى إصانخٓا يٍ انًقذيت (األٔنٗ) نهقائًت‬ٚٔ ‫ش) يٍ انقائًت‬ٛ‫(األخ‬

Queues are an example of a first-in, first-out (FIFO) data structure.


.)FIFO(‫اَاث الدخال أٔلً الخشاس أل‬ٛ‫كم ب‬ْٛ ٗ‫ؼذ انغابٕس يزاًل ػه‬ٚ

Queue Operations ‫عمليات الطابور‬



The two primary operations involving queues are adding a new
item to the queue and removing an item from the queue.
‫ذا‬ٚ‫صذ‬
ً ‫ًا‬
‫ اضافّ ػُصش‬ْٙ ‫خضًُٓا انغابٕس‬ٚ ٙ‫خاٌ انخ‬ٛ‫خاٌ األساس‬ٛ‫انؼًه‬
.‫إنٗ انغابٕس ٔإصانت ػُصش يٍ انغابٕس‬

The operation for adding a new item is called Enqueue(also called
enq, enque, add, or insert, and the operation for removing an item
from a queue is called Dequeue(also called deq, deque, remove, or
serve.
add ٔ‫ أ‬enque ٔ‫ أ‬enq ‫ًا‬
‫ض‬ٚ‫ (ٔحسًٗ أ‬Enqueue ‫ذ‬ٚ‫ت إضافت ػُصش صذ‬ٛ‫حسًٗ ػًه‬
‫ًا‬
‫ض‬ٚ‫ (ٔحسًٗ أ‬Dequeue ‫ت إصانت ػُصش يٍ انغابٕس‬ٛ‫ ٔحسًٗ ػًه‬، )insert ٔ‫أ‬
.)service ٔ‫ أ‬remove ٔ‫ أ‬deque ٔ‫ أ‬deq

The other primary operation (The Peek method) to perform on a
queue is viewing the beginning item.
‫خى إصشاؤْا ػهٗ انغابٕس‬ٚ ٙ‫) انخ‬Peek ‫قت‬ٚ‫ت األخشٖ (عش‬ٛ‫ت األساس‬ٛ‫انؼًه‬
.‫ت‬ٚ‫ ػشض ػُصش انبذا‬ْٙ

1
BY NsM [DATA STRUCTURES IN ARABIC]

Logical Structure ‫الهيكل المنطقي‬

Storage Structure ‫هيكل التخزين‬


Storage structure depends on the implementation of queue , array
or linked list structure.
‫كم قائًت‬ْٛٔ‫ يصفٕفّ أ‬. ‫ز انغابٕس‬ٛ‫ٍ ػهٗ حُف‬ٚ‫كم انخخض‬ْٛ ‫ؼخًذ‬ٚ
.‫يشحبغت‬
Example: Draw queue in each the following cases:

:‫ت‬ٛ‫ كم انغالث انخان‬ٙ‫ اسسى انغابٕس ف‬:‫يزال‬

1.enqueue('A');
2.enqueue('B');
3.enqueue('C');
4.dequeue();

Implementation of Queue ‫تنفيذ الطابور‬

Queue can be easily implemented using an Array or a Linked List.


Arrays are quick, but are limited in size and Linked List requires
overhead to allocate, link but is not limited in size.
.‫ز انغابٕس بسٕٓنت باسخخذاو يصفٕفّ أٔ قائًت يشحبغت‬ٛ‫ًكٍ حُف‬ٚ
‫ص‬ٛ‫نكٍ يغذٔدة انغضى ٔحخغهب انقائًّ انًشحبغّ حخص‬، ‫ؼت‬ٚ‫انًصفٕفاث سش‬
‫سج يغذٔدة انغضى‬ٛ‫ ٔنكُٓا ن‬overhead


2
BY NsM [DATA STRUCTURES IN ARABIC]

Implementation of Queue using Array ‫تنفيذ الطابىر بأستخذام مصفىفه‬

class queue {
int first=-1;
int last=-1;
int qu []=new int [10];
boolean isempty() {
return (first == last);
}
boolean isfull() {
return (last==qu.length-1);
}

void enQueue(int val) {


if (isfull())
System.out.println("Queue is full");
else {
last++;
qu[last] = val; }
}

int deQueue() {
int val=-1;
if (isempty()) {
System.out.println("Queue is empty, cant dequeue");
else {
first++;
val = qu[first];}
return val;}
}

3
BY NsM [DATA STRUCTURES IN ARABIC]

Exercise: Suppose the following operations ‫ افتزض العملياث التاليت‬:‫تمزين‬

1.dequeue(): Returns and removes item from front of queue.


.‫إسصاع ٔإصانت انؼُصش يٍ يقذيّ انغابٕس‬
2.void enqueue(int item): Adds item to last of queue
.‫إضافت ػُصش إنٗ آخش انغابٕس‬
3.boolean isEmpty(): Returns true if queue has no elements in it.
.ّٛ‫غخٕ٘ ػهٗ ػُاصش ف‬ٚ ‫ًّ صائبّ إرا كاٌ انغابٕس ل‬ٛ‫إسصاع ق‬
4.boolean isFull(): Returns true if queue full.
.‫صائبّ إرا كاٌ انغابٕس يًخهئ‬ ًّٛ‫إسصاع ق‬
5.int peek() : Returns item at front of queue without removing it.
.ّ‫ يقذيّ انغابٕس دٌٔ إصانخ‬ٙ‫إسصاع انؼُصش ف‬
6.int size() : Number of elements in queue.
.‫ انغابٕس‬ٙ‫ػذد انؼُاصش ف‬

Show the results of these operations on an initially empty integer queue


named q with Draw the queue contents after each operation, making clear
where the front is, and give the return value of each non-void method.
‫ يغ سسى‬q ًٗ‫س‬ٚ ‫ًا‬ٛ‫ظ فاسؽ يبذئ‬ٛ‫ عابٕس صغ‬ٙ‫اث ف‬ٛ‫اػشض َخائش ْزِ انؼًه‬
‫ ٔإػغاء‬، ‫ظ يٕقغ انًقذيت‬ٛ‫ يغ حٕض‬، ‫ت‬ٛ‫اث انغابٕس بؼذ كم ػًه‬ٕٚ‫يغخ‬
.‫ت‬ٛ‫ش خان‬ٛ‫قت غ‬ٚ‫ًت اإلسصاع نكم عش‬ٛ‫ق‬

4
BY NsM [DATA STRUCTURES IN ARABIC]

Queue implementation using a linked list


‫تنفيذ الطابور بأستخدام قائمه مرتبطة‬

Circular Queue ‫طابور دائري‬


Suppose the following queue:: ٙ‫افخشض انغابٕس انخان‬

and we went to execute enqueue('L'), in this case queue was full, it is


possible for the last of the queue to reach the end of the (physical) array
when the (logical) queue is not yet full. Because there may still be space
available at the beginning of the array, the solution is to let the array can be

5
BY NsM [DATA STRUCTURES IN ARABIC]

treated as a circular structure in which the last slot is followed by the first
slot as shown in below figure.
، ‫ ْزِ انغانت كاٌ انغابٕس يًخهئ‬ٙ‫ف‬, enqueue('L'( ‫ز‬ٛ‫رْبُا إنٗ حُف‬
‫) ػُذيا ل‬ٙ‫ت انًصفٕفّ (انفؼه‬ٚ‫صم آخش انغابٕس إنٗ َٓا‬ٚ ٌ‫يٍ انًًكٍ أ‬
‫ضال ُْاك يساعت‬ٚ ‫ ألَّ قذ ل‬.‫) يًخهئ بؼذ‬ٙ‫كٌٕ انغابٕس (انًُغق‬ٚ
‫ؼايم‬ٚ ٌ‫ ٔانغم ْٕ انسًاط نهًصفٕفّ أ‬، ّ‫ت انًصفٕف‬ٚ‫ بذا‬ٙ‫يخٕفشة ف‬
ْٕ ‫ آخش فخغت يٍ قبم انفخغت األٔنٗ كًا‬ٙ‫ حخبغ ف‬ٙ‫كم دائش٘ انخ‬ٛٓ‫ك‬
.ِ‫ انشكم أدَا‬ٙ‫ٍ ف‬ٛ‫يب‬

To get the next position for the last indicator, we can use an if statement
IF ًّٛ‫ًكُُا اسخخذاو حؼه‬ٚ ، ‫ش‬ٛ‫ نهًؤشش األخ‬ٙ‫نهغصٕل ػهٗ انًٕقغ انخان‬

in enQueue method:
if (last == (qu.length - 1))
last = 0;
else
last = last + 1;
And also in deQueue method:
if (first == (qu.length - 1))
first = 0;
else
first = first + 1;

6
BY NsM [DATA STRUCTURES IN ARABIC]

Numeric for Circular Queues ‫رقميه الطابور الدائري‬


Another way to reset last is to use the modulo (%) operator:
Front increases by (1 modulo size(length of queue) after each dequeue( ).
modulo ‫م‬ٛ‫ اسخخذاو ػايم انخشغ‬ْٙٔ ‫ٍ آخش‬ٛٛ‫قت أخشٖ إلػادة حؼ‬ٚ‫ُْاك عش‬
‫ عضى انٕعذة (عٕل انغابٕس)) بؼذ كم‬1( ‫حضداد انًقذيّ بًقذاس‬:)٪(
Front = (Front+1) % size; .)(dequeue

Rear(or last) increases by (1 modulo size(length of queue)) after


each enqueue( ):
))‫ عضى انٕعذة (عٕل انغابٕس‬1( ‫ش) بًقذاس‬ٛ‫ (أٔ األخ‬ٙ‫ذ انضضء انخهف‬ٚ‫ض‬ٚ
Rear= (Rear +1) % size; :enqueue)(‫بؼذ كم‬

The following example shown the queue is become full when first=last
‫صبظ يًخهئ ػُذ أٔل = آخشا‬ٚ ‫ انغابٕس‬ٙ‫ٕضظ انًزال انخان‬ٚ

7
BY NsM [DATA STRUCTURES IN ARABIC]

And also from the following cases shown the queue is empty when first=last
‫كٌٕ انغابٕس فاسؽ ػُذ أٔل = آخشا‬ٚ ‫ت انًٕضغت‬ٛ‫ًا يٍ انغالث انخان‬
‫ض‬ٚ‫ٔأ‬

Then, the condition last=first (in isempty() method) and the condition
last=qu.length(in isfull() method) are not suitable in circular queue, these
methods must be as follow:
= ‫ش‬ٛ‫األخ‬.‫) ٔانششط‬isempty ‫قت‬ٚ‫ عش‬ٙ‫ش = أٔلً (ف‬ٛ‫األخ‬. ‫ انششط‬، ‫رى‬
‫ضب‬ٚ ، ٘‫ عابٕس انذائش‬ٙ‫ٍ ف‬ٛ‫ش يُاسب‬ٛ‫) غ‬isfull ‫قت‬ٚ‫ عش‬ٙ‫ (ف‬qu.length
:ٙ‫ه‬ٚ ‫أٌ حكٌٕ ْزِ انغشق كًا‬

and in dequeue method if (last=first) must restart the queue as:


‫م انغابٕس‬ٛ‫ضب إػادة حشغ‬ٚ )last = first( ٌ‫ إرا كا‬dequeue ‫قت‬ٚ‫ عش‬ٙ‫ٔف‬
:‫كـ‬

first=-1;
last=-1;

8
BY NsM [DATA STRUCTURES IN ARABIC]

Double Ended Queue ‫الطابور مزدوج‬


Double Ended Queue is also a Queue data structure in which the insertion
and deletion operations are performed at both the ends (front and rear).
That means, we can insert at both front and rear positions and can delete
from both front and rear positions.
‫اث اإلدساس ٔانغزف‬ٛ‫ز ػًه‬ٛ‫ٓا حُف‬ٛ‫خى ف‬ٚ ‫اَاث انغابٕس‬ٛ‫كم ب‬ْٛ ‫ًا‬‫ض‬ٚ‫ أ‬ْٙ
‫ كم‬ٙ‫ًكُُا إدساس ف‬ٚ ، ُٙ‫ؼ‬ٚ ‫ ْٔزا‬.)ٙ‫ ٔانخهف‬ٙ‫ٍ (األياي‬ٛ‫ كال انغشف‬ٙ‫ف‬
‫ت‬ٛ‫ًكٍ عزفٓا يٍ كم يٍ انًٕاقغ األياي‬ٚٔ ‫ت‬ٛ‫ت ٔانخهف‬ٛ‫يٍ انًٕاقغ األياي‬
.‫ت‬ٛ‫ٔانخهف‬

Double Ended Queue can be represented in TWO ways, those are as follows..
.. ٙ‫ ْٔزِ كانخان‬، ٍٛ‫قخ‬ٚ‫ّ بغش‬ٚ‫م انغابٕس يضدٔس انُٓا‬ٛ‫ًكٍ حًز‬ٚ

1.Input Restricted Double Ended Queue ‫طابور مزدوج النهايه محدود اإلدخال‬
2.Output Restricted Double Ended Queue ‫طابور مزدوج النهايه محدود االخراج‬

Input Restricted Double Ended Queue ‫طابور مزدوج النهايه محدود اإلدخال‬

In input restricted double ended queue, the insertion operation is


performed at only one end and deletion operation is performed at
both the ends.
ٙ‫ت اإلدساس ف‬ٛ‫ز ػًه‬ٛ‫خى حُف‬ٚ ، ‫ت يغذٔد اإلدخال‬ٚ‫ عابٕس يضدٔس انُٓا‬ٙ‫ف‬
..ٍٛ‫ كال انغشف‬ٙ‫ت انغزف ف‬ٛ‫ز ػًه‬ٛ‫خى حُف‬ٚٔ ‫ت ٔاعذة فقظ‬ٚ‫َٓا‬

Output Restricted Double Ended Queue‫طابور مزدوج النهايه محدود االخراج‬

In output restricted double ended queue, the deletion operation is


performed at only one end and deletion operation is performed at both the
ends.

9
BY NsM [DATA STRUCTURES IN ARABIC]

ٙ‫ت انغزف ف‬ٛ‫ز ػًه‬ٛ‫خى حُف‬ٚ ، ‫ّ يغذٔد الخشاس‬ٚ‫ عابٕس يضدٔس انُٓا‬ٙ‫ف‬
.ٍٛ‫ كال انغشف‬ٙ‫ت انغزف ف‬ٛ‫ز ػًه‬ٛ‫خى حُف‬ٚٔ ‫ت ٔاعذة فقظ‬ٚ‫َٓا‬

Problem: Implementing the enqueue, dequeue, Peek, Clear in the


Double Ended Queue.
ّٚ‫ عابٕس يضدٔس انُٓا‬ٙ‫ ف‬Clear ،Peek ،dequeue ،enqueue ‫ز‬ٛ‫ حُف‬:‫انًشكهت‬

Hints: In the following algorithm for each above operation


enqueue operation
‫ت إدساس‬ٛ‫ت أػالِ ػًه‬ٛ‫ت نكم ػًه‬ٛ‫ت انخان‬ٛ‫ انخٕاسصي‬ٙ‫ ف‬:‫غاث‬ًٛ‫حه‬

Create new object(NewItem) ‫إنشاء كائن جديد‬

DeQueue operation ‫عمليه الحذف‬

10
BY NsM [DATA STRUCTURES IN ARABIC]

Q1: Write a printAll method which display all elements' data of the queue.

‫اكتب طزيقت طباعه الكل يعزض جميع عناصز الطابىر‬

Q2: Write a find method which search about a specific data in double ended
queue.

.‫اكتب طزيقت بحث والتي تبحث عن بياناث محذدة في طابىر مزدوج النهايت‬

Q3: Write a sortAsc method which sorts the elements of the queue
ascending.
.‫اكتب طزيقت تزتيب تصاعذي التي تزتب عناصز الطابىر تصاعذيًا‬

11

You might also like