Queue PGT 303 Data Structure Lecturer En Mohd

  • Slides: 20
Download presentation
Queue PGT 303 : Data Structure Lecturer : En. Mohd Rizal bin A. Rejab

Queue PGT 303 : Data Structure Lecturer : En. Mohd Rizal bin A. Rejab

GROUP MEMBERS 1. YOONG YING KHAI 2. NESAMALAR 3. LOW KAR XIN 4. MOHAMMAD

GROUP MEMBERS 1. YOONG YING KHAI 2. NESAMALAR 3. LOW KAR XIN 4. MOHAMMAD HAKIMI JOHARI 5. MUHAMAD AKMAL ADIB BIN AKHPAH (121302530) (121301416) (121300719) (121300910) (121301052)

GROUP CONTENT 1. What is queue? 2. Operation on queues • Queue implementation •

GROUP CONTENT 1. What is queue? 2. Operation on queues • Queue implementation • Implementation of insert and delete operations on a queue 3. Limitations of linear queues 4. Circular queues • Operations on circular queues • Implementation of insert and delete operations on a circular queue 5. Application

What is Queue? • A data structure in which the elements are added at

What is Queue? • A data structure in which the elements are added at one end, called the rear, and deleted from the other end, called the front. • It is characterised by FIFO (First In First Out) principle. Front Rear • All elements are entered into the queue from a fix end called as Rear whereas all the elements are removed from an opposite end called as Front of the queue.

Implementation of Queue Basic Structure of a Queue: -Data structure that hold the queue

Implementation of Queue Basic Structure of a Queue: -Data structure that hold the queue -front -back/rear

[0] [1 ] [2] [3] queue. Front [0] [1] [2 ] [3] queue. Front

[0] [1 ] [2] [3] queue. Front [0] [1] [2 ] [3] queue. Front Queue Operation queue. Rear =>initialize. Queue =>add. Queue=>delete. Queue =>is. Empty. Queue =>is. Full. Queue queue. Rear

Limitation of Linear Queues • By the definition of a queue, when we add

Limitation of Linear Queues • By the definition of a queue, when we add an element in Queue, rear pointer is increased by 1 whereas, when we remove an element front pointer is increased by 1. But in array implementation of queue this may cause problem as follows: • Consider operations performed on a Queue (with SIZE = 5) as follows: • 1. Initially empty Queue is there so, front = 0 and rear = -1

2. When we add 5 elements to queue, the state of the queue becomes

2. When we add 5 elements to queue, the state of the queue becomes as follows with front = 0 and rear = 4 10 20 30 40 50 3. Now suppose we delete 2 elements from Queue then, the state of the Queue becomes as follows, with front = 2 and rear = 4 30 40 50

4. Now, actually we have deleted 2 elements from queue so, there should be

4. Now, actually we have deleted 2 elements from queue so, there should be space for another 2 elements in the queue, but as rear pointer is pointing at last position and Queue overflow condition (Rear == SIZE-1) is true, we can’t insert new element in the queue even if it has an empty space. To overcome this problem there is another variation of queue called CIRCULAR QUEUE.

Circular queue A circular queue is a particular implementation of a queue. It is

Circular queue A circular queue is a particular implementation of a queue. It is very efficient. It is also quite useful in low level code, because insertion and deletion are totally independant, which means that you don’t have to worry about an interrupt handler trying to do an insertion at the same time as your main code is doing a deletion. **Front=Head** Rear/Back=Tail

How does it work? • A circular queue consists of an array that contains

How does it work? • A circular queue consists of an array that contains the items in the queue, two array indexes and an optional length. The indexes are called the head and tail pointers and are labelled H and T on the diagram. • The head pointer points to the first element in the queue, and the tail pointer points just beyond the last element in the queue. If the tail pointer is before the head pointer, the queue wraps around the end of the array.

Operation of circular queue Insertion and deletion are very simple. To insert, write the

Operation of circular queue Insertion and deletion are very simple. To insert, write the element to the tail index and increment the tail. (add. Queue) To delete, save the head element and increment the head. (delete. Queue)

Implementation • A circular buffer first starts empty and of some predefined length. For

Implementation • A circular buffer first starts empty and of some predefined length. For example, this is a 7 -element buffer: • Assume that a 1 is written into the middle of the buffer (exact starting location does not matter in a circular buffer): • Then assume that two more elements are added — 2 & 3 — which get appended after the 1: •

 • If two elements are then removed from the buffer, the oldest values

• If two elements are then removed from the buffer, the oldest values inside the buffer are removed. The two elements removed, in this case, are 1 & 2 leaving the buffer with just a 3(Deletion): • If the buffer has 7 elements then it is completely full: • A consequence of the circular buffer is that when it is full and a subsequent write is performed, then it starts overwriting the oldest data. In this case, two more elements — A & B — are added and they overwrite the 3 & 4:

 • Finally, if two elements are now removed is 5 & 6 because

• Finally, if two elements are now removed is 5 & 6 because A & B overwrote the 3 & the 4 yielding the buffer with: • Alternatively, the routines that manage the buffer could prevent overwriting the data and return an error or raise an exception. Whether or not data is overwritten is up to the semantics of the buffer routines or the application using the circular buffer.

Limitation of circular queue • There is a problem with this: Both an empty

Limitation of circular queue • There is a problem with this: Both an empty queue and a full queue would be indicated by having the head and tail point to the same element. End Start 6 7 8 9 End Start A Empty queue B C Full queue

Solution • To keep a count(to implement the queue, count++ means new element added

Solution • To keep a count(to implement the queue, count++ means new element added to the queue or count—means an element removed from the queue) • To let Head/Front indicate the index of the array position preceding the first element of the queue, rather than the index of the (actual) first element itself [0] [1] [2] [97] [98] [99] (queue elements ) Reserved slot Head/Front=1 Tail/Rear/Back=97

APPLICATION OF QUEUES. • Queuing system Consists of servers and queues of objects waiting

APPLICATION OF QUEUES. • Queuing system Consists of servers and queues of objects waiting to be served. -theater problem o when cashier serving a customer, the next customer must wait. o FIFO system is applied. o This section examines computer simulations in which queue are the basic data structure. Other exp: Grocery store Banking system Printing request to a networked printer that shared by many people.

 • Priority queues -in some cases the FIFO algorithm should be relaxed. -jobs

• Priority queues -in some cases the FIFO algorithm should be relaxed. -jobs with higher priority are pushed to the front of queue. -implement: o Ordinary linked list( high to low priority) o Treelike stucture • Hospital environment Queues used to ensure patients are seen in order they arrive but if there patients that arrive with severe symptoms. they are threated first. • Simulation -A technique in which one system models the behavior of another system -used when it is too expensive or dangerous to experiment with real system -usually modeled by computers shortly Exp: - flight simulators are used to train airline pilots

THANK YOU

THANK YOU