Professional Documents
Culture Documents
Chapter five
The Queue
What is Queue data structure?ما هو هيكل بيانات الطابور
1
BY NsM [DATA STRUCTURES IN ARABIC]
1.enqueue('A');
2.enqueue('B');
3.enqueue('C');
4.dequeue();
2
BY NsM [DATA STRUCTURES IN ARABIC]
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);
}
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]
4
BY NsM [DATA STRUCTURES IN ARABIC]
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]
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
:ٙهٚ أٌ حكٌٕ ْزِ انغشق كًا
first=-1;
last=-1;
8
BY NsM [DATA STRUCTURES IN ARABIC]
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 طابور مزدوج النهايه محدود اإلدخال
9
BY NsM [DATA STRUCTURES IN ARABIC]
ٙت انغزف فٛز ػًهٛخى حُفٚ ، ّ يغذٔد الخشاسٚ عابٕس يضدٔس انُٓاٙف
.ٍٛ كال انغشفٙت انغزف فٛز ػًهٛخى حُفٚٔ ت ٔاعذة فقظَٚٓا
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