الطابور QUEUE (abstract data type) واحد من هياكل البيانات الخطية الشائعة الاستخدام داخل البرامج. يحتوي علي عناصر من نفس النوع. من أنواع البيانات الخطية.

Slides:



Advertisements
Similar presentations
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Advertisements

Queues. Queue Definition Ordered list with property: All insertions take place at one end (tail) All insertions take place at one end (tail) All deletions.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
Module R2 Overview. Process queues As processes enter the system and transition from state to state, they are stored queues. There may be many different.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Data Structures (Second Part) Lecture 3 : Array, Linked List, Stack & Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering.
1 Stack and Queue. 2 Stack In Out ABCCB Data structure with Last-In First-Out (LIFO) behavior.
CSCE 3110 Data Structures & Algorithm Analysis Queues Reading: Chap. 3 Weiss.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
1 A full binary tree A full binary tree is a binary tree in which all the leaves are on the same level and every non leaf node has two children. SHAPE.
1 Queues and Lists. QUEUES Very similar to stacks The only difference between them is in the order in which elements are processed. A stack uses a last-in/first-out.
Data Structure Dr. Mohamed Khafagy.
Queue Overview Queue ADT Basic operations of queue
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Queues.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Linked List Spring 2013Programming and Data Structure1.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Cosc237/data structures1 Data Types Every data type has two characteristics: 1.Domain - set of all possible values 2.set of allowable operations Built-in.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
Data Structures Using C++
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
Cousin of the Stack.  An abstract data type (container class) in which items are entered at one end and removed from the other end  First In First.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
Data Structures – Week #4 Queues. January 12, 2016Borahan Tümer, Ph.D.2 Outline Queues Operations on Queues Array Implementation of Queues Linked List.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Linear Data Structures
1 Queues and Lists. QUEUES Very similar to stacks The only difference between them is in the order in which elements are processed. A stack uses a last-in/first-out.
1 Queues Chapter 4. 2 Objectives You will be able to Describe a queue as an ADT. Build a dynamic array based implementation of a queue ADT.
LINEAR LINKED LISTS The disadvantages of arrays: 1.The size of the array is fixed. 2.Large size of array??? 3. Inserting and deleting elements. If the.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
Linked List. Insert a node at the beginning #include struct node { int data; struct node* next; }; struct node* head; void Insert(int x) { node *temp.
Queue ADT for lining up politely. COSC 2006 queue2 Queue – simple collection class  first-in first-out (FIFO) structure insert new elements at one end.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
Circular Queues Maitrayee Mukerji. Queues First In – First Out (FIFO) The first element to be inserted is the first one to be retrieved Insertion at one.
1 Data Structures and Algorithms Queue. 2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
What is a Queue? Queue is a linear data structure in which the insertion and deletion operations are performed at two different ends. In a queue data structure,
Unit-3 Queues-operations, array and linked representations. Circular Queue operations, Dequeues, applications of queue.
CS505 Data Structures and Algorithms
UNIT II Queue.
QUEUES.
Doubly Linked List Review - We are writing this code
Queue data structure.
Stack and Queue APURBO DATTA.
Queues Chapter 4.
CSCE 3110 Data Structures & Algorithm Analysis
Queues.
DATA STRUCTURE QUEUE.
CSC 143 Queues [Chapter 7].
18.5 Linked Queues Like a stack, a queue can be implemented using pointers and nodes Allows dynamic sizing, avoids issue of wrapping indices NULL front.
Queues FIFO Enqueue Dequeue Peek.
Stacks and Queues 1.
Getting queues right … finally (?)
Data Structures & Programming
Presentation transcript:

الطابور QUEUE (abstract data type) واحد من هياكل البيانات الخطية الشائعة الاستخدام داخل البرامج. يحتوي علي عناصر من نفس النوع. من أنواع البيانات الخطية linear data structure. مبدأ عمل الطابور هو FIFO الداخل اولا الخارج اولا. هناك طابور يعمل بمبدأ NON-FIFO ( Priority Queue). Queues and Priority Queues الطابور QUEUE في حياتنا عبارة عن خط من الزبائن المنتظرة خدمة معينة, في الغالب الزبون الاول في هذا الخط هو التالي في تقديم الخدمة له. هناك بعض الاستثناءات : 1- علي سبيل المثال في المطار الزبون ( الشخص ) التي ستغادر رحلته الحين سوف يخرج من الطابور لتقديم الخدمة له. 2- في السوبر ماركت الزبون المهذب سوف يسمح للزبون الذي يمتلك اشياء بسيطة في الدخول للمحاسب.

قاعدة حساب من التالي في الخروج من الطابور تعرف بــ queueing discipline. ابسط نوع من queueing discipline هي ( QUEUE ) FIFO. القاعدة العامة من queueing discipline هي priority queueing. في priority queueing كل عميل له periority و الذي يملك اعلي periority هو الذي يخرج من الطابور لتقدم له الخدمة مع اهمال من اتي اولا للطابور. The Queue ADT and the Priority Queue ADT الاثنان يملكان نفس العمليات و لهم نفس الشكل interface, الاختلاف في التركيب الداخلي للعمليات. يمكن تمثيلة باستخدام الــ heap. stack — elements are pulled in last-in first-out-order.last-in first-out queue — elements are pulled in first-in first-out-order.first-in first-out priority queue — elements are pulled highest-priority-first.

Priority Queues insert_with_priority: add an element to the queue with an associated priorityelementqueuepriority pull_highest_priority_element: remove the element from the queue that has the highest priority, and return it. طرق تمثيل الطابور QUEUE: باستخدام المصفوفة Queue - Array Implementation. باستخدام القائمة Queue – LIST Implementation.

queue | a | b | c | ^ ^ | | front rear عملية الاضافة ( الادخال ) تتم من المؤخرة عند Rear. عملية الحذف ( الاخراج ) تتم من المقدمة عند Front. العمليات التي تتم علي الطابور هي : 1- اضافة (add / enter). 2- حذف delete / remove)). 3- اختبار الطابور فارغ ام لا Empty(). 4- اختبار الطابور ممتلئ ام لا Full(). مثال : الطابور مكون من مجموعة احرف, front يشير للعنصر الاول ( موقعه ) في الطابور, rear يشير للعنصر الاخير ( موقعه ).

Now, Enter ( queue, 'd‘ ) Queue | a | b | c | d | ^ ^ | | front rear Now, ch = Delete(queue)... Queue ch | b | c | d | | a | ^ ^ | | front rear مثال : اضافة عنصر الي الطابور حذف عنصر من الطابور.

تمثيل الطابور كمصفوفة ثابته الحجم Fixed Size Array Implementation: 1. مصفوفة ( contents) an array 2.a front index ( موقع اول عنصر ) 3. a rear index ( موقع آخر عنصر ) سنحتاج الي : queue (made up of 'contents', 'front' and 'rear') | a | b | c | d | | 0 | | 3 | front rear contents

عند حذف عنصر من الطابور contents. Now, remove one with ch = Delete(queue), giving: queue (made up of 'contents', 'front' and 'rear') | | b | c | d | | 1 | | 3 | front rear contents عند اضافة عنصر للطابور contents. Now, add one with Enter(queue, 'e'), giving: queue (made up of 'contents', 'front' and 'rear') | e | b | c | d | | 1 | | 0 | front rear contents

#include int const max=10; int queue[max]; int front=-1, rear=-1; int full() { if (rear ==max-1) return(1); else return(0); } int empty() { if (front == rear) return(1); else return(0); }

void add(int x) { if(full() == 1) { cout<<"\n\nQueue Full\n"; } else { rear =rear + 1; queue[rear] = x; }

int delete1() { if(empty() == 1) { cout<<"\n\nQueue Empty\n"; } else { if(front == max-1) { front=0;} else return queue[front++]; } }

void main() { add(10); add(90); add(100); cout<<delete1()<<" "<<delete1()<<" "<<delete1(); }

تمثيل الطابور كقائمة Linked List Implementation: سنحتاج الي : تركيبة node ( عقدة / حلقة ) لتخزين البيانات + مؤشر للعقدة التالية. الوظائف التي ستتم علي الطابور هي : اضافة عنصر للطابور Enqueue. حذف عنصر من الطابور Dequeue. ميزة استخدام القائمة عن المصفوفة هو امكانية اضافة أي عدد من العناصر. الاضافة ستتم من نهاية القائمة ( اضافة عقدة في النهاية ). الحذف يتم من مقدمة القائمة ( حذف عقدة من بداية القائمة ).

اضافة عقدة جديدة في نهاية القائمة Insert to back struct node { int data; node *next; }; typedef struct node *PtrToNode; typedef PtrToNode List;

node* enqueue(list head,int info) { node *temp1; temp1 = head; while(temp1->next!=NULL) temp1 = temp1->next; node *temp; temp = new node; temp->data = info; temp->next = NULL; temp1->next = temp; return head; }

node* dequeue(List head) { node *temp; temp = head; head = temp->next; delete temp; return head; } حذف عقدة من بداية القائمة delete from front

طريقة اخري لبرمجة QUEUE

# include # define MAXSIZE 3 struct st { int front,rear; int queue[MAXSIZE]; } s; int empty(); int full(); void add(int); void delete1(); void display();

void main() { s.front = -1; s.rear = -1; add(3); add(6); add(9); display(); delete1(); add(15); display(); delete1(); display(); }

int full() { if (s.rear == MAXSIZE-1) return(1); else return(0); } int empty() { if (s.front == s.rear + 1) return(1); else return(0); }

void add(int x) { if(full() == 1) { cout<<"\n\nQueue Full\n"; } else { s.rear = s.rear + 1; s.queue[s.rear] = x; if(s.rear == 0) s.front ++; }

void delete1 () { if(empty() == 1) { cout<<"\n\nQueue Empty\n"; } else { cout<<"\n"<<s.queue[s.front]<<" Has Been Deleted!"<<endl; s.front = s.front +1; }

void display() { int i; if(empty () == 1) cout<<"\nQueue Empty!!"; else { cout<<"\nDisplaying Queue\n"; for(i = s.front ; i<=s.rear ; i++) { cout<<s.queue[i]<<endl; } }

RUN

تمثيل الطابور كمصفوفة متغيرة الحجم dynamic Size Array Implementation: struct st { int *queue; int front,rear; int maxSize; }; typedef struct st *s1; typedef s1 ss; void add(ss P, int x); int delete1(ss P);

void main() { struct st d,*r; r=&d; r->rear=-1; r->front=-1; cout<<"Enter array size? "; cin>>r->maxSize; r->queue=new int[r->maxSize]; add(r, 10); add(r, 20); add(r, 30); add(r, 40); cout<< "\n" << delete1(r)<<endl; }

void add(ss p, int x) { if (p->rear == p->MAXSIZE-1) { cout<<"\nqueue is full \n"; } else { p->rear = p->rear + 1; p->queue[p->rear] = x; if(p->rear == 0) p->front++; }

int delete1(ss p) { if (p->front == p->rear + 1) { cout<<"\n\nQueue Empty \n"; } else { return p->queue[p->front++]; }

RUN

#include struct node { int value; struct node *next; }; typedef struct node *n; typedef n queue; تابع للمحاضرة القديمة ( غير المتعثرات )

void Enqueue(queue &head,int value) { queue temp1=(struct node*)malloc(sizeof(struct node)); temp1->value=value; temp1->next=NULL; queue temp ; temp=head; if(head==NULL) head=temp1; else { while(temp->next!= NULL) { temp=temp->next; } temp->next= temp1; }

int Dequeue(queue &head) { if(head==NULL) cout<<"\n No Element to Dequeue \n"; else { queue temp; temp=head; head=temp->next; free(temp); return temp->value; }

void main() { queue head=NULL; Enqueue(head,10); Enqueue(head,50); Enqueue(head,570); Enqueue(head,5710); cout<<"\n Value Dequeued is : "<<Dequeue(head)<<endl; } Run