0% found this document useful (0 votes)
13 views

03 Queue

Uploaded by

madm85369
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

03 Queue

Uploaded by

madm85369
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

CS214-Data Structure

Lecturer: Dr. Salwa Osama


Queue

1
Queue - What?
Let’s Refresh our Mind
Refresh your mind

1. Queue is (Linear – non linear) data structure


2. Queue is (non primitive - primitive)
3. Queue is a (Heterogenous – homogeneous)collection of elements.
4. The new element is added in ( front - rear ) of the queue. And remove element
from ( front – rear ) of the
5. The queue is called (First in first out, first in last out, last in first out, last in last out)
6. Examples of queue are ….(select one or more)
1. Toll Station

2. in Big super market

3. Printer

4. Function calls

5. Backtracking

6. Delimiter Checking

7. All of mentioned
Queue- What

 Queue is Linear - non-primitive data structure


 Queue is a homogeneous collection of elements.
 In which new elements are added at one end called rear or
tail, and the existing elements are deleted from other end
called front or head.

So, the Queue is called First-in-First-out (FIFO)


 Examples of queues: Toll Station, Check-out in Big super
market, Printer, …
Queue- What

20  Add (20)
50  Add( 5)
-3
30  Add(30)
 Delete
 Delete
 Add(0)
 Add( -3)

Stack Animation by Y. Daniel Liang


(pearsoncmg.com)
Implementation User View

Queue
OPERATIONS PERFORMED ON
QUEUE

 Create the queue, leaving it empty.

 Determine whether the queue is empty or not.

 Determine whether the queue is full or not.

 Enqueue a new entry onto the end of the queue

 Dequeue the entry at the front of the queue.


Queue Implementation

#define MAX 10
typedef char EntryType;
typedef struct {
int front;
int rear;
EntryType entry[MAX];
} QueueType;
Queue Implementation

D C B A
• A queue
Max rear front

• After A leaves, D C B

Max rear front


• After B and C leave
Max KG I HG F E D

rear front
How can we insert a new element?!!
Queue Implementation

One solution is to shifting all items to front when


dequeue operation.
D C B A

Max rear front

After A leaves, D C B

Max rear front


After B and C leave
G F E D
Max
Too Costly…
rear front
Queue
Implementation

A better solution is a
circular Queue
Circular Queue
rear front
front
rear

One solution is to keep track with


the number of elements on each
queue
Size = Max Size = 0
Full Queue Empty Queue
In both cases front comes after rear with one
step. Then, how can we distanguish between
the two cases?!!
Queue Implementation

#define MAX 10
typedef char EntryType;
typedef struct {
int front;
int rear;
int size;
EntryType entry[MAX];
} QueueType;
Queue Implementation

 Create operation:
Pre: None.
Post: The queue is initialized to be empty.
void CreateQueue(QueueType *q){
q->front= 0;
q->rear = max -1;
q->size = 0;
}
Queue Implementation

Queue empty operation:


Pre: The queue is initialized.
Post: If the queue is empty (1) is returned. Otherwise (0) is returned.
int QueueEmpty(QueueType q){
return (q.size==0);}
Queue full operation:
Pre: The queue is initialized.
Post: If the queue is full (1) is returned. Otherwise (0) is returned.
int QueueFull(QueueType q){
return (q.size==MAX);}
Queue Implementation

 Enqueue operation:
Pre: The queue is initialized and is not full.
Post: Item is added to the end of the queue.
void Enqueue(EntryType item, QueueType *q){
q->rear ++; It is for linear not circular
q->rear = (q->rear+1)%Max; // to circulate the queue
q->entry[q->rear] = item;
q->Size ++;
}
Queue Implementation

 Dequeue operation:
Pre: The Queue is initialized and is not empty.
Post: The front element of the Queue is removed from it and is
assigned to item.
void dequeue (Entry *item, QueueType *q){
*item = q->entry[ q->front];
q->front = (q->front +1)% MAX
q->Size--;
}
Exercise

 In the implentation level of the queue ADT, write the


QueueTraverse which is defined as :
void TraverseQueue(QueueType* pq, void (*pf)(Entry
*))
Where pf is a pointer to a function that is passed over all of the
queue *pq elements to perform a task

 Use the previous function to print on the screen all a queue


elements in the user level.
Exercise

void TraverseQueue(Queue* q, void


(*f)(EntryType*)){
int i, siz;
for(i=q->front, siz=0; siz<q->size; siz++){
Print(&q->entry(i))
could Or
(*f)(&q->entry[i]); Increment(&q->entry(i))
be
Or

i=(i+1)%MAX;
}
}
Exercise

void Print(EntryType *e){


printf(“e is: %d\n", *e);
}
void main(){
QueueType q;
CreateQueue(&q);


TraverseQueue(&q, &Print);
}
Excercise2

 Write a function that returns a copy from the first element in a


queue. (Implementation level)
Excercise2

 Write a function that returns a


copy from the first element in
a queue. (Implementation
level)
Exercises3

 We (as a user for QueueADT) have two filled queues; the first
queue holds section code while the other holds group code
(where number of groups inside the section is maximum 10).
Merge those numbers (section code*10+group code) in a
newly created queue.
Exercises3

 We (as a user for


QueueADT) have two filled
queues; the first queue
holds section code while
the other holds group code
(where number of groups
inside the section is
maximum 10). Merge
those numbers (section
code*10+group code) in a
newly created queue.
Exercises3
27

You might also like