هياكل البيانات المكدسات والصفوف
TRANSCRIPT
المكدسات Stackمفهوم
2
المتغيرة .- الخطية البيانات هياكل من هيكل عبارة هي-. معينة بطريقة المخزنة العناصر من مجموعة من تتكونيسمى )- المكدسة قاع يشكل االول والعنصر( BOTTOMالعنصر
يسمى ) المكدسة قمة (.TOPاألخيرهو - واحد اتجاهـ من يجريان والحذف TOPاالضافةمبدأ - : .LIFO (Last In First Out)يستخدم أوال يخرج أخيرا: الداخل
243161165517
TOP= 6TOP= -1
BOTTOM
3
المكدسة :Stackخصائص
-. الرغبة حسب ويتقلص يزداد ديناميكيعبر - يتم عنصر الي الي . TOPالوصول الوصول يمكن ال
. منه االعلى العناصر جميع حذف بعد اال عنصر ايقمة - الي يشير واحد مؤشر تعريف المكدسة تتطلب
عليه نطلق Topالمكدسةاضافة - عملية كل عند واحد بمقدار المؤشر قيمة تزداد
حذف عملية كل عند واحد بمقدار وتنقصيساوي -- المؤشر كان اذا C خاليا المكدس topأي 1يصبح
=-1.
4
ADT StackData A list of item with a position
Top = indicates the top of the stack
Operations
Constructor stackInitial values: NoneProcess: Initialize the top of the stack
dtypeStack empty()Input: NonePreconditions: NoneProcess: Check whether the stack is
emptyOutput: Return true if stack is empty
and false otherwisePost conditions: none
5
ADT StackOperations
dtype Pop(void)Input: None
Preconditions: Stack is not empty
Process: Remove the item from the top of the stack
Output: Return the element from the top of the stack
Post conditions: Element at the top of the stack is removed
void Push(dtype)Input: An item for the stack
Preconditions: Stack is not full
Process: Store the item on the top of the stack
Output: None
Post conditions: The stack has a new element at the top
6
ADT StackOperations
dtypePeek(void)Input: None
Preconditions: Stack is not empty
Process: Retrieve the value of the item on the top
Output: Return the value of the item from the top
Post conditions: The stack is unchanged
void Clearstack(void)Input: None
Preconditions: None
Process: Delete all the item of the stack and reset the top
Output: None
Post conditions: The stack is reset to the initial conditions
7
ADT StackOperations
dtypeStackfull()
Input: None
Preconditions: None
Process: Check whether the stack is full
Output: Return true if stack is full else false
Post conditions: None
8
المكدسة فصيلة Stack classانشاء
.1. المكدسة إنشاء
.2 : المكدسخاليا ان من Empty_stackالتأكد
.3 : المكدسممتلئا من Full_ stackالتاكد
المكدس )4. من عنصر (POPحذف
المكدس )5. الي عنصر (.Pushاضافة
: منها كعمليات المكدسات مع تتعامل أن يمكن الدوال من عدد هنالك# include > iostream .h<# include > conio .h<const int max_stack_size=4;class stack{private:int stack_list[max_stack_size-1];int top;public:Stack();int empty_stack();int pop();void push(int);int full_stack();};
9
األعضاء الدوال تعريفالمكدس >include > iostream .h #انشاء
# include > conio .h<const int max_stack_size=4;class stack{private:int stack_list[max_stack_size-1];int top;public:Stack();{
Top== -1;
}
};
تنفذ)( stackالدالة بناء دالة هييتبع هدف عن االعالن بمجرد
قيمة وتضع الفصيلة اليللمؤشر Top =-1اولية
10
األعضاء الدوال تعريف Nالمكدسخال أن من التأكد
# include > iostream .h<# include > conio .h<const int max_stack_size=4;class stack{private:int stack_list[max_stack_size-1];int top;public:Int empty_Stack(){
return Top== -1;
}
};
تقوم) ( empty stackالدالة ام خالية المكدسة من بالتأكد
قيمة خالل من وذلك Topالتساوي - كانت تقوم 1فاذا
ان( True)1بارجاع ايستقوم واال خاليا المكدسة
ان( false )0بارجاع وهيخالية غير المكدسة
11
األعضاء الدوال تعريفالمكدس ) من (POPحذفعنصر
# include > iostream .h<# include > conio .h<const int max_stack_size=4;class stack{private:int stack_list[max_stack_size-1];int top;public:Int pop() {if )top==-1(
{cout > > “\nattemp to pop an empty stack”;
Exit)1(;}
else
{int item = stack_list[top];
top--;
return item;}}
من) ( popالدالة بالتأكد تقوممن وذلك ال ام خالية المكدسة
قيمة تساوي Topخالل كانت فاذاهي 1- خطأ رسالة بطباعة تقوم
مكدسة) من حذفعنصر محاولة: ) بـ تقوم وإال اصال: خالية
قمة 1. في الموجود العنصر قراءةالمكدسة
قيمة 2. واحد topنقصان بمقدار
المحذوف 3. العنصر ارجاع
12
األعضاء الدوال تعريفالمكدسممتلئ أن من التأكد
# include > iostream .h<# include > conio .h<const int max_stack_size=4;class stack{private:int stack_list[max_stack_size-1];int top;public:Int empty_Stack();{
return Top== -1;
}
};
تقوم) ( int full_ stackالدالة ام خالية المكدسة من بالتأكد
قيمة خالل من وذلك Topالتساوي - كانت تقوم 1فاذا
ان( True)1بارجاع ايستقوم واال خاليا المكدسة
ان( false )0بارجاع وهيخالية غير المكدسة
13
األعضاء الدوال تعريفالمكدس ) من عنصر (Pushاضافة
# include > iostream .h<# include > conio .h<const int max_stack_size=4;class stack{private:int stack_list[max_stack_size-1];int top;public:void push(int item);{
if )top== max_stack_size-1(
{cout > > “\nstack is full overflow/n”;
Exit)1(;}
++ top;
Stack_list [top] = item;}
تقوم push(int item )الدالة ممتلئة المكدسة من بالتأكد
قيمة خالل من وذلك ال Topامتساوي كانت فاذا
max_stack_size تقومالمكدسة ) رسالة بطباعة
ممتلئة( قيمة بزيادة تقوم topوإال
العنصر وجعل واحد بمقدارالمكدسة قمة في الجديد
14
األعضاء الدوال تعريفالمكدس تغيير عدم من )(peekللتأكد
# include > iostream .h<# include > conio .h<const int max_stack_size=4;class stack{private:int stack_list[max_stack_size-1];int top;public:int peek ();{
if )top== -1(
{cout > > “/nstack is empty/n”;
Exit)1(;
}
Return Stack_list [top];
}
تقوم)( dtype peekالدالة الذي العنصر قيمة باسترجاع
في ان Topيوجد من للتاكداي به يحصل المكدسلم
تعديل
15
المكدسات على تطبيقات
بلغة 1. دالة اكتب ثم اخرى مكدسة الي مكدسة محتويات نقل فكرة اشرحC ++؟ بذلك تقوم
دالة 2. اكتب ثم اخرى مكدسة الي مكدسة محتويات نسخ فكرة اشرح؟++ Cبلغة بذلك )( void mainتقوم
{ stack s,s1,temp;
int I;
while )!s.empty_stack)((
{
i =s.pop)(;
temp .push)i(;
}
while )!temp.empty_stack)(({
i= temp.pop)(;
s1 .push)i(;
s.push)i(;}
}
16
Queueالصفوف خطية - بصورة تحفظ العناصر من قائمة هيالمقدمة - ويسمى واحد طرف من يتم العناصر Frontحذف
الذيل ويسمى االخر الطرف من تتم العناصر Rearوإضافةمبدأ - FIFO (First In First Out). Cيستخدم أوال يخرج C أوال الداخل
front rear
17
الصفوف أنواعالخطي الصف
محددة سعة له اي الحجم محدود العناصر من قائمةالعناصر = عدد الذيل قيمة أن هو امتالئه شرط
) Rear=sizeالحجم)
61
16
55
17
size= 6Size=6
front=0
Rear=4
Rear=front=0
صفخطي - اعالهـ الشكل يمثلالذيل > قيمة حيث ممتلئ غير
الراس = قيمة وان 0الحجمعلى - ممتلئ الصفغير يكون قد
من الراسأكبر قيمة أن من الرغمالحجم 0 من اصغر الذيل وقيمة
صفخطي - اعالهـ الشكل يمثلقيمة = الذيل قيمة حيث فارغ
0الراس = على - ممتلئ الصفغير يكون قد
من الراسأكبر قيمة أن من الرغمالحجم 0 من اصغر الذيل وقيمة
61
16
55
17
size=rear = 6
front=2
18
الصفوف أنواعالدائري الصف
فكرة - ويتجنب باوله الصف آخر اتصال على فكرته تقوم اخر اسلوب هواالزاحة
7788
38
54
6590
01 23
456789
1112
- : دائريا صف اعالهـ الشكل يمثلالذيل > قيمة حيث ممتلئ غير
الراس =13> 6الحجم قيمة 0وانعلى - ممتلئ الصفغير يكون قد
من الراسأكبر قيمة أن من الرغمالحجم 0 من اصغر الذيل وقيمة
7788
38
54
6590
01 23
456789
1112
9966
55
44
2233
- : دائريا صف اعالهـ الشكل يمثلالحجم = الذيل قيمة حيث ممتلئا:
الراس = 13= 13 قيمة 0وان- : ممتلئا الدائري الصف يكون وقد
هو آخر front = rear+1بشرط
19
ADT Queue
Data A list of item
Rear:a position that references the first item in the queue
front:a position that references the last item in the queue
nitem:the number of entries in the queue at given time
20
Operations Constructor queueInitial values: None
Process: Initialize the front,rear and nitem of the queue
dtype Qlength(void)Input: None
Preconditions: None
Process: Determine the number of items in the queue
Output: Return the number of items in the queue
Post conditions: none
ADT Queue
21
ADT queueOperations dtype Qempty(void)Input: None
Preconditions: None
Process: Check whether the queue is empty
Output: Return 1 is empty 0 otherwise
Post conditions: None
void Qinsert(dtype)Input: An item to store in the queue
Preconditions: Queue is not full
Process: Store the item on the rear of the queue
Output: None
Post conditions: a new item is added to the queue
22
ADT queueOperations
dtypedelete(void)Input: None
Preconditions: Stack is not empty
Process: Remove an item from the front of the queue
Output: Return the item that is removed from the queue
Post conditions: An item is deleted from the queue
void Clearqueue(void)Input: None
Preconditions: None
Process: revomed all the item from the queue and reset the initial conditions
Output: None
Post conditions: The queue is empty
23
الصفوف فصيلة Queue classانشاء
.1. الصف إنشاء
.2 : خاليا الصف ان من Empty_queueالتأكد
.3 : ممتلئا الصف من Full_ queueالتاكد
الصف )4. من عنصر (Qdeleteحذف
.5( الصف الي عنصر (Qinsertاضافة
الصف )6. في العناصر عدد (Qlengthتحديد
الصف )7. عناصر جميع (Qclearحذف
المقدمة )8. في الذي الرقم (Qfrontعرض
: منها كعمليات االصفوف مع تتعامل أن يمكن الدوال من عدد هنالك
# include > iostream .h<# include > conio .h<const int max_queue_size=10;class queue{private:dtype queue_list[max_queue_size];Dtype rear,front,nitem;public:queue();dtype empty_queue();Dtype Qdelete();Void Qinsert(dtype);int full_queue();};
24
األعضاء الدوال تعريفالصف >include > iostream .h #انشاء
# include > conio .h<const int max_queue_size=10;class queue{private:Dtype
queue_list[max_queue_size];int rear,front,nitem;public:queue();{
Rear ==0;
Front ==0;
Nitem==0;}
};
تنفذ)( queueالدالة بناء دالة هييتبع هدف عن االعالن بمجرد
قيمة وتضع الفصيلة اليللمؤشراتالمستخدمة اولية
الصف : فيRear=0 , front =0 ,nitem=0
25
األعضاء الدوال تعريف Nخال الصف أن من التأكد
# include > iostream .h<# include > conio .h<const int max_queue_size=10;class queue{private:Dtype
queue_list[max_queue_size];int rear ,front,nitem;public:Int empty_queue();{if)nitem==0(
{
Cout<<“queue is empty”;
}
else
{Cout<<“queue is not empty”;}};
بالتأكد) ( empty queueالدالة تقوممن وذلك ال ام الصفخالية من
قيمة كانت nitemخالل فاذابارجاع 0تساوي ( True)1تقوم
ستقوم واال الصفخاليا ان ايالصف( false )0بارجاع ان وهي
خالي غير
26
األعضاء الدوال تعريفالصف من حذفعنصر
# include > iostream .h<# include > conio .h<const int max_queue_size=10;class queue{private:Dtype queue_list[max_queue_size];int rear ,front,nitem;public:Dtype Qqueue();{ dtype item;
if)nitem;==0({
Cout<<“queue is empty”;}
else{
item = queue_list[front];
nitem--;
Front=)front+1(%max_queue_size;
return item;}};
الصف) ( Qdeleteالدالة من بالتأكد تقومقيمة خالل من وذلك ال ام nitemخالي
تساوي = كانت بارسال 0فاذا تقوم: واال خطأ رسالة
مقدمة - في الموجود العنصر قراءةالصف
قيمة - 1بمقدار nitemنقصانقيمة - = frontحساب
(front+1)%max_queue _ sizeالمحذوف - العنصر ارجاع
27
األعضاء الدوال تعريفالصف الي عنصر Qinsertاضافة
# include > iostream .h<# include > conio .h<const int max_queue_size=10;class queue{private:Dtype queue_list[max_queue_size];int rear ,front,nitem;public:Void Qinsert(dtype item);{if)nitem== max_queue_size(
{Cout<<“queue is full”;}
else
{ nitem++;
Queue_list[rear]=item;
Rear = )rear+1(%max_queue_size;}};
بالتأكد Qinsert(dtype item)الدالة تقوممن وذلك ال ام الصفممتلئ ان من
قيمة تساوي nitemخالل كانت فاذاmax_queue_size بطباعة تقوم تقوم
قيمة بزيادة تقوم واال خطأ رسالةnitem ذيل قيمة وتعدل واحد بمقدار
خالل =rearالصفمن(rear+1)%max_queue_size وجعلالصف ذيل في الجديد العنصر
28
األعضاء الدوال تعريفالصفممتلئ أن من التأكد
# include > iostream .h<# include > conio .h<const int max_queue_size=10;class queue{private:Dtype queue_list[max_queue_size];int rear ,front,nitem;public:dtype full_queue();{if)nitem== max_queue_size(
{
Cout<<“queue is full”;
}
else
{Cout<<“queue is not full”;}};
بالتأكد) ( full queueالدالة تقومال ام الصفممتلئ ان من
قيمة خالل من فاذا nitemوذلكتساوي max_queue_sizeكانتبارجاع ان( True)1تقوم اي
ستقوم واال الصفممتلئالصف( false )0بارجاع ان وهي
ممتلئ غير
29
األعضاء الدوال تعريفالعناصر عدد او الصف طول معرفة
# include > iostream .h<# include > conio .h<const int max_queue_size=10;class queue{private:Dtype
queue_list[max_queue_size];int rear ,front,nitem;public:Int Qlength(){return nitem;}
};
عدد) ( Qlengthالدالة بارجاع تقومالصف في العناصر
30
األعضاء الدوال تعريفالمقدمة في الذي العنصر من التأكد
# include > iostream .h<# include > conio .h<const int max_queue_size=10;class queue{private:Dtype queue_list[max_queue_size];int rear ,front,nitem;public:Int Qfront();{if)nitem==0(
{
Cout<<“queue is empty”;
}
else
{return queue_list[front];}};
العنصر)( Qfrontالدالة بارجاع تقومالمقدمة في الذي
31
األولوية Priority Queueصفوففي - االولويات بعض بها يكون الصفوف انظمة بعض ان يالحظ
الخدمات تقديم : اوال تقدموا بمن يتم ال معينة كلية الي الشهادة طالب دخول مثال
الطالب ) لنتيجة وفقا يتم مؤشر بل هي (النتيجة
تصاعديا- او تنازليا يكون ان يمكن الترتيبمثال:
front 3 6 7 12 23 rear
العنصر ادخال تم الـ 15اذا بين باتباع 23والـ 12فيجبوضعه وذلكاالتي:
الصف. 1 مؤخرة من االدخال rearيتمالعناصر. 2 ترتيب فيها يتم اليمصفوفة الصف عناصر ارسال يتم
المطلوبة بالطريقة3. ترتيبها. اعادة بعد جديد الصفمن الي العناصر ارسال frontيتم 3 6 7 12 15 23 rear
يتم اعادة ترتيب الصف كل فيعملية اضافة
32
االولوية صفوف على مأخذالتشغيل - زمن اطالة في يتسبب ما هو عليها العمليات كثرة-) + المصفوفة ) الصف الذاكرة من مضافة مساحة اهدارطوله - عن يفترضاالعالن الصف ان باعتبار للتوسع القابلية عدمفقط )- واحدة اولوية فيظل تصاعديا يعمل أو (تنازليا
في التفكير من البد فكانعلى المحافظة مع امثل حل
للعنصر الخروج ان خاصيةالقائمة مقدم من يتم
فكانت القوائم المتصلة